码迷,mamicode.com
首页 > 其他好文 > 详细

黄聪:WordPress 多站点建站教程(六):使用WP_Query、switch_to_blog函数实现获取子站点分类中的文章

时间:2014-05-10 18:35:44      阅读:481      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

首先在你使用主题的funtions.php里面添加下代码:

bubuko.com,布布扣
//根据时间显示最新的分类文章内容,每个站点显示一篇内容
//$blog_id   子站点ID
//$catid   分类ID

wp_reset_query();
switch_to_blog($blog_id);
global $post;?>
$my_query2 = new WP_Query(‘showposts=1&order=desc&orderby=date&cat=‘.$catid);
while ($my_query2->have_posts()) : $my_query2->the_post();
bubuko.com,布布扣

 

接下来获取分类ID号,使用方法是

 

bubuko.com,布布扣
global $wpdb;

$most_viewed_cat = $wpdb->get_results("SELECT * from $wpdb->blogs");
foreach ($most_viewed_cat as $keyc => $vc)
{
    if($vc->blog_id !=1)  //排除主站点ID
    {
        $id_cat[$keyc] = $vc->blog_id;
    }
}
$newid = array_flip($id_cat);
$new_array_cat = array_rand($newid,10);
$n_post = array_filter($new_array_cat);
foreach ($n_post as $ksc => $volsc)
{
     $most_cat = $wpdb->get_results("SELECT * from wp_".$volsc."_terms where name = ‘最新博文‘");
     foreach ($most_cat as $ks => $vs)
     {
         get_cat_blogposts_wpmu($volsc,$vs->term_id); //方法调用
     }
 }
bubuko.com,布布扣

 

 

黄聪:WordPress 多站点建站教程(六):使用WP_Query、switch_to_blog函数实现获取子站点分类中的文章,布布扣,bubuko.com

黄聪:WordPress 多站点建站教程(六):使用WP_Query、switch_to_blog函数实现获取子站点分类中的文章

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/huangcong/p/3719996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!