Wordpress 在head中创建mini-loop以使用高级摘录-loop来使用当前帖子ID

Wordpress 在head中创建mini-loop以使用高级摘录-loop来使用当前帖子ID,wordpress,Wordpress,我使用插件是因为它的选项有更多的灵活性 虽然如果发现插件有缺陷,我必须使用remove_all_过滤器(“the_摘录”)删除标准摘录函数其他方面它与高级摘录选项冲突 问题 我想在facebook的一个开放式元图中将“除了”添加到我的头部。通常我会用一个简单的get_the_extract()并将其编织到元标记中,如下所示 <meta property="og:description" content="<?php echo get_the_excerpt(); ?>" /&g

我使用插件是因为它的选项有更多的灵活性

虽然如果发现插件有缺陷,我必须使用
remove_all_过滤器(“the_摘录”)删除标准摘录函数其他方面它与高级摘录选项冲突

问题

我想在facebook的一个开放式元图中将“除了”添加到我的头部。通常我会用一个简单的
get_the_extract()并将其编织到元标记中,如下所示

<meta property="og:description" content="<?php echo get_the_excerpt(); ?>" />
为什么不直接使用

然后使用如下函数检索最多x个字符的单词:

  function extractText150char($text, $length = '150') {
    $textArr = explode(' ', $text);
    $text = '';

    // Loop through each word
    for ( $i=0; $i<count($textArr)-1; $i++ ) {
      $text .= $textArr[$i] . ' ';
      // Check string length
      if ( (strlen($text) + strlen($textArr[$i+1]) ) > $length )
        return $text;
    }
    return $text;
  }
函数extractText150char($text,$length='150'){
$textArr=爆炸(“”,$text);
$text='';
//循环浏览每个单词
对于($i=0;$i$length)
返回$text;
}
返回$text;
}

谢谢@Steven提出你的想法,但我无法得到它,也无法归还任何东西。假设我用你的想法作为函数。。。但它什么也不返回函数insert_fb_in_head(){global$post;if(!is_singular())返回;$my_post=get_post($id);$extract=$my_post->post_extract;echo'';echo“\n”}添加操作('wp_head','insert_fb_in_head 5)`如果执行
var\u dump($my\u post)
,是否会得到任何输出?如果是,如果执行
echo$my_post->post_extract
,是否会得到任何输出?如果是,则
extractText150char()
应该可以工作。如果没有,只需修改它以返回字符串的长度。
function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) 
        return;

        echo '<meta property="og:description" content=" . $MyExcerptHere . "/>';

    echo "\n";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
 $my_post = get_post($id);
 $excerpt = $my_post->post_excerpt;
  function extractText150char($text, $length = '150') {
    $textArr = explode(' ', $text);
    $text = '';

    // Loop through each word
    for ( $i=0; $i<count($textArr)-1; $i++ ) {
      $text .= $textArr[$i] . ' ';
      // Check string length
      if ( (strlen($text) + strlen($textArr[$i+1]) ) > $length )
        return $text;
    }
    return $text;
  }