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
2 Answers
Reset to default 2There 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:
- 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/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
版权声明:本文标题:php - Wordpress query undefined offset in loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741655440a2390731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论