skip to Main Content

So I’m building a website for my new business.

An I want to only run the following code if the post have a category of ‘certain-category’.

How do I do this? I tried a number of things. but they do not work..“

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

    <?php if ( has_post_thumbnail() ) : ?>
            <div class="entry-thumb">
                <a href="<?php the_permalink(); ?>" class="H1-posts"  title="<?php the_title(); ?>" >
                    <?php the_post_thumbnail('moesia-thumb'); ?>
                </a>
            </div>
        <?php endif; ?>


        <?php if (has_post_thumbnail()) : ?>
            <?php $has_thumb = ""; ?>
        <?php else : ?>
            <?php $has_thumb = ""; ?>
        <?php endif; ?>

        <div class="post-content <?php echo $has_thumb; ?>">
            <header class="entry-header">
                <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

                <?php if ( 'post' == get_post_type() ) : ?>
                
                <?php endif; ?>
            </header><!-- .entry-header -->

            <div class="entry-summary">
                <?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
                    <?php the_content(); ?>
                <?php else : ?>
                    <?php the_excerpt(); ?>
                <?php endif; ?>
            </div><!-- .entry-content -->

2

Answers


  1. Use has_category()

    if(has_category('certain-category', get_the_ID())):
        // Do something
    endif;
    
    Login or Signup to reply.
  2. WordPress now has a native block which does this for you if you want the easy route the block is called Post and Page Grid it allows you to select what category of posts or pages it will show and you can select what information is shown e.g. Title, thumbnail, excerpt etc

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