2012-11-19 36 views
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> 
+0

'<?php wp_get_archives('title_li =&type = postbypost&limit = 10'); ?>它也显示相同的结果。还有什么帮助? –

回答

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') 

E DIT 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()就是这样。

+0

实际上我并不了解从哪里开始,请您总结一下? Thankyou –

+0

我用一个最终代码应该是什么样子的例子更新了我的答案。 –

+0

Grt Grt Just Grt @Nikola。非常感谢。 –