Support » Themes and Templates » Check if post in search results loop

  • Resolved Darrell Schauss

    (@dralezero)


    My <?php if(is_single()){ ?> line doesn’t do anything. If I put an }else{ on it, it always shows the else result. I want to check if the item in the search result is a post or a page. I only want to show category and date meta info if it is a post. Doesn’t make since to show that if its a page.

    <?php if (have_posts()) : ?>
    
    			<h2 class="pagetitle">Search Results</h2>
    
    			<div class="navigation">
    				<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    				<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    			</div>
    			<div class="clear"></div>
    
    			<?php while (have_posts()) : the_post(); ?>
    
    				<div <?php post_class() ?>>
    					<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    
    					<?php if(is_single()){ ?>
    					<p class="postmetadata">
    					<small><?php the_time('l, F jS, Y') ?></small>
    					<br />
    					Posted in <?php the_category(', ') ?></p>
    					<?php } ?>
    				</div>
    
    			<?php endwhile; ?>
    
    			<div class="navigation">
    				<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    				<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    			</div>
    
    			<?php else : ?>
    
    				<h2 class="center">No posts found. Try a different search?</h2>
    				<?php get_search_form(); ?>
    
    			<?php endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi

    (@esmi)

    Forum Moderator

    Try changing:

    <?php if(is_single()){ ?>

    to:

    <?php if(!is_page()){ ?>

    Ok I tried that and it always shows the meta then. I have a specific word that will give me a result of Page and a Post. Both behave the same. I have a feeling its checking the search page itself with the condition?

    esmi

    (@esmi)

    Forum Moderator

    Got it! <?php if($post->post_type == 'post') :?>

    That works.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Check if post in search results loop’ is closed to new replies.