【问题标题】:Wordpress loop: custom number of posts and sort by dateWordpress 循环:自定义帖子数量并按日期排序
【发布时间】:2021-04-21 03:32:29
【问题描述】:

我试图在网站上找到我需要的解决方案(例如:https://www.baiweb.nl/)。我一直在寻找一种方法来显示每个类别的自定义帖子数量并按日期对它们进行排序。我已经设法在每个类别中获得一个帖子,但我似乎无法修复其余部分。它现在按日期对帖子本身进行排序,但不是全部。

所以,我的问题是:

  1. 是否可以按日期对所有不同类别的循环进行排序?
  2. 是否可以控制每个类别显示的帖子数量?这个对我来说是额外的,不是必需的,但它会很好。

希望有人可以提供帮助!非常感谢您抽出宝贵时间!

这是我现在在循环中使用的代码:

    <?php 

    $categories = get_categories();
    $cats = array();
    foreach($categories as $category) {
        $cats[] = $category->name . ", ";
    }

    $exclude_posts = array();
    foreach( $cats as $cat ) {
    // build query argument
    $query_args = array(
            'category_name' => $cat,
            'showposts' => 1,
            'post_type' => 'post',
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'DESC'
    );

    // exclude post that already have been fetched
    // this would be useful if multiple category is assigned for same post
    if( !empty($exclude_posts) )
        $query_args['post__not_in'] = $exclude_posts;

    // do query
    $query = new WP_Query( $query_args );

    // check if query have any post
    if ( $query->have_posts() ) {

        // start loop
        while ( $query->have_posts() ) {
            // set post global
            $query->the_post();

            // add current post id to exclusion array
            $exclude_posts[] = get_the_ID();

            ?>
                            <article id="post-<?php the_ID(); ?>" <?php post_class('loop');?>>

                                <div class="corner"><span><?php foreach((get_the_category()) as $category){ echo $category->name.'<br> '; } ?></span></div>

                                <!-- thumbnail -->
                                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="image">
                                <?php 
                                    $thumb_id = get_post_thumbnail_id();
                                    $thumb_url = wp_get_attachment_image_src($thumb_id,'medium', true);
                                    if ( in_category('2') || in_category('32') ) {
                                        echo '<div class="newstitle">' . the_title() . '</div>';
                                    } 

                                    else {
                                        if ( has_post_thumbnail()) {
                                            echo "<div class='ctr-image test2' style='background-image: url(" . $thumb_url[0] . ")'></div>";
                                        }
                                        else {
                                            echo '<div class="newstitle">' . the_title() . '</div>';
                                        } 
                                    } 
                                ?>          
                            </a>        

                            <div class="content">
                                <span class="date"><?php the_time('j/m/Y'); ?></span>
                                <h3>
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                        <?php if ( in_category('2') || in_category('32')  ) {} 
                                            else {  echo the_title();} ?>           
                                    </a>
                                </h3>
                                <div class="text">
                                    <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
                                </div>
                            </div>
                        </article>  
            <?php
            
            // do something
        }
    } else {
        // no posts found
    }

    // Restore original Post Data
    wp_reset_postdata();
} ?>

【问题讨论】:

    标签: php wordpress loops date categories


    【解决方案1】:

    你很亲密。您的类别foreach 循环必须包含所有内容。然后我们做一个简单的循环,但我们根据每个类别指定要使用的参数。

    这是我们的最终结果。

    <?php
    $categories = get_categories(); // ... get all our categories
    foreach( $categories as $category ) { // ... start foreach categories
      if ( $category->name == 'Mercury' ) { // ... if our category name is 'Mercury'
        $posts_per_page = 1; // ... 1 post per page if our category is named 'Mercury'
      } else { // ... else, for all other categories
        $posts_per_page = 3; // ... 3 posts per page
      };
      $args = array( // ... all our arguments
        'posts_per_page' => $posts_per_page, // ... 1 or 3 posts per page depending on our categories
        'post_type' => 'post',
        'category_name' => $category->name,
        'post_status' => 'publish',
        'orderby' => 'date', // ... order by date
        'order' => 'ASC', // ... most recent first 
      );
      $query = new WP_Query( $args ); // .. start a new loop
      if( $query->have_posts() ):
        echo '<section>';
        echo '<h1>' . $category->name . '</h1>'; // ... only display our section IF a post exist in the category
        while( $query->have_posts() ): $query->the_post(); 
          echo '<article>'; // ... our post template
          the_title( '<h4>', '</h4>' );
          echo '</article>';
        endwhile;
        echo '</section>';
      endif;
      wp_reset_postdata(); // ... After looping through a separate query, this function restores the $post global to the current post in the main query. 
    };  // ... end foreach categories
    ?>
    

    【讨论】:

      【解决方案2】:

      非常感谢您快速而全面的回答!类别数量的一部分工作得很好,但是,结果与我想要的略有不同。可能是我的问题不清楚,对此表示歉意。

      您已经制作的漂亮的是所有具有属于它的帖子的类别。我想要实现的是所有消息都混合在一起,按日期排序,并且在这里已经完成的工作,我可以指出每个类别中有多少帖子。这可能吗?

      您的代码结果的一些屏幕截图:

      这是我正在尝试的,除了现在我需要按日期排序:

      这是稍微调整的代码:

      <?php
      $categories = get_categories(); // ... get all our categories
      foreach( $categories as $category ) { // ... start foreach categories
          if ( $category->name == 'Mercury' ) { // ... if our category name is 'Mercury'
              $posts_per_page = 1; // ... 1 post per page if our category is named 'Mercury'
          } else { // ... else, for all other categories
              $posts_per_page = 2; // ... 3 posts per page
          };
          $args = array( // ... all our arguments
              'posts_per_page' => $posts_per_page, // ... 1 or 3 posts per page depending on our categories
              'post_type' => 'post',
              'category_name' => $category->name,
              'post_status' => 'publish',
              'orderby' => 'date', // ... order by date
              'order' => 'ASC', // ... most recent first 
          );
          $query = new WP_Query( $args ); // .. start a new loop
          if( $query->have_posts() ):
              // echo '<section>';
              // echo '<h1>' . $category->name . '</h1>'; // ... only display our section IF a post exist in the category
              while( $query->have_posts() ): $query->the_post(); 
                  echo '<article>'; // ... our post template
                  echo '<h1>' . $category->name . '</h1>';
                  the_time('j/m/Y');
                  the_title( '<h4>', '</h4>' );
                  echo '</article>';
              endwhile;
              // echo '</section>';
          endif;
          wp_reset_postdata(); // ... After looping through a separate query, this function restores the $post global to the current post in the main query. 
      };  // ... end foreach categories ?>
      

      希望这很清楚,再次感谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-23
        • 1970-01-01
        相关资源
        最近更新 更多