0

オプションの柔軟性が非常に高いため、高度な抜粋プラグインを使用しています。

プラグインに欠点が見つかった場合でも、標準の抜粋関数を削除する必要がremove_all_filters('the_excerpt');あります。そうしないと、高度な抜粋オプションと競合します。

問題

Facebookのオープングラフメタで頭に例外を追加したいと思います。通常、私はこれを単純なget_the_excerpt();方法で行い、これを次のようにメタタグに織り込みます...

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


しかし、これは機能しません-抜粋フィルターを削除したためです。


直し方?

以下の私の試みは、現在のIDを取得し、事前エキスパートコールを配置できるミニループを作成するWPクエリを作成します。

しかし、何らかの理由でこのループは私のサイトを壊し、私は修正できないようです。誰かが私がこのループを修正するのを手伝ってくれますか?よろしくお願いします。


<?php if ( is_single() || is_page() ) {

    $postID         = get_the_ID();
    $fbexcerpt      = new WP_Query(array( 'p' => $postID ));

    while ($fbexcerpt->have_posts()) :
    $fbexcerpt->the_post();

    echo '<meta property="og:description" content="' . the_advanced_excerpt('length=120&use_words=0') . '" />';

    unset($fbexcerpt);
    endif; wp_reset_query();

} ?>



アップデート

誰かが「高度な抜粋」を必要としない代替案を持っている場合-次に、functions.phpを介してオープングラフメタを挿入しています

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 );
4

1 に答える 1

0

単にget_postを使用しないのはなぜですか?

 $my_post = get_post($id);
 $excerpt = $my_post->post_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;
  }
于 2012-04-30T11:52:40.713 に答える