[WP]YARPP の関連記事でTypes のカスタム投稿も対象にする方法
スポンサード
※上の記述はTypes プラグインを使っている場合に限ります。
カスタム投稿の設定を直接functions.php に書いている場合は、以下のプラグイン公式サイトにも記述がある通り、
スポンサード
「Yet Another Related Posts Plugin (YARPP)」プラグインで、通常の投稿や固定ページ以外の「カスタム投稿」にも関連記事を表示する方法のご紹介です。
※今回は、Types で作成したカスタム投稿を前提にしております。
目次
Yet Another Related Posts Plugin (YARPP)プラグインのインストール
管理画面のプラグイン新規追加より YARPP を検索するか、以下のページよりプラグインファイルをダウンロードします。Types のカスタム投稿もYARPP 関連記事の対象にする
ご利用のテーマのfunctions.php を編集して、以下のコードを追記します。add_filter('wpcf_type', 'yarpp_support_func', 10, 2); function yarpp_support_func($data, $post_type) { if(in_array($post_type, array( 'ポストタイプ名', ))) { $data['yarpp_support'] = true; } return $data; }「ポストタイプ名」の部分を、ご利用の環境に合わせて変更してください。
※上の記述はTypes プラグインを使っている場合に限ります。
カスタム投稿の設定を直接functions.php に書いている場合は、以下のプラグイン公式サイトにも記述がある通り、
Does YARPP support custom post types?
Yes. To make YARPP support your custom post type, the attribute yarpp_support must be set to true on the custom post type when it is registered. It will then be available on options on the YARPP settings page.
'yarpp_support' => true
If you would like to programmatically control which post types are considered in an automatically-displayed related posts display, use the yarpp_map_post_types filter.
Yet Another Related Posts Plugin (YARPP) — WordPress Plugins
functions.php 内で'yarpp_support' => true
を加えてあげるといけるようです。
YARPP の関連記事を出力
ご利用のテーマのsingle.php などを開いて、関連記事を出力したい箇所に以下の記述を追加します。<?php yarpp_related(array( 'post_type' => 'ポストタイプ名' ));?>
<?php related_posts(); ?>
によるYARPP テンプレートの呼び出しが出来なかったので、上記のようなYARPP のクエリをカスタマイズしてあげることで、カスタム投稿の関連記事が表示されるようになりました。スポンサード
コメント