donglu6805 2016-04-11 16:27
浏览 33
已采纳

Wordpress - 将特殊类添加到特定术语循环

I want to loop through all posts for a custom post type however whilst looping them add a class to the posts that are tagged with a certain term.

I currently have the below function however this only grabs posts with the specific term and not all of the posts along side.

/**
 * Get posts of particular termID
*/

function post_type_tax_query($post_type=null, $taxonomy=null, $term_id=null){

    $args = array(
        'posts_per_page'  => -1,
        'post_type'       => $post_type,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field' => 'id',
                'terms' => $term_id
            )
        )
    );

    $the_query = new \WP_Query( $args );
    $queryitems = $the_query->get_posts();

    return $queryitems;

}

My current loop with the above

            $projects_posts = post_type_tax_query('projects', 'locked', '5');
                if($projects_posts){ ?>
                    <ul class="full-width projects-grid grid list-style-none">
                    <?php $counter = 1; ?>
                    <?php foreach ($projects_posts as $project):
                            $project_title = get_field('project_title', $project->ID ); // get all the rows
                            $project_image_thumbnail = get_field('project_thumbnail_image', $project->ID); ?>
                            <li class="animation-element bounce-up delay-motion-<?php echo $counter; ?> col-lg-4 col-xs-12 col-sm-6">
                                <a href="<?php echo the_permalink($project->ID); ?>" target="_blank">
                                    <figure class="effect-milo">
                                        <img src="<?php echo $project_image_thumbnail; ?>"/>
                                        <h4><?= $project_title; ?></h4>
                                    </figure>
                                </a>
                            </li>
                    <?php $counter++; ?>
                    <?php endforeach; ?>
                    </ul>
            <?php } ?>  

Just wondering how I could edit the above to ensure it loops through all posts, but applies a class to the specific posts with the term 5. This is for styling purposes.

Thanks,

DIM3NSION

  • 写回答

2条回答 默认 最新

  • douhuigang9550 2016-04-11 16:58
    关注

    Within foreach loop you can use has_term() function to check if the post belongs to a certain category. if it is, then add the class or whatever action you want to take.

    $special_class='';
    if( has_term( $term_id', $taxonomy, $post_id ) ) {
        # in your case term_id = 5
        $special_class='CLASS_YOU_WANT_ADD';
        # Print $special_class into the li class
        # OR Whatever you want to do
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题