• Hi There,

    I am trying to create a page that has some general information and then shows below all posts from a certain category

    I am using a page template file and have added the following code
    <?php
    if ($page_id=376){
    query_posts(‘cat=23’);
    };
    if ($page_id=389){
    query_posts(‘cat=44’);
    };
    if ($page_id=’396′){
    query_posts(‘cat=65’);
    };
    if ($page_id=’397′){
    query_posts(‘cat=66’);
    };
    ?>
    Basically if the page id is 376 then I would like show all the posts from category 23.
    At the moment it only seems to be showing posts from category 66 (last one) no matter what page. Im assuming I need to put a elseif somewhere but I cannot figure it out.

    Is there an easier way of doing this- plugin maybe?

    Thanks
    Richard

Viewing 9 replies - 1 through 9 (of 9 total)
  • Try if($page_id == 389), etc.

    Thread Starter babyuniverse

    (@babyuniverse)

    Thanks – That doesnt seem to work, it doesnt return any posts
    Should it be if($page_id == ‘389’) or if($page_id == 389)

    My latest code is as follows and it is showing no posts.
    <?php
    if ($page_id==376){
    query_posts(‘cat=23’);
    };
    if ($page_id==389){
    query_posts(‘cat=44’);
    };
    if ($page_id==396){
    query_posts(‘cat=65’);
    };
    if ($page_id==397){
    query_posts(‘cat=66’);
    };

    EDIT – it works by adding just the query_posts and manually selecting the category, the if statement isnt workinh though

    Thanks
    Richard

    vtx was correct in that if you want to compare 2 values like this inside a conditional you should use two equals signs, as one equals sign is an assignment operator, so it is like making the $page_id variable equal 397 with just one equals sign!

    Also you can use an else if, if you like, although your method would work pretty much the same way in this example.

    What you should probably do here is make use of the is_page() function WordPress has. They have many conditional statements already set up to use in your PHP if statements.

    This would make the code:

    <?php
    if (is_page('376')){
    query_posts('cat=23');
    };
    if (is_page('389')){
    query_posts('cat=44');
    };
    if (is_page('396')){
    query_posts('cat=65');
    };
    if (is_page('397')){
    query_posts('cat=66');
    };

    Try:

    <?php
    if( is_page('376') ) query_posts('cat=23');
    elseif( is_page('389') ) query_posts('cat=44');
    elseif( is_page('396') ) query_posts('cat=65');
    elseif( is_page('397') ) query_posts('cat=66');
    ?>
    Thread Starter babyuniverse

    (@babyuniverse)

    Hi All,

    Thanks this is now working, I wiould now like to use a similar if and elseif statement for adding some PHP code instead of the query post

    The code I require is <?php echo GeoMashup::map(); ?> this will add a map depending on what page is showing.

    Thanks again
    Richard

    Thread Starter babyuniverse

    (@babyuniverse)

    edit

    Thread Starter babyuniverse

    (@babyuniverse)

    Hi,

    Any idea on this?

    The php code will need to go in ‘The Loop’. You need to find which .php file is displaying the screen you want to change – it is probably index.php if it is your home page, but depends on the theme.

    Find the start of the loop, which probably looks like:
    <?php while ( have_posts() ) : the_post() ?>

    Put your if-else statements after the while and before the corresponding endwhile, depending on where on the page you want the map.

    BTW, I don’t see anything in your required code that will put a different map on each page.

    Thread Starter babyuniverse

    (@babyuniverse)

    Sorry I dont think I explained myself very well
    the code below is currently on my site and working fine.
    <?php
    if( is_page(‘376’) ) query_posts(‘cat=23’);
    elseif( is_page(‘389’) ) query_posts(‘cat=44’);
    elseif( is_page(‘396’) ) query_posts(‘cat=65’);
    elseif( is_page(‘397’) ) query_posts(‘cat=66’);
    ?>

    I was trying to create a seperate if statement something along the lines of

    <?php
    if ( is_page(‘376’) ) echo GeoMashup::map(1);
    elseif( is_page(‘389’) ) echo GeoMashup::map(2);
    elseif( is_page(‘396’) ) echo GeoMashup::map(3);
    elseif( is_page(‘397’) ) echo GeoMashup::map(4);
    ?>

    But I cannot get it to work – what am I missing?
    The code I need to run is <?php echo GeoMashup::map(1); ?> but I assume I cannot use the <?php tag within another php tag which is why I dropped it above.

    Thanks again
    Richard

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Show Posts on a Page’ is closed to new replies.