doumengwei0138 2017-03-09 19:45
浏览 19
已采纳

带有自定义分类术语的get_next_post

I have a custom taxonomy called visitors_location, with the terms NL and INT. I have pages tagged that are tagged with either 1 or both these terms.
A visitor get a value NL or INT depending on his IP. When the visitor from eg. 'NL' sees a gird overview of pages with the taxonomy visitors_location he will only see the 'NL' pages. Up to here no problem.

But when he goes to a detail page the next page has to be tagged with NL as well. This code doesn't do the trick: get_next_post(true, '', 'visitors_location') because when a page is tagged with both 'NL' and 'INT' then, if the next page is only tagged with INT, it will be shows as well. So basically I need to be able to only include pages in get_next_post() that have an NL tag in this case. Any suggestion?

  • 写回答

1条回答 默认 最新

  • douce1368 2017-03-10 10:42
    关注

    You can try this:

    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;
    }
    

    Make sure to replace 'NL' with a dynamic value that is in your case either 'NL' or 'INT'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路