Jump to content

Recommended Posts

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.

 

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.
?>

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");

 

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().

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

 

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'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.

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"); 
}
?>

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.