• 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; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

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

    Thread Starter jimmiejo

    (@jimmiejo)

    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:

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

    Thanks again, Otto!

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using in_category on search results page to customize the results’ is closed to new replies.