blesseld Posted January 23, 2009 Share Posted January 23, 2009 Hey All, I am working with WordPress and have modified the template, however; im not to familiar with php, just enough to move stuff around. I do understand a bit of the syntax as well. I basically would like to drop in a <div> that will hold a list which in turn will be a left navigation. Possibly using an include. ex. <?php include("menu1_about.php"); ?> And that worked. ... Now What I have been trying to do is allow the include to be placed in according to what my page ID is ...ex -> my site-> .com/sp/?page_id=2. that way If ?page_id=2 .... include(blah) or something along those lines. I have been using a piece of code to ID divs .. .ex <div class="page" id="post-<?php the_ID(); ?>"> outcome is <div id="post-2" class="page"> Anyone have any suggestions, I am clueless and have been looking all over but just can't seem to find an answer...biggest problem is Im not fully knowledgable in php... html/css no problem. thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/ Share on other sites More sharing options...
Philip Posted January 23, 2009 Share Posted January 23, 2009 You're going to want to use GET. This is a very basic example of what you want: <?php // Use get to retrieve the value of a variable in a url. // pagename.php?variable=value $pageID = $_GET['pageID']; // Now you can use the value ($pageID) in that script echo '<div class="page" id="post-',$pageID,'">'; // That'll show: // <div class="page" id="post-2"> // If you want to include a file, based on the page, I'd recommend checking it against an array // since the GET variables can be easily modified. ?> Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744081 Share on other sites More sharing options...
dvd420 Posted January 23, 2009 Share Posted January 23, 2009 Hi, I think, create an array containing page id's and corresponding includes. $inc_list = array(1 => "include1.php", 3 => "hello.php", 11 => "welcome.php"); if( file_exists($inc_list[ $_GET['PageID'] ]) ) include( $inc_list[ $_GET['PageID'] ]); else include("default.php"); Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744099 Share on other sites More sharing options...
Snowmiser Posted January 23, 2009 Share Posted January 23, 2009 Maybe something like this. <?php $page_id = isset($_GET['page_id']) ? $_GET['page_id'] : ''; switch ($page_id) { case 'page1': { include('some_page.php'); break; } case 'page2': { include('some_other_page.php'); break; } default: { include('some_default_page.php'); break; } } ?> I don't think it'd be a good idea to put the user input directly into your include(). Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744100 Share on other sites More sharing options...
blesseld Posted January 23, 2009 Author Share Posted January 23, 2009 thanks for the replies, I'm starting to get somewhere. But I think I am misunderstanding something. The Array is not grabbing my Page ID, It will include whatever I set "else" to . here's my code <body> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="page" id="post-<?php the_ID(); ?>"> <div id="header"> <div id="nav"> <?php wp_list_pages('title_li=' ); ?> </div> </div> <div class="header_main"><h2><?php the_title(); ?></h2> <?php endwhile; endif; ?> <?php //Trying to get this to work----------- $inc_list = array(2 => "decor_leftnav.php", 3 => "hello.php", 11 => "welcome.php"); if( file_exists($inc_list[ $_GET['PageID'] ]) ) include( $inc_list[ $_GET['PageID'] ]); else include("maintest.php"); ?> </div> and currently to add the page ID just to a div...I need to do this....but this was already in place form word press I just discovered and started using. <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="page" id="post-<?php the_ID(); ?>"> <?php endwhile; endif; ?> can anyone help with why I'm unable to grab my ID with the array, thanks again Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744319 Share on other sites More sharing options...
blesseld Posted January 23, 2009 Author Share Posted January 23, 2009 Little update. got this to work <?php if($_GET['page_id'] == 2) { ?> <?php include("decor_leftnav.php"); ?> <?php } if($_GET['page_id'] == 3) { ?> <?php include("maintest.php"); ?> <?php } ?> Is this a good way of doing or should I still try to get my Array to work? Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744426 Share on other sites More sharing options...
haku Posted January 23, 2009 Share Posted January 23, 2009 It's essentially the same thing that Snowmiser gave you, except that yours has no default if the URL variable is missing, and his does. And his is just cleaner code - you should go with it. Plus, you don't need to keep closing and opening new php tags. If its php right after php, just keep it open. Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744563 Share on other sites More sharing options...
xangelo Posted January 23, 2009 Share Posted January 23, 2009 Little update. got this to work <?php if($_GET['page_id'] == 2) { ?> <?php include("decor_leftnav.php"); ?> <?php } if($_GET['page_id'] == 3) { ?> <?php include("maintest.php"); ?> <?php } ?> Is this a good way of doing or should I still try to get my Array to work? It works, but you don't need to open and close the tags that many times. Also, I would include a catch-all. A final else statement that will display whatever you want incase the argument doesn't match: <?php if($_GET['page_id'] == 2) { include("decor_leftnav.php"); } else if($_GET['page_id'] == 3) { include("maintest.php"); } else { include("404.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744568 Share on other sites More sharing options...
Philip Posted January 23, 2009 Share Posted January 23, 2009 Little update. got this to work <?php if($_GET['page_id'] == 2) { ?> <?php include("decor_leftnav.php"); ?> <?php } if($_GET['page_id'] == 3) { ?> <?php include("maintest.php"); ?> <?php } ?> Is this a good way of doing or should I still try to get my Array to work? It works, but you don't need to open and close the tags that many times. Also, I would include a catch-all. A final else statement that will display whatever you want incase the argument doesn't match: <?php if($_GET['page_id'] == 2) { include("decor_leftnav.php"); } else if($_GET['page_id'] == 3) { include("maintest.php"); } else { include("404.php"); } ?> Or use a switch, which is easier to maintain IMO. Honestly, I would go with Snowmiser's code, it's going to be a lot easier and cleaner in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744571 Share on other sites More sharing options...
blesseld Posted January 23, 2009 Author Share Posted January 23, 2009 Awsome...thanks alot, everyone....you've help out a bunch I shall return with more questions. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/142081-how-to-add-an-include-according-to-page-id/#findComment-744595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.