WordPress如何保持我的cpt的the_content过滤器不影响同一页面上的其他帖子

时间:2014-05-15 21:16:37

标签: wordpress

我在主题中为幻灯片使用自定义帖子类型。我试图使用the_content过滤器钩子从cpt中删除wpautop,它使用以下代码工作,但它也将wpautop从同一页面上的其他查询中删除。这是代码:

add_filter( 'the_content', 'remove_autop_for_post_type', 0 );

function remove_autop_for_post_type( $content )
{
if('par2_slide' === get_post_type(get_the_ID())){
    remove_filter( 'the_content', 'wpautop' );
    return $content;
};
return $content;
};

添加第二个条件以包含"! is_main_query()如下:

if('par2_slide' === get_post_type(get_the_ID()) && ! is_main_query()){

导致脚本停止工作。我应该应用的cpt的查询如下:

function par2_slides_query () {
$args = array( 'post_type' => 'par2_slide');
$slides_loop = new WP_Query( $args );
while ( $slides_loop->have_posts() ) : $slides_loop->the_post();
echo '<li class="slide">';
the_content();
echo '</li>';
endwhile;
};

wpauto混淆了幻灯片布局,所以我真的需要过滤掉特定帖子类型的内容以关闭它而不关闭页面其余部分的the_content实例。它不会影响同一页面上the_content上的其他页面。

1 个答案:

答案 0 :(得分:0)

IF声明后重新添加过滤器。

add_filter('the_content', 'wpautoop');

删除它并返回内容IF par2_slide是帖子类型,如果不是,则会添加过滤器(如果它不存在)。