WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Specific Post based on category (3 posts)

  1. acmc
    Member
    Posted 6 years ago #

    I am trying to create a template page to serve as category pages with an else if to display specific post based on category. New to php, so i think my syntax is incorrect. This is what i have pieced together:
    <?php get_header(); ?>
    <div id="content" class="narrowcolumn">

    <?php if (is_category('3') ):
    ?>

    <?php query_posts('p=17'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h4><?php the_title(); ?></h4>
    <?php the_content(); ?>

    <?php } elseif (is_category('13') ):

    <?php query_posts('p=11'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h4><?php the_title(); ?></h4>
    <?php the_content(); ?>

    <?php else : ?>
    //whatever goes in here
    <?php endif; ?>

    Displays blank page. Unsure what I am doing wrong. I will continue searching, but it seems the search function on the wp site is not working right now making it exceedingly difficult:-/ any help GREATLY appreciated

  2. Vaughan
    Member
    Posted 6 years ago #

    Hi,

    Most of your code here was correct and you got the use of is_category and query_posts perfectly (those usually trip up many of us :) The only issues were where and how you were closing your if structures and while loops. For example, in line 12 see how you have a closing bracket but no opening bracket earlier?

    It's also a little complex because you're using the 'shorthand' style of notating decision structures (by using the : instead of brackets). There's nothing wrong with that, but I just find it less clear than brackets. If you are going to do that though, remember to use endif; and endwhile; where appropriate.

    I've fixed this as follows and for demonstration purposes combined shorthand for the while loops and brackets for the if structures:

    <?php get_header(); ?>
    <div id="content" class="narrowcolumn">
    
    <?php if (is_category('3') ) { ?>
     <?php query_posts('p=17'); ?>
     <?php while (have_posts()) : the_post(); ?>
       <h4><?php the_title(); ?></h4>
       <?php the_content(); ?>
     <?php endwhile;
    } elseif (is_category('13') ) { ?>
     <?php query_posts('p=11'); ?>
     <?php while (have_posts()) : the_post(); ?>
       <h4><?php the_title(); ?></h4>
       <?php the_content(); ?>
     <?php endwhile;
    } else { ?>
     //whatever goes in here
    <?php } ?>

    Hope that helps.
    Vaughan

  3. acmc
    Member
    Posted 6 years ago #

    Vaughan

    You are a life-saver! thank you. will continue to play and see if i can get this thing to behave;-)

    **Ann

Topic Closed

This topic has been closed to new replies.

About this Topic