0

I have a search page that when you enter a blank query, it shows posts and pages together. This isn't an issue, it's fine. However I'd like to remove some details like the date from the pages as it doesn't make much sense.

I'm using the is_page function but that doesn't seem to work and I have no idea why, I'm including all of the necessary information (or so I thought).

<?php while ( have_posts() ) : the_post(); ?>

    <article id="post-<?php the_ID(); ?>">

        <?php if ( is_page() ) : ?>

            <?php the_title(); ?>

        <?php else : ?>

            <?php the_title(); ?>

            <?php the_time("jS F Y"); ?>

            <?php foreach ( get_the_category() as $the_category ) : ?>

                <?php echo $the_category->cat_name; ?>

            <?php endforeach; ?>

        <?php endif; ?>

    </article>

<?php endwhile; ?>

The is_page function in the WP Codex.

0

The is_page() depends on how the request maps to the main query variables, so you could instead check the post type within the loop:

the_title();

if( 'post' === get_post_type() ) {
    // display some extra stuff
}
  • PS: note how we have simplified things here. 1) We don't need an else part if we check for the 'post' post type instead of 'page'. 2) We've removed the PHP tag spam to improve readability. – birgire Jan 21 '17 at 9:08
0
<?php while ( have_posts() ) : the_post(); ?>

<?php

    $posttype = get_post_type();

    if ($posttype == 'page') { ?>

        <?php the_title(); ?>

<?php } else { ?>

        <?php the_title(); ?>

        <?php the_time("jS F Y"); ?>

        <?php foreach ( get_the_category() as $the_category ) : ?>

            <?php echo $the_category->cat_name; ?>

        <?php endforeach; ?>

<?php } ?>

You can check for any post type, including custom post types.

0

Edited foreach could easily messed up in if..else condition. Try to use following code `

<article id="post-<?php the_ID(); ?>">

    <?php if ( is_page() ) : ?>

        <?php the_title(); ?>

    <?php else : ?>

        <?php the_title(); ?>

        <?php the_time("jS F Y"); ?>

    <?php endif; ?>

        <?php foreach ( get_the_category() as $the_category ) : ?>

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

            <?php echo $the_category->cat_name; ?>

        <?php endif; ?>

        <?php endforeach; ?>

</article>

`

  • Why do you say that foreach doesn't work in if..else blocks? I've never heard of that or experienced it myself. – birgire Jan 21 '17 at 8:14
  • foreach works in if..else. But, it could be mess up easily (It happens to me). Suppose, you could. is_page() return true only on single page. otherwise, it always false. I thought you messed up this login when make complicated loop. – Md. Mrinal Haque Jan 21 '17 at 8:39
  • Not sure what you mean by "I thought you messed up", but in my answer I removed the PHP tag spam to improve readability. Then I simplified the original snippet as well by avoiding the_title() duplication and unnecessaryelse part, by checking for 'post' instead of 'page'. Also I don't use is_page() as it depends on the request. – birgire Jan 21 '17 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy