显示多站点的合并帖子并按最新日期排序

时间:2020-11-09 作者:Danilo Muñoz

我有密码。它可以工作,但并不完美:每个网站都有一个类别;通知”;但不显示按最新日期排序的帖子,只显示为一个序列(博客1:3按日期排序的帖子,博客2:3按日期排序的帖子,博客3:3按日期排序的帖子,等等),我想一起显示。

网站:黄色来自博客2-3

enter image description here

代码

$blog_ids = array( 1, 2, 3, );
foreach( $blog_ids as $id ) {
switch_to_blog( $id );
$args = array(
\'category_name\' => \'noticias\',
\'post_status\' => \'publish\',
\'orderby\' => \'publish_date\',
\'order\' => \'DESC\',
\'posts_per_page\' => \'4\',
);

$query = new WP_Query( $args );

if( $query->have_posts() ) :
while( $query->have_posts()) : $query->the_post() ; 
?>

<div class="col-md-3 mb-36">
<div class="uc-card card-height--same">

<span class="tag-fixed"><?php $tags = get_the_tags($post->ID);  ?>
            <?php foreach($tags as $tag) :  ?><a class="uc-tag" href="<?php bloginfo(\'url\');?>/tag/<?php print_r($tag->slug);?>"><?php print_r($tag->name); ?></a> <?php endforeach; ?></span>
<div class="uc-card_body">
  <p class="p-size--sm p-color--gray p-text--condensed"><?php //the_date();?></p>
  <h4><?php the_title(); ?></h4>
  <div class="uc-text-divider divider-primary my-20"></div>
  <div class="mt-auto">
    <a href=" <?php echo get_permalink(); ?> " class="uc-btn btn-inline">
      Ver más
      <i class="uc-icon">keyboard_arrow_right</i>
    </a>
      </div>
    </div>
  </div>
</div>

<?php
endwhile;
endif;
wp_reset_postdata();
restore_current_blog();}
我希望你能帮助我,问候!

1 个回复
SO网友:Danilo Muñoz

The solution (with template parts):

$blogs = array( 1,4,5 );
$all_posts = array();
foreach( $blogs as $blog ) {
    switch_to_blog( $blog );
    $args = array( \'category_name\' => \'noticias\', \'posts_per_page\' => 2, \'orderby\' => \'publish_date\', \'order\' => \'DESC\');
    $blog_posts = get_posts( $args );
    foreach( $blog_posts as $blog_post ) {
        // Correct permalink, blogname, blog url and post thumnail (with size)
        $blog_post->permalink = get_the_permalink( $blog_post->ID );
        $blog_post->blogname = get_bloginfo( $blog_post->ID, \'name\'  );
        $blog_post->blogurl = get_site_url($blog_post->ID);
        $blog_post->post_thumbnail_url = get_the_post_thumbnail_url($blog_post->ID, \'noticia-archivo\'); 
        $all_posts[] = $blog_post;
    }
    restore_current_blog();
}

usort( $all_posts, \'__sort_by_date\' );
// Display all posts
foreach( $all_posts as $post ) {
setup_postdata( $post );
// Template part
get_template_part( \'template-parts/card-1\' );       
}

function __sort_by_date( $b, $a ) {
    $a_date = strtotime( $a->post_date );
    $b_date = strtotime( $b->post_date );
    if( $a_date === $b_date ) { return 0; }
    if( $a_date > $b_date ) { return 1; }
    return -1;
}

the template part:

<div class="item">
<div class="card-height--same">
<img src= <?php echo \'"\'.esc_url($post->post_thumbnail_url).\'"\'; ?> class="img-fluid" alt="">
<span class="tag-fixed"><a href="<?php echo( \'\' . $post->permalink . \'\' ); ?>" style="background: #fec60d;color: #444;"> <?php echo $post->blogname; ?> </a></span>
        <div class="uc-card_body">
          <p class="p-size--sm p-color--gray p-text--condensed"><?php the_date();?></p>
          <h4><?php the_title(); ?></h4>
          <div class="mt-auto">
<?php echo( \'<a class="uc-btn btn-inline" href="\' . $post->permalink . \'">\' ); echo( \'Ver más<i class="uc-icon">keyboard_arrow_right</i></a>\' ); ?>
          </div>
        </div>
      </div>
    </div>

相关推荐

本地WordPress MultiSite在AWS开发服务器上不工作“建立数据库连接时出错”

我在本地mac上使用mamp安装了composer WP starter。它按预期工作。我正在使用phpdotenv隐藏我的wp配置。php设置和我将wordpress目录移动到文档根目录下的子目录中。我在本地mac dev2上使用虚拟主机。域名。com。我使用wp db migrate pro查找dev2并将其替换为dev3作为域名。com在本地和AWS上是相同的。唯一的其他区别是数据库连接设置。我知道数据库连接设置是正确的,因为我在运行composer并使用wp cli安装wp的shell脚本中使用了