繁体   English   中英

使用自定义分类术语的get_next_post

[英]get_next_post with custom taxonomy term

我有一个名为Visitorslocation的自定义分类法,名称为NL和INT。 我有标记过的页面被这些1个或两个标记。
访客根据自己的IP获得NL或INT值。 当访客来自 “ NL”看到的页面分类法带有“ visitors_location”分类,他只会看到“ NL”页面。 到这里没问题。

但是当他转到详细页面时,下一页也必须标记为NL。 这段代码并不能解决问题: get_next_post(true, '', 'visitors_location')因为当页面同时被标记为'NL'和'INT'时,如果下一页仅被标记为INT,它将被显示。 因此,基本上,在这种情况下,我仅需要能够将包含NL标签的页面包含在get_next_post()中。 有什么建议吗?

您可以尝试以下方法:

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”替换为您所用的动态值“ NL”或“ INT”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM