如何在描述元标记中打印摘录

时间:2015-10-08 16:00:59

标签: wordpress

我只需要一种打印方式:

<meta property="og:description" content="This in the content" />

我使用过这段代码:

<meta property="og:description" content="<?php 

$tags = array("<p>", "</p>");
$myExcerpt = str_replace($tags, "", the_excerpt());

echo get_the_excerpt(); ?>" />

但它打印出来:

<meta property="og:description" content="<p>Content of my plugin. No related posts.</p>
This is the content." />

如果我使用此代码:

    <meta property="og:description" content="<?php    
    echo get_the_excerpt();  ?>" />

打印出来:

<meta property="og:description" content="<p>Content of my plugin. No related posts.</p>" />

如果我使用此代码:

<meta property="og:description" content="<?php 
$tags = array("<p>", "</p>");
$myExcerpt = str_replace($tags, "", the_excerpt());
echo $myExcerpt; ?>" />

打印出来:

<meta property="og:description" content="<p>Content of my plugin. No related posts.</p>" />

3 个答案:

答案 0 :(得分:2)

您应该使用get_the_excerpt()(返回摘录)而不是the_excerpt()(与摘录相呼应):

<meta property="og:description" content="<?php 

$tags = array("<p>", "</p>");
$myExcerpt = str_replace($tags, "", get_the_excerpt());

echo $myExcerpt; ?>" />

或者,您可以使用wp_strip_all_tags()删除所有标记来简化操作:

<meta property="og:description" 
      content="<?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?>" />

答案 1 :(得分:0)

ALL in ONE seo pack插件可以将内容插入元标记,但这并不意味着它成为页面/帖子的内容,描述或摘录。您需要回显var seo pack用于存储该信息的值。

答案 2 :(得分:0)

我已在多个网站上使用此代码段并且效果很好。

<meta property="og:description" content='<?php $myExcerpt = strip_tags(get_the_excerpt()); echo $myExcerpt; ?>' />