skip to Main Content

I’m developing a custom theme for my website and I can’t seem to check if the current page is a blog post or a page. How would I do this?

My code so far (post.php):

<?php get_header(); ?>

<?php if (is_front_page()) : ?>
    <div class="hero">
        <h1>hello world</h1>
    </div>
<? elseif (is_page()) : ?>
    <div class="hero">
        <h1><?php the_title(); ?></h1>
    </div>
<? elseif (is_singular()) : ?>
    <div class="hero">
        <h1><?php the_title(); ?></h1>
        <p>Date: <?php echo get_the_date(); ?></p>
        <p>Categories:</p>
    </div>
<?php endif; ?>

<?php get_footer(); ?>

2

Answers


  1. The WordPress helper function is_singular() checks if the current request "is for an existing single post of any post type".

    if ( is_singular( 'post' ) ) {
        // request is for a single post.
    }
    
    if ( is_singular( 'page' ) ) {
        // Request is for a single page.
    }
    

    There are additional helper functions, like is_single() and is_page() that vary slightly from is_singular().

    Login or Signup to reply.
  2. Hi you can use something like this….

    <?php
      get_header(); 
      if(get_post()->post_type === 'page') { 
    ?>
    
    <div class="hero">
        <h1>hello world</h1>
    </div>
    <? 
      }elseif (get_post()->post_type === 'post') { 
    ?>
    <div class="hero">
        <h1><?php the_title(); ?></h1>
    </div>
     <? 
       }else (is_singular()) { 
     ?>
    <div class="hero">
        <h1><?php the_title(); ?></h1>
        <p>Date: <?php echo get_the_date(); ?></p>
        <p>Categories:</p>
    </div>
    <?php } ?>
    
    <?php get_footer(); ?>
    

    Hopefully this one can help you…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search