admin管理员组

文章数量:1300069

I created a query with arguments see blow. On the page I see an error in a loop

  • Notice undefined offset 1

  • Notice undefined offset 2

  • Notice undefined offset 3 and so on...

    $args = array (
      'post_type'     => 'catalog',
      'post_status'   => 'publish',
    );
    $loop = new WP_Query( $args );
       if ( $loop->have_posts() ) {
          while ( $loop->have_posts() ) {
              the_post(); 
              echo get_the_title();
          }
      }
    

I tried other arguments but this does not work.

  • 'posts_per_page' => 4

Please who can help me?

I created a query with arguments see blow. On the page I see an error in a loop

  • Notice undefined offset 1

  • Notice undefined offset 2

  • Notice undefined offset 3 and so on...

    $args = array (
      'post_type'     => 'catalog',
      'post_status'   => 'publish',
    );
    $loop = new WP_Query( $args );
       if ( $loop->have_posts() ) {
          while ( $loop->have_posts() ) {
              the_post(); 
              echo get_the_title();
          }
      }
    

I tried other arguments but this does not work.

  • 'posts_per_page' => 4

Please who can help me?

Share Improve this question asked Mar 25, 2021 at 12:24 StefanStefan 31 bronze badge 1
  • 1 What's the full error? You've removed all the useful parts of the error message, such as the line it happened on and the file. Why do you think that this post loop is the cause? There is nothing in the questions code capable of producing these errors – Tom J Nowell Commented Mar 25, 2021 at 13:40
Add a comment  | 

2 Answers 2

Reset to default 2

There might be other causes to the issue in question, but one issue I noticed in your code is that because you are looping through a custom WP_Query instance, i.e. $loop, then you need to use $loop->the_post() instead of just the_post():

while ( $loop->have_posts() ) {
    $loop->the_post();
    the_title();
}

And you could see that I simply called the_title() and not doing echo get_the_title(), so you should also do the same.

There are five default Post Types readily available to users or internally used by the WordPress installation:

  1. Post (Post Type: ‘post’)
  2. Page (Post Type: ‘page’)
  3. Attachment (Post Type: ‘attachment’)
  4. Revision (Post Type: ‘revision’)
  5. Navigation menu (Post Type: ‘nav_menu_item’)

refer this link for more: https://developer.wordpress/themes/basics/post-types/ according to me your post type in the query is wrong. use post_type => 'post' instead of post_type => 'catalog'

$args = array (
  'post_type'     => 'post',
  'post_status'   => 'publish',
);
query_posts($args); 
                if(have_posts()) {
                    while(have_posts()) {
                        the_post();
                        echo get_the_title();
                           }
                     }

Please correct it if you find it correct.

本文标签: phpWordpress query undefined offset in loop