快捷代码在执行快捷代码之前和之后插入段落

时间:2013-10-30 作者:NW Tech

当执行我的短代码时,它会插入一个<p></p> 在短代码之前和之后。

我的短代码是页面文本编辑器中唯一的内容。我的短代码前后都没有换行符。

以下是我的短代码函数:

//* Shortcode for child pages
function list_sub_pages( $atts ) {

    extract( shortcode_atts(array(\'page\' => \'home\', \'display\' => \'excerpt\', \'show\' => \'3\', \'speed\' => \'10\'),$atts));

        $page_id = get_page_by_path($page);

    switch ( $display ) {
        case \'excerpt\':
            $content = display_page_excerpt($page_id, (int) $speed);
            break;
        case \'kids\':
            $content = display_child_pages($page_id, (int) $show, (int) $speed);
            break;
        case \'blog\':
            $content = display_blog_posts((int) $show, (int) $speed);
            break;
        default:
        break;
    }


    return $content;
}
add_shortcode(\'group\', \'list_sub_pages\');

function display_page_excerpt( $page_id = 1, $speed = 10 ){
    $page_data = get_page($page_id->ID);

    $output = \'<section id="\'.$page_id->post_name.\'">\';
    $output .= \'<div class="\'.$page_id->post_name.\'-bkgd" data-type="background" data-speed="\'.$speed.\'"></div><div class="wrap">\';
    $output .= \'<blockquote class="\'.$page_id->post_name.\'">\'.$page_data->post_excerpt.\'</blockquote>\';
    $output .= \'</div></section>\';

        return $output;
}

function display_child_pages( $page_id = 1, $show = 3, $speed = 10 ){
    global $post;

    //query subpages
    $args = array(
        \'post_parent\' => $page_id->ID,
        \'showposts\' => $show,
        \'post_type\' => \'page\',
        \'orderby\' => \'menu_order\',
        \'order\' => \'ASC\'
    );
    $subpages = new WP_query($args);

    // create output
    if ($subpages->have_posts()) :
        $output = \'<section id="\'.$page_id->post_name.\'">\';
        $output .= \'<div class="\'.$page_id->post_name.\'-bkgd" data-type="background" data-speed="\'.$speed.\'"></div><div class="wrap">\';
        $output .= \'<h2 class="\'.$page_id->post_name.\'-title home-titles"><i class="icon-home-title-\'.$page_id->post_name.\'"></i>\';
        $output .= get_the_title($page_id->ID);
        $output .= \'</h2><ul class="\'.$page_id->post_name.\'">\';
        while ($subpages->have_posts()) : $subpages->the_post();
            $output .= \'<li><a href="\'.get_permalink().\'" class="\'.$page_id->post_name.\'-link"><div class="\'.$page_id->post_name.\'-info"><div class="\'.$page_id->post_name.\'-img">\'.get_the_post_thumbnail($page->ID,\'icons\').\'</div><h2>\'.get_the_title().\'</h2>
                        <p class="\'.$page_id->post_name.\'-excerpt">\'.get_the_excerpt().\'</p>
                        </div></a></li>\';
        endwhile;
        $output .= \'</ul>\';
        $output .= \'<div style="clear: both;"></div></div></section>\';
    endif;

    // reset the query
    wp_reset_postdata();

    // return something
    return $output;
}

function display_blog_posts( $show = 3, $speed = 10 ){

    $output = \'<section id="blog" data-type="background" data-speed="\'.$speed.\'">\';
    $output .= \'<div class="blog-bkgd" data-type="background" data-speed="\'.$speed.\'"></div><div class="wrap">\';
    $output .= \'<h2 class="blog-title home-titles"><i class="icon-home-title-blog"></i>Recent News</h2><ul class="recent-posts">\';

    $args = array(
        \'numberposts\' => $show,
        \'order\' => \'post_date\',
        \'order\' => \'DESC\',
        \'post_status\' => \'publish\'
    );  
    $posts = wp_get_recent_posts($args);
    foreach( $posts as $post ){
        $post_id = $post["ID"];

        $output .= \'<li><a href="\' . get_permalink($post_id) . \'" title="\'.esc_attr($post["post_title"]).\'" ><h2>\'.$post["post_title"].\'</h2><p class="blog-excerpt">\'.$post["post_excerpt"].\'</p></a></li> \';
    }

    $output .= \'</ul>\';
    $output .= \'</div></section>\';

    // reset the query
    wp_reset_postdata();

    return $output;
}
下面是我的浏览器中HTML标记的外观。

enter image description here

所以,我需要弄清楚这些流氓段落是从哪里来的。如有任何见解,将不胜感激。谢谢

1 个回复
最合适的回答,由SO网友:Ahmad M 整理而成

我认为这是wpautop 过滤器正在运行the_content. 您可以从内容中删除此筛选器:

remove_filter(\'the_content\', \'wpautop\');
// OR if the above line does not work:
add_filter(\'the_content\', \'remove_autop\', 1);
function remove_autop($content) {
    remove_filter(current_filter(), \'wpautop\');
    return $content;
}
或者您可以使用shortcode_unautop 筛选,但这仅在短代码独立且文本编辑器中没有其他内容时才起作用:

add_filter(\'the_content\', \'shortcode_unautop\');

结束

相关推荐

Loading shortcode with ajax

如果浏览器的屏幕大小特定,我会尝试加载幻灯片,也就是说,我不希望幻灯片加载到更小的移动屏幕上。幻灯片放映插件提供了一个短代码:<?php echo do_shortcode(\'[metaslider id=\"8302\"]\'); ?> 但当我试着加载它时。load(),它只是输出字符串,不运行php。因此,环顾四周,似乎有几个帖子与此相关:call shortcode in javascriptWhy might a plugin's 'do_shortcode