根据类别显示多个循环

时间:2020-03-23 作者:RLM

如何让这个循环检测特定的类别,如果是不同的类别,如何更改html。

换句话说,如果检测到一个类别,我想用与当前吐出的内容不同的特定类别来设置博客的样式。

这是我的代码:

<?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();
?>

1 个回复
最合适的回答,由SO网友:disinfor 整理而成

您可以使用wp_get_post_terms()in_array() 要检查“投资”是否在术语数组中,请执行以下操作:

<?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();
?>
我对所有的事情都发表了评论,所以你应该能够了解正在发生的事情。

相关推荐

Disable RSS Feed

我想禁用rss提要,我的wordpress版本是5.2.4.请参阅下面的代码以禁用rss提要。 function itsme_disable_feed() { wp_die( __( \'No feed available\' ) ); } add_action(\'do_feed\', \'itsme_disable_feed\', 1); add_action(\'do_feed_rdf\', \'itsme_disable_feed\', 1);