Wordpress - 将特殊的 类 添加到特定的术语循环
Wordpress - Adding special classes to a specific term loop
我想遍历所有 posts 以获得自定义 post 类型,但是在循环它们时将 class 添加到标记为某个词。
我目前有以下功能,但是这只抓取带有特定术语的 post,而不是旁边的所有 post。
/**
* 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;
}
我当前的循环与上面
$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 } ?>
只是想知道我如何编辑上面的内容以确保它遍历所有 posts,但是将 class 应用于特定的 posts 与术语 5。这是用于造型目的。
谢谢,
DIM3NSION
我不知道我是否理解正确,但可能是这样的:
<?php if ($term === 'theTerm') :?>
<div class="classfortheterm"></div>
<?php endif ?>
在 foreach
循环中,您可以使用 has_term()
函数来检查 post 是否属于某个类别。如果是,则添加 class 或您要执行的任何操作。
$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
}
我想遍历所有 posts 以获得自定义 post 类型,但是在循环它们时将 class 添加到标记为某个词。
我目前有以下功能,但是这只抓取带有特定术语的 post,而不是旁边的所有 post。
/**
* 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;
}
我当前的循环与上面
$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 } ?>
只是想知道我如何编辑上面的内容以确保它遍历所有 posts,但是将 class 应用于特定的 posts 与术语 5。这是用于造型目的。
谢谢,
DIM3NSION
我不知道我是否理解正确,但可能是这样的:
<?php if ($term === 'theTerm') :?>
<div class="classfortheterm"></div>
<?php endif ?>
在 foreach
循环中,您可以使用 has_term()
函数来检查 post 是否属于某个类别。如果是,则添加 class 或您要执行的任何操作。
$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
}