Home > Software design >  How do i only display posts with a certain category?
How do i only display posts with a certain category?

Time:10-05

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 >
                <a href="<?php the_permalink(); ?>"   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 >
            <header >
                <?php the_title( sprintf( '<h1 ><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

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

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

CodePudding user response:

Use has_category()

if(has_category('certain-category', get_the_ID())):
    // Do something
endif;

CodePudding user response:

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

  • Related