简体   繁体   English

如何防止短代码在描述中显示

[英]How to keep shortcodes from displaying in description

Using this code 使用此代码

    <?php
$post = $wp_query->post;
$descrip = strip_tags($post->post_content);
$descrip_more = '';
if (strlen($descrip) > 155) {
$descrip = substr($descrip,0,155);
$descrip_more = ' ...';
}
$descrip = str_replace('"', '', $descrip);
$descrip = str_replace("'", '', $descrip);
$descripwords = preg_split('/[\n\r\t ]+/', $descrip, -1, PREG_SPLIT_NO_EMPTY);
array_pop($descripwords);
$descrip = implode(' ', $descripwords) . $descrip_more; 
echo '<meta name="description" content="'.$descrip.'">';
?>

Which outputs someething like this in my header: 在我的标题中输出的内容如下:

<meta name="description" content="[wptabs mode=horizontal background=false] [wptabtitle]     English Lyrics[/wptabtitle] [wptabcontent]Speed Circus Yeah A-yoWOO TAEWOON with the am I     ...">

I was wondering if I could add something to the code that would get rid of the shortcodes? 我想知道我是否可以在代码中添加一些可以摆脱短代码的东西? and perhaps anything in them? 也许还有什么呢?

Maybe out it like this? 也许这样吗?

<meta name="description" content="Speed Circus Yeah A-yoWOO TAEWOON with the am I     ...">

What need to do is render the post and not display the post contents. 需要做的是渲染帖子而不显示帖子内容。 There's a slight difference in here. 这里略有不同。

You should try using: 你应该尝试使用:

apply_filters('the_content', $post->post_content); 

Instead of: 代替:

$post->post_content

When you apply filters to the content you get the post as it is displayed to the user, while $post->post_content displays the raw content for the post. 当您对内容应用过滤器时,您会收到向用户显示的帖子,而$ post-> post_content会显示帖子的原始内容。

There's a function to remove shortcodes. 有一个删除短代码的功能。

https://codex.wordpress.org/Function_Reference/strip_shortcodes https://codex.wordpress.org/Function_Reference/strip_shortcodes

Put this just before the echo: 把它放在回声之前:

$descrip = strip_shortcodes( $descrip );

Given that you're outputting this in an attribute you should use the function for escaping attributes. 鉴于您要在属性中输出它,您应该使用该函数来转义属性。 esc_attr() . esc_attr() You can find out more about that here: https://codex.wordpress.org/Function_Reference/esc_attr 您可以在此处找到更多相关信息: https//codex.wordpress.org/Function_Reference/esc_attr

Complete example based on your code and comments left on my answer: 基于您的答案留下的代码和评论的完整示例:

 <?php
$post = $wp_query->post;
$descrip = strip_tags( $post->post_content );
$descrip_more = '';
if (strlen($descrip) > 155) {
    $descrip = substr($descrip,0,155);
    $descrip_more = ' ...';
}
$descrip = str_replace('"', '', $descrip);
$descrip = str_replace("'", '', $descrip);

$descripwords = preg_split('/[\n\r\t ]+/', $descrip, -1, PREG_SPLIT_NO_EMPTY);
array_pop($descripwords);
$descrip = implode(' ', $descripwords) . $descrip_more; 
$descrip = strip_shortcodes( $descrip );

echo '<meta name="description" content="' . esc_attr( get_the_title( $post->ID ) . trim( $descrip ) ) . '">';
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM