WordPress.org

Ready to get started?Download WordPress

Forums

Using in_category on search results page to customize the results (4 posts)

  1. jimmiejo
    Member
    Posted 6 years ago #

    I'm working with numerous category templates that when viewed individually offer more or less information about posts within each of them.

    I've stumped myself when it comes to carrying over those unique categorical displays to search results.

    What I'm trying to do is say "if any of the results are in category X, call on these elements for it". Or else, "if any of the results are in category y, display these other elements for only those ones".

    But I can't seem to get this to work, so I wanted to ask the WordPress experts if this is possible?

    A simplified example of my code is:

    <?php $my_query = new WP_Query($query_string . "&posts_per_page=10");
    while ($my_query->have_posts()) : $my_query->the_post();
    if ( $post->ID == $do_not_duplicate ) continue;
    update_post_caches($posts);
    $do_not_duplicate = $post->ID; ?>
    
    <?php if (in_category('1')); ?>
    
    	<div class="post" id="post-<?php the_ID(); ?>">
    		<h2 class="posttitle">
    			<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    		</h2>	
    
    		<div class="postentry">
    			<?php the_excerpt(); ?>
    		</div>
    
    		<!--
    		<?php trackback_rdf(); ?>
    		-->
    
    	</div>
    
    <?php } elseif (in_category('7,8,9,10,11')); ?>
    
    	<div class="post" id="post-<?php the_ID(); ?>">
    		<h2 class="posttitle">
    			<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    		</h2>	
    
    		<span class="postmeta"><?php comments_number('no responses','one response','% responses'); ?></span>
    
    		<!--
    		<?php trackback_rdf(); ?>
    		-->
    
    	</div>
    
    <?php endif; ?>
  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    You're on the right track, but look closely at your code:

    <?php if (in_category('1')); ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    ...
    <?php } elseif (in_category('7,8,9,10,11')); ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    ...
    <?php endif; ?>

    Kinda mixing up how your if statements work there, aren't you?

    There's two kinds of if statements:
    This:

    if (x) :
    ... code to run ...
    endif;

    or:

    if (x) {
    ... code to run ...
    }

    You can't mix and match here. Pick one or the other.

    Also, the way you are calling in_category is wrong. It can only take integers for ID's and strings for names of catgeories. It also can't do multiple like the other functions (hasn't been upgraded yet). So do it like this:

    in_category(1)
    in_category(7)||in_category(8)||in_category(9)||in_category(10)||in_category(11)

    Combine the two and you get this way:

    <?php if (in_category(1)) { ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    ...
    <?php } elseif (in_category(7)||in_category(8)||in_category(9)||in_category(10)||in_category(11)) { ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    ...
    <?php } ?>
  3. jimmiejo
    Member
    Posted 6 years ago #

    Thanks a ton Otto. That makes perfect sense.

    I'll add a reference I found moments ago for anyone that looks at this down the road:

    http://www.nietoperzka.com/wptraining/category-dependent-post-stylization/

    Thanks again, Otto!

  4. Stephen Cronin
    Member
    Posted 6 years ago #

    Otto,

    You've helped me and a lot of people enormously, so I write this with the greatest of respect, but I want to question your statment:

    Also, the way you are calling in_category is wrong. It can only take integers for ID's and strings for names of catgeories

    On the Codex In_Cateogry page it states:

    category_id
    (integer) The category ID of the category for which you wish to test. The parameter may either be passed as a bare integer or as a string:
    * in_category(5)
    * in_category('5')

    which appears to say it can take integers as strings. Can you just clarify which is correct? I'd just like to know for sure. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic