2014/10/25

WordPressの関連記事表示プラグイン、YARPPのクエリをカスタマイズする

YARPP(Yet Another Related Posts Plugin)、便利なので利用している方、多いんじゃないかなあと思います。このサイトでも関連記事表示に使わせてもらっています。さて、このプラグイン、かなり細かい設定が管理画面で可能なのですが、それでも、足りないという時に、クエリをカスタマイズできることを知りました。

WordPress › Yet Another Related Posts Plugin (YARPP) « WordPress Plugins

FAQの「Can I customize how YARPP displays?」の項に、クエリのカスタマイズの方法が載っています。これは便利!

yarpp_related(array(
    // Pool options: these determine the "pool" of entities which are considered
    'post_type' => array('post', 'page', ...),
    'show_pass_post' => false, // show password-protected posts
    'past_only' => false, // show only posts which were published before the reference post
    'exclude' => array(), // a list of term_taxonomy_ids. entities with any of these terms will be excluded from consideration.
    'recent' => false, // to limit to entries published recently, set to something like '15 day', '20 week', or '12 month'.
    // Relatedness options: these determine how "relatedness" is computed
    // Weights are used to construct the "match score" between candidates and the reference post
    'weight' => array(
        'body' => 1,
        'title' => 2, // larger weights mean this criteria will be weighted more heavily
        'tax' => array(
            'post_tag' => 1,
            ... // put any taxonomies you want to consider here with their weights
        )
    ),
    // Specify taxonomies and a number here to require that a certain number be shared:
    'require_tax' => array(
        'post_tag' => 1 // for example, this requires all results to have at least one 'post_tag' in common.
    ),
    // The threshold which must be met by the "match score"
    'threshold' => 5,

    // Display options:
    'template' => , // either the name of a file in your active theme or the boolean false to use the builtin template
    'limit' => 5, // maximum number of results
    'order' => 'score DESC'
),
$reference_ID, // second argument: (optional) the post ID. If not included, it will use the current post.
true); // third argument: (optional) true to echo the HTML block; false to return it

これで例えば複数のカスタム投稿タイプがあるサイトでも、AとBのカスタム投稿タイプは関連づけるけど、Cのカスタム投稿タイプは関連づけない、みたいなのをテンプレート側でコントロールできることになります。いやあもっとわかりやすいところに書いといてくれ。

関連記事表示というのは良い文化ですね。僕自身、たまに、ああこんなの書いてたなと見つけることもあり。

加藤 康祐 / 企画・設計

プランナー、デザイナー。加藤康祐企画設計代表。Webデザインを入り口に、2005年よりフリーランスとしてのキャリアスタート。主な仕事としてベンチャー企業でのサービスのUXデザイン、独法との防災メディアの運営、社会的養護の子どもたちの自立を支援するNPOのサポート。ラグビーと料理、最近イラスト。

加藤康祐企画設計

是非、フォローしてください!
Twitter / Instagram

(2012-10-5)
売り上げランキング: 14,705
100円