是否可以使用函数中的过滤器向div添加动态类。php?我知道body标签也可以使用,也可以使用id自定义定义的div?
if ( !is_search() ) {
$customclass = \'myclass\';
}
是否可以使用函数中的过滤器向div添加动态类。php?我知道body标签也可以使用,也可以使用id自定义定义的div?
if ( !is_search() ) {
$customclass = \'myclass\';
}
您可以尝试使用以下方法之一,我认为除了下面这两个选项之外,没有其他具体的方法可以做到这一点。
Option 1
在模板文件中,添加post_class();
给你的部门如下:<div id="your-div" <?php post_class(); ?> >
然后您可以使用post_class
在主题或插件函数文件中进行筛选,如下所示:// Add dynamic post class
function royal_custom_class($classes) {
global $post;
$classes[] = \'your-class-name\'; // Add your class however its generated here
return $classes;
}
add_filter(\'post_class\', \'royal_custom_class\');
Option 2
您的另一个选项是,如果div是在实际内容中定义的,那么您可以使用the_content
过滤、更换和更新。类似这样:
function updated_page_content( $content )
{
return \'<div class="custom_class">Whatever goes inside</div>\'. $content;
}
add_filter( \'the_content\', \'updated_page_content\' );
References from Codex for these two methods
后类别筛选器-WordPress Codex内容筛选器-WordPress Codex
您还可以执行以下操作(进入functions.php):
function add_new_class(){
if (is_page()) {
return "page";
} elseif (is_single()) {
return "post";
} else {
//add other elseifs
}
}
然后,您可以在不同的模板中调用它:<header class="entry-header <?php echo $new_class = add_new_class(); ?>">
WordPress将根据您当前使用的模板(帖子/页面/搜索/存档等)加载不同的值。What I want: 通过创建$query = new WP_Query($args);Why: 以json格式返回特定内容,作为一种API请求,准备在另一个站点上显示What I tried first:foreach($query->posts as $post) { $post->post_content = apply_filters(\'the_content\', $post->post_content); }; 这执行了autop 和do