[英]Wordpress: Modifying the content function
is there a way to modify the the_content() function? 有没有办法修改the_content()函数? I would like to add a css class when it display a custom taxonomy negative and positive taxonomy. 我想在显示自定义分类法的负分类法和正分类法时添加一个CSS类。
Example: 例:
<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>
I would like to apply it in the author.php code: 我想在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; ?>
with the function.php : 与function.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: I am using custom post type here with custom taxonomy with two category positive and negative. PS:我在这里使用具有自定义分类法的自定义帖子类型,分为正面和负面两个类别。
You can use has_term()
to test whether or not a post has a certain term. 您可以使用has_term()
来测试帖子中是否包含特定术语。 Alternatvely, you can use get_the_terms
to get the terms attached to a post and use the term slug as value in your css class. 另外,您可以使用get_the_terms
获取附加到帖子中的术语,并在css类中将slug用作值。 This is a bit unreliable if a post has more than one term attached to it 如果帖子附加了多个术语,则这有点不可靠
<?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>
<?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>
You can now use your CSS selectors to target your content 现在,您可以使用CSS选择器来定位您的内容
.entry-content positive {}
.entry-content negative {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.