多站点网络中所有页面的分层页面列表

时间:2013-09-15 作者:Alex V.

我想创建一个页面模板,该模板列出多站点网络中所有博客的所有页面,并以网站的博客名称开始列出这些页面,然后列出该网站中的页面。到目前为止还不错。

这篇文章让我有了99%的成功:How to list all network sites on one page

我需要帮助的最后一部分是按blogname对输出进行排序。我试着按路径排序,但似乎不起作用。

以下是我目前掌握的情况:

$blogs = $wpdb->get_results(
"SELECT blog_id,path FROM {$wpdb->blogs}
WHERE blog_id != {$wpdb->blogid}
AND site_id = \'{$wpdb->siteid}\'
AND spam = \'0\'
AND deleted = \'0\'
AND archived = \'0\'
order by blog_id", ARRAY_A
);
*/

// get all blogs
$blogs = get_blog_list( 0, \'all\' );

if ( 0 < count( $blogs ) ) :
foreach( $blogs as $blog ) :
    switch_to_blog( $blog[ \'blog_id\' ] );

    if ( get_theme_mod( \'show_in_home\', \'on\' ) !== \'on\' ) {
        continue;
    }

    $description  = get_bloginfo( \'description\' );
    $blog_details = get_blog_details( $blog[ \'blog_id\' ] );
    ?>
    <li class="no-mp">

        <h2 class="no-mp blog_title">
            <a href="<?php echo $blog_details->path ?>">
                <?php echo  $blog_details->blogname; ?>
            </a>
        </h2>

        <div class="blog_description">
           <!-- <?php echo $description; ?> -->
        </div>
        <?php
          $args = array(
            \'depth\'        => 4,
            \'child_of\'     => \'\',
            \'title_li\'     => \'\',
            \'echo\'         => 1,
            \'sort_column\'  => \'menu_order, post_title\',
            \'post_type\'    => \'page\',
            \'post_status\'  => \'publish\'
          );
        ?>
        <?php wp_list_pages( $args ); ?>
    </li>
<?php endforeach;
endif; ?>
</ul>

1 个回复
SO网友:brasofilo

这可以通过以下方法实现get_blog_list, originally by Frank Bueltge, 并修改为还返回每个博客的名称和描述。Attention to the use 属于get_current_blog_id()switch_to_blog().

/**
 * 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;
}
调用函数并使用sorting function. 一个简单的例子:

$blogs = b5f_get_blog_list( 0, \'all\', true );
uasort( $blogs, function( $a, $b ) {
    return strcasecmp( $a[\'name\'], $b[\'name\'] );
});
$current_blog_id = get_current_blog_id();
foreach( $blogs as $blog ) :
    switch_to_blog( $blog[ \'blog_id\' ] );
    echo $blog[\'name\'] . \' - \' . $blog[\'domain\'] . \' - \' . $blog[\'desc\'];
endforeach;
switch_to_blog( $current_blog_id );

结束

相关推荐

在WordPress MultiSite上将网络管理仪表板强制为1列

我想知道是否有人知道如何在网络管理仪表板的wordpress多站点安装中强制dasboard布局为1列这是解决方案的一部分,但并不强制列数为1:function so_screen_layout_columns( $columns ) { $columns[\'dashboard-network\'] = 1; return $columns; } add_filter( \'screen_layout_columns\', \'so_screen_layo