我尝试了一些方法来删除<br>
在短代码之间创建时,我在另一个线程中发现了一个,看起来很有希望,但没有成功:
function the_content_filter($content) {
$block = join("|",array("category_product_list", "category_products"));
$rep = preg_replace("/(<p>)?\\[($block)(\\s[^\\]]+)?\\](<\\/p>|<br \\/>)?/","[$2$3]",$content);
$rep = preg_replace("/(<p>)?\\[\\/($block)](<\\/p>|<br \\/>)?/","[/$2]",$rep);
return $rep;
}
add_filter("the_content", "the_content_filter");
我读到wpautop可以做到这一点,但对我来说什么都不管用。我是否需要以某种方式修改我的实际短代码?
下面是我正在使用的一个短代码:
function category_product_list( $atts ) {
extract(shortcode_atts(array(
\'category\' => \'\',
\'per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'asc\'
), $atts));
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array( esc_attr($category) ),
\'operator\' => \'IN\',
);
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => 1,
\'posts_per_page\' => $per_page,
\'orderby\' => $orderby,
\'order\' => $order,
\'meta_query\' => $meta_query,
\'tax_query\' => $tax_query
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) :
$term_list = wp_get_post_terms($products->post->ID, \'product_cat\', array("fields" => "all"));
$first_term = $term_list[0];
$first_term_name = $first_term->name;
$url = get_permalink();
$id = $products->post->ID;
// $html_out = \'<h2>Courses</h2>\';
$html_out = \'<ul class="simple-sitemap-courses">\';
$html_out .= \'<li class="page-item page-item-\' . $id . \'"><a href="\' . $url . \'">\' . $first_term_name . \'</a>\';
$html_out .= \'<ul class="children">\';
while ($products->have_posts()) :
$products->the_post();
$product = get_product( $products->post->ID );
$course_title = get_the_title();
$url = get_permalink();
$id = $products->post->ID;
// Output product information here
$html_out .= \'<li class="page-item page-item-\' . $id . \'"><a href="\' . $url . \'">\' . $course_title . \'</a></li>\';
endwhile;
$html_out .= \'</ul>\';
$html_out .= \'</li>\';
$html_out .= \'</ul>\';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_postdata();
return $html_out;
}
add_shortcode( \'category_product_list\', \'category_product_list\' );
SO网友:LWS-Mo
我创建了一个简单的短代码,并将其添加到如下页面中两次:
[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]
如您所见,我添加了换行符(
<br />
) 在第一个短代码之后。因此,在前端也显示如下:
something
else
在HTML中:
<span>something</span>
<br />
<span>else</span>
我还用了
the_content
过滤器,但带有
strtr()
像这样:
function remove_br_from_shortcodes($content){
// look for the string "]<br />" and replace it just with "]"
$replace = array (
\']<br />\' => \']\'
);
$content = strtr($content, $replace);
return $content;
}
add_filter(\'the_content\', \'remove_br_from_shortcodes\');
刷新页面后,前端现在显示:
something else
在HTML中:
<span>something</span>
<span>else</span>
作为的名称
the_content
过滤器已经表明,它是对正常帖子/页面内容的过滤器,而不是文本小部件或摘录。
如果还要删除段落(<p>
) 请尝试将此添加到$replace
数组:(注意逗号!)
\'<p>[\' => \'[\',
\']</p>\' => \']\',
使用PHP
strtr()
函数,可以替换字符串中的某些字符。
More to read here.