有没有办法修改_content((函数?当css类显示自定义的否定和肯定分类法时,我想添加一个css类。
示例:
<p class="positive">this is a content for the positive taxonomy</p>
<p class="positive">this is a content for the positive taxonomy</p>
<p class="negative">this is a content for the negative taxonomy</p>
我想在author.php代码中应用它:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('No posts by this author.'); ?></p>
<?php endif; ?>
使用函数.php:
add_action( 'pre_get_posts', function ( $q ) {
if( !is_admin() && $q->is_main_query() && $q->is_author() ) {
$q->set( 'posts_per_page', 100 );
$q->set( 'post_type', 'custom_feedback' );
}
});
PS:我在这里使用自定义的帖子类型和自定义的分类法,有两个类别的正面和负面。
您可以使用has_term()
来测试帖子是否有特定的术语。或者,您可以使用get_the_terms
将术语附加到帖子中,并在css类中使用术语slug作为值。如果一篇文章附有一个以上的术语,这就有点不可靠了
解决方案1
<?php
$class = '';
if ( has_term( 'positive', 'custom_taxonomy' ) ) {
$class = 'positive';
} elseif ( has_term( 'negative', 'custom_taxonomy' ) ) {
$class = 'negative';
}
?>
<div class="entry-content ><?php echo $class ?>">
<?php the_content(); ?>
</div>
解决方案2
<?php
$terms = get_the_terms( $post->ID, 'custom_taxonomy' );
$class = $terms ? $terms[0]->slug : 'normal';
?>
<div class="entry-content ><?php echo $class ?>">
<?php the_content(); ?>
</div>
用法
您现在可以使用CSS选择器来定位您的内容
.entry-content positive {}
.entry-content negative {}