【问题标题】:Show 4 different posts in Wordpress using loop使用循环在 Wordpress 中显示 4 个不同的帖子
【发布时间】:2018-11-07 05:59:30
【问题描述】:

我在我的 Wordpress 新闻站点中有一个 php 代码,它将在一个容器中一次显示来自 2 个类别的新闻和事件的 4 个不同的帖子。但是我当前的代码只显示了一个类别的一个帖子 4 次,尽管新闻和事件类别中有 4 个帖子。我想在容器中显示 4 个来自新闻和事件的不同帖子,而不是现在正在发生的 1 个帖子显示 4 次。 这是我的代码 sn-p:

<div class="dc-news-trend">
                        <?php
                            $args = array( 'posts_per_page' => 4, 'category' =>'50,52', 'orderby'=>'date','orer'=>'DESC');
                            $lastposts = get_posts( $args );
                            foreach ( $lastposts as $post ) :
                              setup_postdata( $post ); ?>
                                <div class="row">
                                    <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                        <div class="row margin-top-1">  
                                            <div class="col-lg-4 col-md-4 col-sm-6 col-6">
                                                <?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
                                            </div>
                                            <div class="col-lg-8 col-md-8 col-sm-6 col-6">
                                                <a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
                                                <p class="dc-news-date">
                                                    <?php echo get_the_date(); ?>
                                                </p>    
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                        <div class="row">
                                            <div class="col-lg-12 col-md-12 col-sm-12 col-12">
                                                <div class="row margin-top-1">  
                                                    <div class="col-lg-4 col-md-4 col-sm-6 col-6">
                                                        <?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
                                                    </div>
                                                    <div class="col-lg-8 col-md-8 col-sm-6 col-6">
                                                        <a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
                                                        <p class="dc-news-date">
                                                            <?php echo get_the_date(); ?>
                                                        </p>    
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                                <div class="row margin-top-1">  
                                                    <div class="col-lg-4 col-md-4 col-sm-6 col-6">
                                                        <?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
                                                    </div>
                                                    <div class="col-lg-8 col-md-8 col-sm-6 col-6">
                                                        <a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
                                                        <p class="dc-news-date">
                                                            <?php echo get_the_date(); ?>
                                                        </p>    
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                                <div class="row margin-top-1">  
                                                    <div class="col-lg-4 col-md-4 col-sm-6 col-6">
                                                        <?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
                                                    </div>
                                                    <div class="col-lg-8 col-md-8 col-sm-6 col-6">
                                                        <a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
                                                        <p class="dc-news-date">
                                                            <?php echo get_the_date(); ?>
                                                        </p>    
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <?php endforeach; 
                                    wp_reset_postdata(); 
                                ?>
                    </div>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在您的查询参数中使用 cat=>'50,52' 而不是 category=>....:

    <div class="dc-news-trend">
      <div class="row"> // I suppose you don't want new row for each post so we take it out from the loop
              <?php
                global $post;
                $args = array( 
                  'post_type' => 'post',
                  'cat' => '50,52',
                  'posts_per_page' => 4,  
                  'orderby' => 'date',
                  'orer' => 'DESC'
                );
    
               $lastposts = get_posts( $args );
               foreach ( $lastposts as $post ) : setup_postdata( $post ); ?>
               //now we are inside foreach  loop so we need to output div with the content just 1 time (for each returned post)
               <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                  <div class="row margin-top-1">  
                      <div class="col-lg-4 col-md-4 col-sm-6 col-6">
                        <?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
                      </div>
                      <div class="col-lg-8 col-md-8 col-sm-6 col-6">
                         <a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
                         <p class="dc-news-date"><?php echo get_the_date(); ?></p>    
                      </div>
                 </div>
              </div>
             <?php endforeach; 
                   wp_reset_postdata(); 
             ?>
        </div>
    </div>
    

    这将返回具有这些类别的帖子。查看 WP 文档中的query args

    【讨论】:

    • 你设置了全局 $post; ?我发现它在您的代码中丢失了?
    • 如何设置?我是 php 和 Wordpress 的新手
    • 只检查编辑的答案,在 args 之前的 php 开头定义它
    • hmmmm,然后尝试使用 'category__in' => array(50, 52),而不是 'cat' => ...
    • 非常感谢。它工作正常。你是救生员。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 2019-07-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多