如何在 WordPress 多站点中显示最近的全局帖子

2024-02-24

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


首先,我们需要一个获取所有站点的函数 https://wordpress.stackexchange.com/a/114749/12615,有了这个,我们就可以迭代站点数组并使用以下命令提取信息wp_get_recent_posts() http://codex.wordpress.org/Function_Reference/wp_get_recent_posts(这是一个定制版本get_posts() http://codex.wordpress.org/Template_Tags/get_posts).

使用以下内容作为必须使用插件 http://codex.wordpress.org/Must_Use_Plugins,所以函数b5f_print_recent_posts()可在整个网络中使用:

<?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;
}

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

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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 WordPress 多站点中显示最近的全局帖子 的相关文章

  • 由于未定义符号,PECL solr 未加载:curl_easy_getinfo

    我正在尝试加载 PECL solr 扩展 我尝试使用 pecl install solr 并下载并使用 phpize configure make 来安装它 在这两种情况下 扩展安装时都没有错误 但在 apache 重新启动后 或在命令行上
  • 如何检查号码是否是巴基斯坦用户的手机号码而不是固定电话号码

    我所做的是从开头删除 92 或 0092 并使用以下代码检查它是否是巴基斯坦人的有效手机号码 if preg match 3 0 4 0 9 number 1 Pakistani mobile number else not a pakis
  • WordPress 网站上出现数据库错误“一个或多个数据库表不可用”

    Error One or more database tables are unavailable The database may need to be repaired 我怎么解决这个问题 wp config php 是正确的 我使用
  • PHP 文件上传帮助

    div align center div 这是我的代码
  • php表格:每行显示3个单元格[重复]

    这个问题在这里已经有答案了 我看这里 数组放入每行 5 个单元格的表格中 https stackoverflow com questions 9099568 array into a table with 5 cells in each r
  • 在会话 cookie 中存储大量数据会产生什么影响?

    谁能解释一下在会话中存储大量数据的缺点或给我指出一些阅读材料 我也很感兴趣在会话中存储数据和从数据文件读取数据之间是否有任何区别 如果您在会话中存储大量数据 则输入 输出性能会下降 因为会有大量读取 写入 默认情况下 PHP 中的会话存储在
  • 在 PHP 中将 CSV 写入不带括号的文件

    是否有本机函数或实体类 库用于将数组写入 CSV 文件中的一行而无需封装 fputcsv将默认为 如果没有为封装参数传入任何内容 谷歌让我失望了 返回一大堆有关的页面的结果 fputcsv PEAR 的库做的事情或多或少与fputcsv 工
  • 重复使用相同的卷曲手柄。性能大幅提升?

    在 PHP 脚本中 我对不同的 URL 执行了许多不同的curl GET 请求 一百个 将重复使用来自curl init提高性能 还是与请求的响应时间相比可以忽略不计 我这么问是因为在当前的架构中保持相同的句柄并不容易 交叉发布自我应该关闭
  • php 如何统计文件夹中的文件数量?

    我想让用户能够在自己的文件夹中上传一些文件 图片 但只有当该文件夹包含的图片少于五张时才可能 如果已经有 5 张图片 脚本必须让用户知道他 她的文件夹已满 所以 我想知道php中是否有函数可以计算文件夹中的文件数量 或者 php 中有其他方
  • 如何以编程方式获取 WooCommerce 中的所有产品?

    我想获取 WooCommerce 中的所有产品数据 产品 sku 名称 价格 库存数量 可用性等 我可以使用 wp query 来做到这一点吗 这样你就可以通过 wp query 获取所有产品 global wpdb all product
  • Composer 用于下载私有 GitHub 存储库

    我无法使用 Composer 下载 github 私人存储库 php composer phar update 我收到以下错误 The https api github com repos company private1 https ap
  • PHP 中的 Preg_replace

    我想替换 中包含的字符串中的内容content 它是多行等 preg replace 函数应该删除整个 com 没有垫子 蒙特 尝试这个 result preg replace s replacement content subject
  • Laravel 按动态 ID 数组对集合进行排序 [重复]

    这个问题在这里已经有答案了 我有以下 people array 5 2 9 6 11 people collection People find people 但当我倾倒并死去时 people collection集合按 ID ASC 排序
  • PHP7构造函数类名

    我有一个 Laravel 4 2 应用程序 它可以与 PHP5 一起使用 没有任何问题 由于我安装了一个运行 PHP7 的新 vagrant box 一旦我运行一个模型 其中函数名称与类名称 关系函数 相同 就会出现错误 如下所示
  • Android GCM 服务器的 API 密钥

    我有点困惑我应该为 GCM 服务器使用哪个 API 密钥 在文档中它说使用 android api 密钥 这对我不起作用并且总是给出未经授权的 http developer android com google gcm gs html ht
  • 使用PHP套接字发送和接收数据

    我正在尝试通过 PHP 套接字发送和接收数据 一切正常 但是当我尝试发送数据时 PHP 不发送任何内容 Wireshark 告诉我发送的数据长度为 0 我正在使用这段代码
  • PHP中如何识别服务器IP地址

    PHP中如何识别服务器IP地址 对于服务器 ip 来说是这样的 SERVER SERVER ADDR 这是港口的 SERVER SERVER PORT
  • 如何使用 Google 帐户对我们网站中的用户进行身份验证

    如何在我们的网站中使用 Google 帐户对用户进行身份验证 我希望用户重定向到谷歌登录页面 然后将他重定向到我的网站 我想要这个 PHP 实现 你要OAuth http code google com apis accounts docs
  • PHP cURL 在本地工作,在 AWS 服务器上出现错误 77

    最新更新 脚本作为管理员用户通过 SSH shell 作为 php script php 成功运行 当由 nginx 用户运行时 curl 命令无法执行 https 请求 所以我猜测这是nginx用户无法正确使用curl的问题 我已经检查了
  • PHP 和 NLP:嵌套括号(解析器输出)到数组?

    想要将带有嵌套括号的文本转换为嵌套数组 以下是 NLP 解析器的输出示例 TOP S NP PRP I VP VBP love NP NP DT a JJ big NN bed PP IN of NP NNS roses 原文 我喜欢一大床

随机推荐