2017-03-09 86 views
0

我有一個名爲visitors_location的自定義分類,術語爲NL和INT。 我有頁面已標記,標記爲1或兩個這些術語。
根據訪問者的IP,訪問者獲得值NL或INT。當來自例如'NL'可以看到具有分類標籤visitor_location的網頁概覽,他只會看到'NL'頁面。到這裏沒問題。帶自定義分類術語的get_next_post

但是當他進入一個詳細頁面時,下一頁也必須用NL標記。 這段代碼並沒有訣竅: get_next_post(true, '', 'visitors_location') 因爲當一個頁面同時被標記爲'NL'和'INT'時,如果下一個頁面只被標記爲INT,它也會被顯示。 所以基本上我需要能夠在這種情況下只包含get_next_post()中具有NL標籤的頁面。 有什麼建議嗎?

回答

1

你可以試試這個:

add_filter('get_next_post_where', 'fix_adjacent_post_where', 10, 5); 
add_filter('get_previous_post_where', 'fix_adjacent_post_where', 10, 5); 

function fix_adjacent_post_where($where, $in_same_term, $excluded_terms, $taxonomy, $post) 
{ 
    global $wpdb; 
    if ($in_same_term === true && $taxonomy === 'visitors_location') 
    { 
     $where .= " AND ID IN (
         SELECT p.ID FROM {$wpdb->posts} AS p 
         JOIN {$wpdb->term_relationships} AS tr ON tr.object_id=p.ID 
         JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id 
         JOIN {$wpdb->terms} AS t ON t.term_id = tt.term_id 
         WHERE t.name='NL')"; 
    } 
    return $where; 
} 

確保一個動態值,你的情況是「NL」或「INT」

+0

的工作就像一個魅力,以取代「NL」!謝謝Eveline! – SiteHopper

相關問題