2017-03-09 117 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

相关问题