从网络(多站点)获取帖子

时间:2017-02-19 作者:Mathieu

下面的代码给出了来自网络的所有帖子。我正在努力实现的目标:

选择要显示的博客(按ID)选择要显示的帖子数量(我的代码选择帖子数量per blog)

$blogs = get_last_updated();

 foreach ($blogs AS $blog)    {    

  switch_to_blog($blog["blog_id"]);
  $lastposts = get_posts(\'numberposts=3\');

  foreach($lastposts as $post) : ?>

    <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>

  <?php endforeach;

  restore_current_blog();
  }

5 个回复
最合适的回答,由SO网友:Rick Hellewell 整理而成

我创建了一个插件,它做了类似的事情(称为多站点后期显示https://wordpress.org/plugins/multisite-post-reader/ ) . 它显示来自所有多站点子站点的帖子。

其中的代码可能对您的操作有所帮助。欢迎您深入研究它并使用代码来帮助您的项目。(毕竟,我使用了其他人的代码片段来开发它。)

我在完成多站点媒体显示后写了这篇文章,因为我想在一个页面上显示子网站中的媒体,但找不到任何插件。这两种方法对于监控我的多站点发布的媒体和内容都很有用。

免费、开源等等。希望对您有所帮助。

SO网友:Bastian Immanuel André

Ricks的回答当然很有帮助,但我想分享我的方法,即采用或扩展您的代码:

首先获取网络中选定博客的列表:

$args = array(\'site__in\' => array(2, 3, 6))
$sitesObj = get_sites($args);
$sites = object_to_array($sitesObj);
您还可以使用排除网站\'site__not_in\' 在的论点中get_sites().

转换$sitesObj 对象到数组中:

$sites = object_to_array($sitesObj);

object_to_array($object) {
  if (!is_object($object) && !is_array($object)) {
    return $object;
  }
  return array_map(\'object_to_array\', (array) $object) ;
}
然后初始化计数器以控制要显示的帖子总数,并切换到每个选定的博客以使用自定义参数触发循环:

$postCounter = 0;
$maxPosts = 5; // total number of posts to show

foreach ($sites as $site) {
  switch_to_blog($site[\'blog_id\']);

  $args = array(
    \'post_type\' => \'post\', // or custom post type
    \'posts_per_page\' => 2, // number of posts per blog
    \'order\' => \'DESC\',
    \'orderby\' => \'date\' // you could also use \'rand\' here
  );

  $loop = new WP_Query($args);

  if ($loop->have_posts()) :
    while ($loop->have_posts() && $counter < $maxPosts) : $loop->the_post();
      // your output
    endwhile;
  endif;

  restore_current_blog();
}
我希望这有帮助:)

SO网友:Skeffonics

This

function wolpostcount_shortcode($atts) {

  function object_to_array($object) {
    if (!is_object($object) && !is_array($object)) {
      return $object;
    }
    return array_map(\'object_to_array\', (array) $object) ;
  }

  $args = array(\'site__in\' => array(1,7,8,12,14,15,20,21,22,25,32,33,36,41,42,46,47,48,49));
  $sitesObj = get_sites($args);
  $sites = object_to_array($sitesObj);

  foreach ($sites as $site) {
    switch_to_blog($site[\'blog_id\']);

    $postcount = wp_count_posts(\'post\')->publish;
    $pagecount = wp_count_posts(\'page\')->publish;
     echo \'Posts:\'.$postcount.\' Pages:\'.$pagecount.\'<br>\';
    $totalpostcount = $totalpostcount + $postcount;
    $totalpagecount = $totalpagecount + $pagecount;
    restore_current_blog();
  }

  echo \'number of posts \'.$totalpostcount.\'<br>\';
  echo \'number of pages \'.$totalpagecount.\'<br>\';
SO网友:Sudip Debnath
function get_all_networks_posts($args){
   if(!$args){
      return false;
   }
   $sites = get_sites();
   $blog_posts = array();
   if($sites){
      foreach ($sites as $site) {
         switch_to_blog($site->id);
         $posts = get_posts($args);
         $blog_posts[$site->id] = $posts; // this is the above line
         //$blog_posts[] = $posts;If you do not need site ids then use this line removing just the above line
      }
   }
   restore_current_blog();
   if($blog_posts){
      return $blog_posts;
   }
}

// Print a demo
$args = arrat(
   \'post_type\' => \'post\',
   \'posts_per_page\' => -1,
);
print_r(get_all_networks_posts($args));
SO网友:t3chernobyl

不久前,我编写了一个小的内部插件,用于在网络子网站上显示来自主站点(博客所在地)的帖子。此外,我只想在某个子网站上显示特定类别的帖子。

function custom_multiblog ( $atts ) {
    // Get current blog
    $original_blog_id = get_current_blog_id();

    // Setup a category for each blog id you want to loop through - EDIT
    if ( 19 == $original_blog_id ) {
        $catslug_per_blog_id = array( 1 => \'category1\', );
    } else if ( 22 == $original_blog_id ) {
        $catslug_per_blog_id = array( 1 => \'category2\', );
    } else if ( 23 == $original_blog_id ) {
        $catslug_per_blog_id = array( 1 => \'category3\', );
    } else {
        $catslug_per_blog_id = array( 1 => \'category1, category2, category3\', );
    }
    
    foreach( $catslug_per_blog_id as $bid => $catslug ) {
        //Switch to the blog with the blog id $bid
        switch_to_blog( $bid ); 
        $args = array(
            //\'post_type\' => \'post\',
            \'category_name\'  => $catslug,
            \'posts_per_page\' => 10,
        );
        $args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
        
        echo \'<div class="multiblog-wrapper">\';
            $query = new WP_Query( $args );

            while( $query->have_posts() ) : $query->the_post();
                echo \'<article class="multiblog-item">\';
                    echo \'<div class="multiblog-img"><a href="\' .get_the_permalink() . \'">\';
                    the_post_thumbnail (\'large\');
                    echo \'</a></div>\';
                    echo \'<div class="multiblog-content">\';
                        echo \'<h2 style="line-height: 1em;"><a href="\' .get_the_permalink() . \'">\' . get_the_title() . \'</a></h2>\';
                        echo \'<p>\' . get_the_excerpt() . \'</p>\';
                    echo \'</div>\';
                    echo \'<div class="clearfix"></div>\';
                echo \'</article>\';  
            endwhile;
        
        echo \'</div>\';
    }

    switch_to_blog( $original_blog_id ); 
    wp_reset_postdata();
    echo \'<div class="clearfix"></div>\';
    echo \'<div class="multiblog-navigation">\';
        previous_posts_link( \'<< newer\' );
        next_posts_link( \'older >>\', $query->max_num_pages );
    echo \'</div>\';

}
add_shortcode( \'multiblog\', \'jetzt_konf_multiblog\' );

相关推荐

如何为class-wp-query.php追踪这些PHP通知和警告的原因?

我的WordPress调试。日志文件中充满了这些PHP通知,间隔大约15分钟,然后是几个小时…[23-Mar-2018 05:33:00 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736 [23-Mar-2018 05:33:00 UTC] PHP Notice: Trying to get p