如何从子域博客显示主域中的最新帖子 [英] How to show recent post in the main domain from subdomain blog

查看:22
本文介绍了如何从子域博客显示主域中的最新帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要显示从我的子域到我的主域前端的最新帖子.我正在使用下面的代码,但它只选择主域最近的帖子.获取子域最近的帖子有什么帮助吗?

need to show the recent post from my subdomain to my main domain frontend. I am using below code, but its picking only main domain recent post. any help to fetch subdomain recent post ?

<h2>Recent Posts</h2>
<ul>
<?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
?>
</ul>

推荐答案

EDIT 2:

事实证明,您并没有从同一个 WordPress 安装(也称为 WordPress 网络)中运行两个站点.这是我可以建议您在这种情况下使用的内容.

So it turns out, that you are not running both sites from the same WordPress installation(otherwise referred to as a WordPress Network). Here is what I can suggest that you use in this case.

将此代码放在主站点的functions.php中:

Put this code in the main site's functions.php:

/**
* this function retrieves the requested part of the main site
* ok, well basically can be from any site, depending on the $url param, as long as it has the proper function that will display the requested content
* @param $url - the url of the site
* @param $key - part of the name of the function that will display the content
* @param $add_qs - any additional query string that will be appended, use "&params=param1,param2,param3" to pass "param1", "param2" and "param3"
* to the loading function
*/
function get_main_site_part($url, $key, $add_qs = '') {

    // cache the result, so we don't make a request with each page load
    $cache = ABSPATH . 'wp-content/uploads/main_site_' . $key . '.txt';
    $cache_lifetime = 300;

    // just to make sure - try to remove the trailing slash in the $url
    $url = untrailingslashit($url);
    $uri = $url . '/?including_template_part=1&load_part=' . $key . $add_qs;

    # reload the cache on every 5 minutes
    if (!file_exists($cache) || time() - filemtime($cache) > $cache_lifetime) {
        $main_site_html = wp_remote_get($uri);
        if (is_a($main_site_html, 'WP_Error')) {
            //print_r($main_site_html);
            //exit('error! ');
            return;
        }

        $fp = fopen($cache, 'w');
        fwrite($fp, $main_site_html);
        fclose($fp);
    } else {
        $main_site_html = file_get_contents($cache);
    }

    return $main_site_html;
}

现在把这个函数放在你子域的functions.php中:

Now put this function in your sub-domain's functions.php:

/* HTML LOADING HOOK - For loading content from one site to another - best application in multisite */
function print_requested_template_part() {
    // Respond only to requests from the same address... 
    if ( $_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'] && $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['including_template_part']) && isset($_GET['load_part']) && $_GET['load_part'] != '' ) {
        $part = $_GET['load_part'];
        $func = 'render_' . str_replace('-', '_', $part); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
        if ( function_exists($func) ) {
            // Allow for passing parameters to the function
            if ( isset($_GET['params']) ) {
                $params = $_GET['params'];
                $params = ( strpos($params, ',') !== false )? explode(',', $params) : array($params);
                call_user_func_array($func, $params);
            } else {
                call_user_func($func);
            }
        }
        exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
    }
}
add_action('init', 'print_requested_template_part', 1);

function render_my_recent_posts( $numberposts = 5 ) { ?>
    <h2>Recent Posts</h2>
    <ul>
    <?php
        $args = array( 'numberposts' => '5' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ) {
            echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
        }
    ?>
    </ul><?php
}

然后在您的主站点中调用此函数,您希望在其中显示您最近的帖子:

Then in your main site call this function, where you want your recent posts to appear:

echo get_main_site_part( 'http://questions.admissiontimes.com/', 'my_recent_posts', '&params=5' )

编辑 1:

使用您在问题中提供的示例代码,结合我下面的解决方案,最终代码如下所示:

Using the sample code that you have in your question, combined with my solution from below, here is what the final code will look like:

<?php 
switch_to_blog( 2 ); // Switch to the blog that you want to pull posts from. You can see the ID when you edit a site through the Network Admin - the URL will look something like "http://example.com/wp-admin/network/site-info.php?id=2" - you need the value of "id", in this case "2" ?>
<h2>Recent Posts</h2>
<ul>
<?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ) {
        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
?>
</ul>
<?php restore_current_blog(); // Restore the current blog ?>

现在,您只需将该代码放在您拥有原始代码的任何地方,一切都应该正常工作.

Now, you just put that code wherever you had your original code and everything should be working properly.

您需要使用switch_to_blog($blog_id) 函数,其中 $blog_id 是相关博客(子站点)的 ID.

You need to switch to the blog in question, using the switch_to_blog($blog_id) function, where $blog_id is the ID of the blog(sub-site) in question.

然后执行正常的 get_posts/或等效/功能并以您想要的方式显示/存储帖子.

Then do your normal get_posts/or equivalent/ function and display/store the posts in the way you want.

完成后,只需调用 restore_current_blog() 就是这样.

Once you're done with that, just call restore_current_blog() and that's it.

这篇关于如何从子域博客显示主域中的最新帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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