如何在wordpress多站点中显示最近的全球帖子 [英] How to display recent global post in wordpress multisite

查看:24
本文介绍了如何在wordpress多站点中显示最近的全球帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ayp.no 上运行了一个 wordpress 多站点,我试图找出一种方法来显示所有子站点的徽标和所有博客的最新帖子,我知道有一个 wpmudev 高级插件,但我希望有是一些我可以自己做的编码(好吧,显然不是我自己,但至少在这里问问看).

I run a wordpress multisite on ayp.no and I am trying to figure out a way to present all subsites with logos and recent posts from all blogs, I know there is a wpmudev premium plugin for this, but I was hoping there was some coding i could do myself (well, obviously not myself, but at least ask here and see)..

推荐答案

首先,我们需要一个函数来获取所有站点,有了这个,我们迭代了一系列站点并使用 wp_get_recent_posts 提取信息()(这是get_posts()).

First, we need a function to get all sites, with that at hand we iterate thought the array of sites and pull the information with wp_get_recent_posts() (which is a customized version of get_posts()).

使用以下作为必须使用的插件,所以函数b5f_print_recent_posts() 可在整个网络中使用:

Use the following as a Must Use plugin, so the function b5f_print_recent_posts() is available throughout the network:

<?php
/**
 * Plugin Name: Recent Network Posts
 * Plugin URI: http://stackoverflow.com/q/23713801/1287812
 * Description: Creates a function that lists recent posts from all sites of the network. Call it in another plugins or themes.
 * Author: brasofilo   
 */

/**
 * Iterates throught all sites of the network and grab the recent posts
 */
function b5f_print_recent_posts()
{
    $blogs = b5f_get_blog_list( 0, 'all', true );
    $current_blog_id = get_current_blog_id();
    foreach( $blogs as $blog ) 
    {
        switch_to_blog( $blog[ 'blog_id' ] );
        echo '<h3>' . $blog['name'] . ' - ' . $blog['domain'] . ' - ' . $blog['desc'] . '</h3>';
        $posts = wp_get_recent_posts( array(), OBJECT );
        if( $posts )
        {
            foreach( $posts as $post )
            {
                printf(
                    '- <a href="%s">%s</a><br />',
                    get_permalink( $post->ID ),
                    $post->post_title
                );
            }
        }
    }
    switch_to_blog( $current_blog_id );
}

/**
 * Returns an array of arrays containing information about each public blog 
 * hosted on this WPMU install.
 * 
 * Only blogs marked as public and flagged as safe (mature flag off) are returned.
 *
 * @author Frank Bueltge
 * 
 * @param   Integer  The first blog to return in the array.
 * @param   Integer  The number of blogs to return in the array (thus the size of the array).
 *                   Setting this to string 'all' returns all blogs from $start
 * @param   Boolean  Get also Postcount for each blog, default is False for a better performance
 * @param   Integer  Time until expiration in seconds, default 86400s (1day)
 * @return  Array    Returns an array of arrays each representing a blog. 
 *                   Details are represented in the following format:
 *                       blog_id   (integer) ID of blog detailed.
 *                       domain    (string)  Domain used to access this blog.
 *                       path      (string)  Path used to access this blog.
 *                       postcount (integer) The number of posts in this blog.
 *                       name      (string) Blog name.
 *                       desc      (string) Blog description.
 */
function b5f_get_blog_list( $start = 0, $num = 10, $details = FALSE, $expires = 86400 ) {

    // get blog list from cache
    $blogs = get_site_transient( 'multisite_blog_list' );

    // For debugging purpose
    if ( defined( 'WP_DEBUG' ) && WP_DEBUG )
        $blogs = FALSE;

    if ( FALSE === $blogs ) {

        global $wpdb;

        // add limit for select
        if ( 'all' === $num )
            $limit = '';
        else
            $limit = "LIMIT $start, $num";

        $blogs = $wpdb->get_results(
            $wpdb->prepare( "
                SELECT blog_id, domain, path 
                FROM $wpdb->blogs
                WHERE site_id = %d 
                AND public = '1' 
                AND archived = '0' 
                AND mature = '0' 
                AND spam = '0' 
                AND deleted = '0' 
                ORDER BY registered ASC
                $limit
            ", $wpdb->siteid ), 
        ARRAY_A );

        // Set the Transient cache
        set_site_transient( 'multisite_blog_list', $blogs, $expires );
    }

    // only if usable, set via var
    if ( TRUE === $details ) {

        $blog_list = get_site_transient( 'multisite_blog_list_details' );

        // For debugging purpose
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG )
            $blog_list = FALSE;

        if ( FALSE === $blog_list ) {

            global $wpdb;
            $current_blog_id = get_current_blog_id();
            foreach ( (array) $blogs as $details ) {
                $blog_list[ $details['blog_id'] ] = $details;
                $blog_list[ $details['blog_id'] ]['postcount'] = $wpdb->get_var( "
                    SELECT COUNT(ID) 
                    FROM " . $wpdb->get_blog_prefix( $details['blog_id'] ). "posts 
                    WHERE post_status='publish' 
                    AND post_type='page'" 
                );
                switch_to_blog( $details['blog_id'] );
                $blog_list[ $details['blog_id'] ]['name'] = get_blog_details()->blogname;
                $blog_list[ $details['blog_id'] ]['desc'] = get_bloginfo( 'description' );
            }
            switch_to_blog( $current_blog_id );
            // Set the Transient cache
            set_site_transient( 'multisite_blog_list_details', $blog_list, $expires );
        }
        unset( $blogs );
        $blogs = $blog_list;
    }

    if ( FALSE === is_array( $blogs ) )
        return array();

    return $blogs;
}

您可以在之前的插件中添加以下网络仪表板小部件进行测试:

You can add the following network dashboard widget in the previous plugin to test it out:

add_action( 'wp_network_dashboard_setup', 'dashboard_setup_so_23713801' );

function dashboard_setup_so_23713801() 
{
    wp_add_dashboard_widget( 'widget_so_23713801', __( 'Test widget' ), 'print_widget_so_23713801' );
}

function print_widget_so_23713801() 
{
    b5f_print_recent_posts();
}

这篇关于如何在wordpress多站点中显示最近的全球帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆