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

本代码实现Wordpress多站点模式下主站获取子站点最新文章列表信息,包含文章缩略图、所属分类、发布日期、阅读数等。

在当前主题的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,4);
    $sgnum = 1;
    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>';
        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);
                }
                //直接获取文章内第1张图片,但不是缩略图,不建议采用该方法
                //preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $vs->post_content, $matches, PREG_PATTERN_ORDER);
            }
            $count    += 1;
            //文章缩略图、文章标题和链接
            echo '<li><img class="thumbnail" src="'.$thumbnail.'"><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);
}

前端样式请自行设计。

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

赞 (0) 打赏

精彩评论

7+1=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏