0

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

<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>
4

1 回答 1

0

编辑2:

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

将此代码放在主站点的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

/* 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
}

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

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

编辑1:

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

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

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


您需要切换到相关博客,使用switch_to_blog($blog_id)功能,$blog_id相关博客(子站点)的 ID 在哪里。

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

一旦你完成了,只需打电话restore_current_blog()就可以了。

于 2012-11-19T10:12:36.377 回答