Wordpress query undefined offset in loop
Frage
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?
Lösung
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.
Andere Tipps
There are five default Post Types readily available to users or internally used by the WordPress installation:
- Post (Post Type: ‘post’)
- Page (Post Type: ‘page’)
- Attachment (Post Type: ‘attachment’)
- Revision (Post Type: ‘revision’)
- Navigation menu (Post Type: ‘nav_menu_item’)
refer this link for more: https://developer.wordpress.org/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.