• Resolved mheathgriffin

    (@mheathgriffin)


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • hmmm. Well the code pasted above only has the code correct for shotguns. I hope it is that simple.

    The other 3 need to be changed to “category_name=”, they are currently “category_name-“.

    So, corrected would be:

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

    Hope that works.

    Not sure if this would be ideal for you or not, but a, perhaps, more simplified way to do what you are doing, that would be a bit more dynamic would be like this:

    <?php
    	$page_name = $wp_query->post->post_name;
    	query_posts('category_name='. $page_name);
    ?>
    <?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&hellip;</a></p>
    	</div>
    	<p class="date"><?php the_time('l, F jS, Y') ?></p>
    	<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 ?'); ?></p>
    <?php endwhile; ?>

    And if you wanted to limit it to certain pages, you could change the first part to this:

    <?php
    	if( is_page('shotguns') || is_page('rifles') || is_page('handguns') || is_page('accessories') ){
    		$page_name = $wp_query->post->post_name;
    		query_posts('category_name='. $page_name);
    	}
    ?>
    Thread Starter mheathgriffin

    (@mheathgriffin)

    Thanks jcow,

    I just arrived at the same conclusions myself. The actual problem was my ‘-‘ typo needed to be an ‘=’. That being said, I’m inclined to go with your variation as I’ve fiddled with a more cumbersome way of using the wp_query but I like your cleaner code instead. Thanks for the quick reply!

    – Heath G.

    Thread Starter mheathgriffin

    (@mheathgriffin)

    @jcow or any one else that might know, I’ve come across another issue using query_posts in this way. Right now everything seems to work fine, but while using next/prev links, each subpage shows the same latest 10 entries rather than older/newer entries. I’ve included the full page code and a link to the site in hopes that someone can see my error.

    <div id="main">
    	<h1><?php the_title(); ?></h1>
    	<?php if (is_page('about') || is_page('shotguns') || is_page('rifles') || is_page('handguns') || is_page('accessories')) : while (have_posts()) : the_post();?>
    		<div id="page-info"><?php the_content(); ?></div>
    	<?php endwhile; endif; ?>
    	<?php				
    
    		if( is_page('shotguns') || is_page('rifles') || is_page('handguns') || is_page('accessories') ){
    
    			$page_name = $wp_query->post->post_name;
    			query_posts('category_name='. $page_name);
    
    		while (have_posts()) : the_post(); ?>
    			<div class="search-excerpt">
    				<h3><?php the_title(); ?></h3>
    				<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>
    			<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    		<?php endwhile; }
    	?>
    	<!-- Page Navigation -->
    	<div class="pages">
    		<div class="newer"><?php previous_posts_link('&laquo; Previous 10 Entries'); ?></div>
    		<div class="older"><?php next_posts_link('Next 10 Entries &raquo;'); ?></div>
    	</div>
    	<div class="pages-shown">
    		<?php if (function_exists('wp_searchheader')) : ?>
    		<?php wp_searchheader()?>
    		<?php endif; ?>
    	</div>
    </div>

    Link to site

    Thanks in advance for any and all help

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using if statement to display info based on page title’ is closed to new replies.