0

get_the_contentWordpress のメソッドにフックする適切な方法を探しています。Wordpress は と のフックを提供しますがthe_content、 は提供the_excerpt()get_the_excerpt()ませんget_the_content()。コンテンツ投稿に HTML を追加および追加するプラグインを維持しています。the_excerpt()とに同じものを使用しましたget_the_excerpt()get_the_content()ほとんどのテーマが現在使用しているのと同じようにサポートするにはどうすればよいget_the_content()ですか? つまり、テーマがget_the_content()の代わりに使用されている場合the_content()、プラグインは失敗します。

私のプラグインコードは次のとおりです。

add_action('init', 'my_init');

function my_init() {
  add_filter('the_content', 'my_method', 12);
  add_filter('get_the_content', 'my_method', 12);
}


function my_method($content) {
  $content = "blah blah".$content."blah blah";

  return $content;
}

上記のコード (私のプラグイン内) はthe_content call、テーマからリッスンし、独自のコンテンツを記事に追加します。

これがテーマファイルのメソッドであると仮定します:

<div class="entry-content">
  <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->

上記の場合は正常に動作します。

しかし、テーマ コードがこのようなものである場合 ( the_content() からget_the_content()).

<div class="entry-content">
  <?php echo get_the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->

にフックが定義されていないため、プラグインが失敗しますget_the_content。私はテーマ コードを管理していないため、Wordpress がドキュメントで説明している解決策は役に立ちません。

<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>

更新: わかりやすくするために問題定義を拡張しました。

4

3 に答える 3

2

次のことを試しましたか?

コーデックスによると:

コンテンツをフィルタリングするプラグイン (add_filter('the_content')) を使用する場合、次の方法で (apply_filters を使用して) 呼び出さない限り、これはフィルターを適用しません。

<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>

wordpress codexでここに文書化されています。

編集済み

出力バッファリングを使用the_contentして変数を入れてみてください。

そうすれば気にしなくていいget_the_content

ob_start();
the_content();
$newContent = ob_get_clean();

これで、操作しやすいように変数 easy に格納され$newContentます。

于 2013-04-12T12:13:30.207 に答える
0

以下を試してみてください:

Cform の例:

add_filter('get_the_content', 'cforms_insert',10);

ご参考まで

(また)

$content = get_the_content();
$content = apply_filters('the_content', $content);
$panels = explode("[newpage]", $content);

これはあなたの問題を解決するのに役立つかもしれないと思います。

于 2013-04-12T12:18:31.750 に答える