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'

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

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝