当执行我的短代码时,它会插入一个<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标记的外观。
所以,我需要弄清楚这些流氓段落是从哪里来的。如有任何见解,将不胜感激。谢谢
最合适的回答,由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\');