Using if statement to display info based on page title
-
I’m trying to display posts within a page based on the the title of the page using categories. For example if I had a page titled Accessories I want all posts with the category accessories to display within that page.
In basic terms I want to check to see what page the user is on, take posts that have a category matching the page name and then display those posts on that page.
I’m using the following code to accomplish this:
<?php if (is_page('shotguns')) { query_posts('category_name=shotguns&showposts=10'); } elseif (is_page('rifles')) { query_posts('category_name-rifles&showposts=10'); } elseif (is_page('handguns')) { query_posts('category_name-handguns&showposts=10'); } elseif (is_page('accessories')) { query_posts('category_name-accessories&showposts=10'); } ?> <?php while (have_posts()) : the_post(); ?> <div class="search-excerpt"> <div class="excerpt"><?php the_excerpt(); ?></div> <p class="permalink"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permalink to <?php the_title_attribute(); ?>">Read more...</a></p> </div> <p class="date"><?php the_time('l, F jS, Y') ?> <p class="postmetadata"><?php the_tags('Tags: ', ', ', ''); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php endwhile; ?>
The first if statement queries the proper posts, but each of the following elseif statements add the previous query to the post. I’ve tried nesting the while statement within each if statement, but that makes the problem worse. The website I’m working on currently displays this problem if anyone wants to look at it. The ‘Shotguns’ page displays properly (the first queried page), but the other page do not. I’m sure its a relatively simple mistake, but I may be going about this entirely backwards. Any help would be appreciated! Thanks,
– Heath G.
- The topic ‘Using if statement to display info based on page title’ is closed to new replies.