1

How do I get this loop to detect a specific category, and change the html if it's a different category.

In other words, if a the category is detected, I want to style the blogs with the specific category differently than what's currently spit out.

Here is my code:

<?php $catquery = new WP_Query(array(
      'orderby' => 'date',
      'order' => 'DESC'
)); ?>

<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );?>
<div class="row episodes-feed-wrap">
    <div class="col-4 episodes-feed-thumb">
        <a href="<?php the_permalink() ?>" rel="bookmark">
        <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
        </a>
    </div>
    <div class="col-8 featured-article">
        <div class="container" style="padding:0;">
            <h4 class="episodes-feed-cat">
            <?php $categories = get_the_category();
            if ( ! empty( $categories ) ) {
                echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
            }?>
            </h4>
            <a href="<?php the_permalink() ?>" rel="bookmark">
                <h2 class="episodes-title">
                <?php the_title(); ?>
                </h2>
            </a>
            <div class="episodes-excerpt">
                <a href="<?php the_permalink() ?>" rel="bookmark">
                <?php echo excerpt(25); ?>
                </a>
            </div>
            <div class="episodes-feed-info-wrap">
                <div class="episodes-feed-author">
                    <a href="<?php the_permalink() ?>" rel="bookmark">
                    <?php the_author(); ?>  &#8226; <?php echo meks_time_ago(); /* post date in time ago format */ ?>
                    </a>
                </div>
            </div>
        </div><!-- end container --> 
    </div>
</div>
<?php
    endwhile;
    wp_reset_postdata();
?>
  • I didn't understand what you meant in your question, you never actually state what the structure you wanted is. But judging from what I see in the code, are you trying to display posts in columns? – Tom J Nowell Mar 23 at 18:04
  • @TomJNowell I just want the existing loop above to recognize if a category is called "investments" and if it is, then it has it's own html structure. – RLM Mar 23 at 18:11
  • So you're asking how to test if a post has a particular category or not? If that's the case, then the other stuff is a red herring that'll confuse people trying to read your question. Edit your question using the edit link to make it clear what you're asking. Note that any answer you get will require basic PHP skills to understand – Tom J Nowell Mar 23 at 18:16
  • @TomJNowell Ok I edited it. Thanks for you time. – RLM Mar 23 at 18:22
  • 1
    @disinfor yes, that is correct. thanks. – RLM Mar 24 at 15:55
1

You can use wp_get_post_terms() and in_array() to check if "investments" is in the term array or not:

<?php $catquery = new WP_Query(array(
    'orderby' => 'date',
    'order' => 'DESC'
)); ?>

<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
    <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );?>

    <?php
        // Gather all the post terms for the post.
        $post_term_objects = wp_get_post_terms( $post->ID, 'category' );
        // Empty array to hold our terms
        $post_terms = [];
        // loop through the objects and put the slug (this could be name as well, but slug is easier to manage)
        foreach ( $post_term_objects as $post_term_object ) {
            // put the terms into the array.
            $post_terms[] = $post_term_object->slug;
        }
    ?>

    <div class="row episodes-feed-wrap">
        <!-- CHECK IF investments is NOT in the array -->
        <?php if ( ! in_array( 'investments', $post_terms, false ) ) : ?>
            <div class="col-4 episodes-feed-thumb">
                <a href="<?php the_permalink() ?>" rel="bookmark">
                    <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>
                </a>
            </div>
            <div class="col-8 featured-article">
                <div class="container" style="padding:0;">
                    <h4 class="episodes-feed-cat">
                        <?php $categories = get_the_category();
                        if ( ! empty( $categories ) ) {
                            echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
                        }?>
                    </h4>
                    <a href="<?php the_permalink() ?>" rel="bookmark">
                        <h2 class="episodes-title">
                            <?php the_title(); ?>
                        </h2>
                    </a>
                    <div class="episodes-excerpt">
                        <a href="<?php the_permalink() ?>" rel="bookmark">
                            <?php echo excerpt(25); ?>
                        </a>
                    </div>
                    <div class="episodes-feed-info-wrap">
                        <div class="episodes-feed-author">
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <?php the_author(); ?>  &#8226; <?php echo meks_time_ago(); /* post date in time ago format */ ?>
                            </a>
                        </div>
                    </div>

                </div><!-- end container -->
            </div>
        <?php else : ?>
            <!-- PUT YOUR MARK UP FOR INVESTMENTS HERE -->
        <?php endif; ?>
    </div>

<?php endwhile;
wp_reset_postdata();
?>

I commented everything so you should be able to follow what is happening.

| improve this answer | |
  • Perfect. Thanks. – RLM Mar 24 at 16:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.