最新文章函数不显示指定分类

启用了Video分类,但是不希望这个分类的文章在“最新文章”中显示,怎么办呢?只能过滤分类了。可是现在的WordPress数据库比较奇特,在文章的wp_posts属性里面category已经被全部指定为0 :mrgreen: (汗一个,那文章分类在哪里呢?)原来在现在的WordPress中引用了term_taxonomy(下简称tt)、term_relationships(下简称tr)来确定分类关系~~自己还是不大明白为啥要用那么复杂的办法捏 :question:

怎么排除这个分类的文章呢?参考了“相关日志”这个插件的一些代码,不过作者的思路比较高深,我这种随便玩玩php的人自然很难看懂,所以就用了一些比较笨的办法来实现相关的排除分类。这里就摘取了相关的一段代码:p.s.排除的分类ID修改变量$ex_id即可

//Recent Posts
function get_recent_posts($no_posts = 10, $before = '
  • ', $after = '
  • ', $show_pass_post = false, $skip_posts = 0, $ex_id = '47') { global $wpdb, $post, $table_prefix; $exclude = explode(",",$ex_id); $q = "SELECT tt.term_id, tr.object_id FROM ". $table_prefix ."term_taxonomy tt, " . $table_prefix . "term_relationships tr WHERE tt.taxonomy = 'category' AND tt.term_taxonomy_id = tr.term_taxonomy_id"; $cats = $wpdb->get_results($q); $cnt = 0; foreach($cats as $cat) { if (in_array($cat->term_id, $exclude) == true){ $ex_post_id[$cnt] = $cat->object_id; $cnt++; } } $ex_post_out = 'p.ID !='; for ($i = 0; $i <= $cnt-2; $i++) { $ex_post_out = $ex_post_out . $ex_post_id[$i] . ' AND p.ID !='; } $ex_post_out .= $ex_post_id[$cnt-1]; $request = "SELECT p.ID, p.post_title, p.post_date, p.post_content FROM $wpdb->posts p WHERE $ex_post_out AND p.post_status = 'publish' AND p.post_type = 'post'"; if(!$show_pass_post) { $request .= "AND post_password ='' "; } $request .= "ORDER BY post_date DESC LIMIT $skip_posts, $no_posts"; $posts = $wpdb->get_results($request); $output = ''; ...

    大概原来就是先从表中确定是category的项目,然后再对照tt.term_taxonomy_id = tr.term_taxonomy_id的项目,即把分类和文章ID挂上勾,然后再查找所要排除的分类的ID,再从中筛选出来该分类(或几个分类)的文章ID生成数组ex_post_id,再用一点儿字符串链接的方法结合成字符串p.ID != xxx以便在查询最新文章的时候排除这个几个ID,然后就大功告成了~~~研究这个真的花了不少时间,首先mysql的查询就比较头大,然后php数组问题也卡了一下(原来数组赋值和pascal一样,只不过不用事先声明数组),基本就这样咯~~

    Leave a Reply

    :wink: :twisted: :surprised: :smile: :sad: :rolleyes: :redface: :razz: :question: :neutral: :mrgreen: :mad: :lol: :idea: :exclaim: :evil: :eek: :cry: :cool: :confused: :biggrin: :arrow: