最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - Why is my archive page looping through all but one post?

programmeradmin5浏览0评论

I've created an archive page for my custom posts, it loops through every one except the first. There is 11 posts overall and I only get 10 on the archive page.

Here is my archive template:

<div class="container">
  <h2 class="entry-title text-center pt-5">Market Place</h2>
  <p class="text-center">Filter providers by service and click on the logos shown below for more information and to contact a representative of the risk solution provider.</p>
  <div class="category-search-box text-center"><?php echo do_shortcode( '[searchandfilter fields="provider,categories"]' ); ?></div>
  <div class="row py-5">
    <?php if (have_posts()) : while (have_posts()) :the_post();
          $taxonomy = wp_get_object_terms($post->ID, 'categories');
          $ids = "";

          foreach ($taxonomy as $cat) {
           $ids .= "cat-".$cat->term_id ." ";
         }
        ?>
      <div class="col-6 col-md-4 text-center">
        <div id="provider-archive-boxes" class="provider-archive-box <?php echo $ids; ?>">
          <a href="<?php echo the_permalink();?>"><?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive' ) ); ?></a>
          <p><strong><?php the_title();?></strong></p>
        </div>
      </div>
    <?php endwhile; ?>
    </div>
  <?php endif; ?>
</div>

Here is how I registered the post in the functions.php file:

function provider_setup_post_type() {
    $args = array(
        'public'    => true,
        'label'     => __( 'Providers', 'textdomain' ),
                "public" => true,
                "publicly_queryable" => true,
                "show_ui" => true,
                "show_in_rest" => false,
                "rest_base" => "",
                "has_archive" => 'marketplace',
                "show_in_menu" => true,
                "exclude_from_search" => false,
                "capability_type" => "page",
                "map_meta_cap" => true,
                "hierarchical" => true,
                'rewrite' => array('slug' => 'marketplace'),
        'menu_icon' => 'dashicons-building',
                "supports" => array( "thumbnail","post-thumbnail","title", "editor" ),
    );
    register_post_type( 'provider', $args );

        register_taxonomy("categories", array("provider"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => array( 'slug' => 'marketplace', 'with_front'=> false )));
}
add_action( 'init', 'provider_setup_post_type' );

I've created an archive page for my custom posts, it loops through every one except the first. There is 11 posts overall and I only get 10 on the archive page.

Here is my archive template:

<div class="container">
  <h2 class="entry-title text-center pt-5">Market Place</h2>
  <p class="text-center">Filter providers by service and click on the logos shown below for more information and to contact a representative of the risk solution provider.</p>
  <div class="category-search-box text-center"><?php echo do_shortcode( '[searchandfilter fields="provider,categories"]' ); ?></div>
  <div class="row py-5">
    <?php if (have_posts()) : while (have_posts()) :the_post();
          $taxonomy = wp_get_object_terms($post->ID, 'categories');
          $ids = "";

          foreach ($taxonomy as $cat) {
           $ids .= "cat-".$cat->term_id ." ";
         }
        ?>
      <div class="col-6 col-md-4 text-center">
        <div id="provider-archive-boxes" class="provider-archive-box <?php echo $ids; ?>">
          <a href="<?php echo the_permalink();?>"><?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive' ) ); ?></a>
          <p><strong><?php the_title();?></strong></p>
        </div>
      </div>
    <?php endwhile; ?>
    </div>
  <?php endif; ?>
</div>

Here is how I registered the post in the functions.php file:

function provider_setup_post_type() {
    $args = array(
        'public'    => true,
        'label'     => __( 'Providers', 'textdomain' ),
                "public" => true,
                "publicly_queryable" => true,
                "show_ui" => true,
                "show_in_rest" => false,
                "rest_base" => "",
                "has_archive" => 'marketplace',
                "show_in_menu" => true,
                "exclude_from_search" => false,
                "capability_type" => "page",
                "map_meta_cap" => true,
                "hierarchical" => true,
                'rewrite' => array('slug' => 'marketplace'),
        'menu_icon' => 'dashicons-building',
                "supports" => array( "thumbnail","post-thumbnail","title", "editor" ),
    );
    register_post_type( 'provider', $args );

        register_taxonomy("categories", array("provider"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => array( 'slug' => 'marketplace', 'with_front'=> false )));
}
add_action( 'init', 'provider_setup_post_type' );
Share Improve this question asked Jul 12, 2020 at 4:32 09eric0909eric09 52 bronze badges 2
  • 1 The default number of posts per page is 10, so you're just missing pagination controls to go to page 2: developer.wordpress/themes/functionality/pagination – Jacob Peattie Commented Jul 12, 2020 at 5:53
  • Thanks, I forgot the default is 10. – 09eric09 Commented Jul 12, 2020 at 11:39
Add a comment  | 

1 Answer 1

Reset to default 0

As @Jacob Peattie says, pagination is what's causing this, and probably what you want to add if you will have more posts over time.

If you want you can disable pagination for archive pages easily with this in your functions.php:

    function wpse_disable_pagination( $query ) {
        if (is_archive()) {
            $query->set('nopaging', 1 );
        }
    }

    add_action( 'pre_get_posts', 'wpse_disable_pagination' );
发布评论

评论列表(0)

  1. 暂无评论