4

I use the following code to fetch the links of the previous and next posts respectively, from the single post template;

<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>

<?php echo get_permalink(get_adjacent_post(false,'',true)); ?>

My question is, please, if there are certain posts I'd like these codes to skip, and simply go to the ones right after, could I do that somehow using custom fields, or otherwise how can I make Wordpress skip a certain link when it comes up and fetch the next adjacent one without first going to the one I'd like to skip and then redirect or something, but rather echo the correct one right away..?

Thank you very much!

Alex

4

1 に答える 1

6

これにはさまざまな方法でアプローチできます。最も簡単な解決策は、おそらく「除外カテゴリ」(2 番目のパラメータ) を使用することです。たとえば、用語 ID 5 のカテゴリから投稿を除外します。

get_adjacent_post( false, '5', false )

もう 1 つのオプションは、フィルタget_previous_post_whereget_next_post_whereフィルタを使用して SQL クエリを変更することです。

オプション テーブルに、除外する投稿 ID の配列を格納できます。すべてのスティッキー投稿を除外する方法の例を次に示します。

add_filter( 'get_previous_post_where', 'so16495117_mod_adjacent' );
add_filter( 'get_next_post_where', 'so16495117_mod_adjacent' );
function so16495117_mod_adjacent( $where ) {
    return $where . ' AND p.ID NOT IN (' . implode( ',', get_option( 'sticky_posts' ) ) . ' )';
}

または、Q で提案したように、特定の投稿メタ キーを持つ投稿を除外することもできますmy_field

add_filter( 'get_previous_post_where', 'so16495117_mod_adjacent_bis' );
add_filter( 'get_next_post_where', 'so16495117_mod_adjacent_bis' );
function so16495117_mod_adjacent_bis( $where ) {
    global $wpdb;
    return $where . " AND p.ID NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE ($wpdb->postmeta.post_id = p.ID ) AND $wpdb->postmeta.meta_key = 'my_field' )";
}
于 2013-05-11T14:04:05.537 に答える