WordPress主站获取子站点最新文章信息

WordPress开启多站点后,很多站长都希望能在主站点首页显示子站文章。本文档以2个子站点为例,在首页并排显示子站点5篇最新文章,数据包含子站名称及其最新文章的缩略图、标题、分类名称、发布日期和浏览次数。

0x01 修改主题function.php,添加以下代码:

//主站首页调用子站点最新文章
function subsite_recent_post($showpost) {
    global $wpdb, $post, $table_prefix;
    $most_post = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY last_updated DESC");
    foreach ($most_post as $key => $v) {
        //排除主站点
        if($v !=1 ) {
            $b_id[$key] = $v;
        }
    }
    $newid = array_flip($b_id);
    //PHP数组函数随机显示,数值不能大于子站点数量
    $new_post_id = array_rand($newid,2);
    $sgnum = 1;
    //根修改css样式名,后续同样说明
    echo '<div class="most-comment-posts subsites">';
    foreach ( $new_post_id as $k => $vol ) {
        $blogPostsTable = $wpdb->base_prefix.$vol."_posts"; $recent_post = $wpdb->get_results("SELECT * from $blogPostsTable where post_status = 'publish' and post_type = 'post' ORDER BY post_date desc LIMIT 0,$showpost");
        //获取子站点名称和链接
        $blogname = get_blog_option($vol, 'blogname');
        $blogurl = get_blog_option($vol, 'home');
        echo '<div class="subsite'.$sgnum.'"><h3 class="title"><a href="'.$blogurl.'"><strong>'.$blogname.'</strong></a></h3>';
        echo '<ul class="newlist">';
        foreach ($recent_post as $ks => $vs) {
            //切换到子站点并获取文章和链接、发布日期、阅读数、分类和分类链接
            switch_to_blog($vol);
            $permalink = get_blog_permalink($vol, $vs->ID);
            $postdate = substr("$vs->post_date",5,2)."月".substr("$vs->post_date",8,2)."日";
            $postview = get_post_meta($vs->ID, 'views', true);
            $postcats = get_the_category($vs->ID);
            $catelink = get_category_link($postcats[0]->term_id);
            //若存在特色图像则使用特色图,若文章内也没有图片则使用该默认图像
            $thumbnail = get_stylesheet_directory_uri().'/images/thumbnail.png';
            if ( has_post_thumbnail($vs->ID) ) { 
                $thumbnail = the_post_thumbnail();
            } else {
                //先文章内第1张图片的ID,然后获取该图片的缩略图
                preg_match_all('/<img.+wp-image-(.*)["|\ ]/U', $vs->post_content, $matches);
                $first_img = $matches[1][0];
                if ( count($matches[1]) > 0 ) {
                    $thumbnail = wp_get_attachment_thumb_url($first_img);
                }
            }
            $count += 1;
            //文章缩略图、文章标题和链接
            echo '<li><img class="thumbnail" src="'.$thumbnail.'" alt="'.$vs->post_title.'"><a href="'.$permalink.'">'.$vs->post_title.'</a>';
            //文章分类名称和链接、发布日期、阅读数
            echo '<p class="text-muted views"><a href="'.$catelink.'">'.$postcats[0]->cat_name.'</a><span class="time">'.$postdate.'</span><span class="post-views">阅读('.$postview.')</span></p></li>';
        }
        echo '</ul></div>';
        $sgnum += 1;
    }
    echo '</div>';
    //使程序回到主站点
    switch_to_blog(1);
}

0x02 修改主题css样式(style.css),添加以下代码:

/* 主站显示子站最新文章,需根据实际情况进行调整 */
.subsites {overflow:hidden;}
.subsite1, .subsite3, .subsite2, .subsite4 {width:49%; float:left;}
.subsite1, .subsite3 {margin-right:1%;}
.subsite2, .subsite4 {margin-left:1%;}
.subsites .newlist {padding-top:10px;}
.subsites .newlist li {height:55px; padding:8px 0; white-space:nowrap; text-overflow:ellipsis;}
.subsites .newlist p {float:none;}
.subsites .newlist .thumbnail {float:left; width:52px; height:39px; margin-right:6px;}
.subsites .newlist .time {margin:auto 12px;}
@media (max-width:560px), (min-width:1025px) and (max-width:1150px) {
    .subsite1, .subsite3, .subsite2, .subsite4 {width:100%; margin:auto 0 auto 0;}
    .subsites .newlist li {padding-left:0px;}
    .subsites .newlist p {display:block;}
}
@media (max-width:560px) {
    .subsites .newlist li {height:45px; padding:3px 0;}
}

0x03 修改主题模板文件(index.php),在希望展示子站文章的位置添加以下代码:

//每个主题稍有不同,请根据实际情况修改,以仅在主站首页展示为例
if ( !$paged && $current_blog->blog_id == 1 ) {
    subsite_recent_post(5);
}

原创文章禁止转载:技术学堂 » WordPress主站获取子站点最新文章信息

精彩评论

9+5=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏