admin管理员组文章数量:1122846
A plugin I am using creates a custom post type for articles ie. 'ht_kb'.
In my themes front page I have a custom loop displaying recent posts of the standard 'post' type. On the same front page I have another custom loop displaying recent posts of the plugins custom 'ht_kb' post type. The standard post type loop works as expected, but with the custom post type loop I am getting the error as posted below.
I am having difficulty understanding the reason for this error and would really appreciate if someone could explain what i am doing wrong and what is the correct way of looping through a custom post type.
The loop below works just fine with the standard 'post' type.
//The Query
$post_args = array(
'post_type' => array('post'),
'posts_per_page' => 4
);
$post_query = new WP_Query($post_args);
// The Loop
if ($post_query->have_posts()) {
while ($post_query->have_posts()) {
$post_query->the_post();
the_title();
the_category();
the_date();
the_author();
the_excerpt();
}
//reset loop
wp_reset_postdata();
}
?>
The custom post type loop below returns the error:
Notice: Trying to get property of non-object in C:\localhost\mywebsite\wp->includes\template.php on line 679
Notice: Trying to get property of non-object in C:\localhost\mywebsite\wp->includes\template.php on line 679
Loop for the custom post type
<?php
// The Query
$article_args = array(
'post_type' => array('ht_kb'),
'posts_per_page' => 4
);
$article_query = new WP_Query($article_args);
// The Loop
if ($article_query->have_posts()) {
while ($article_query->have_posts()) {
$article_query->the_post();
the_title();
the_category();
the_date();
the_author();
the_excerpt();
}
//reset loop
wp_reset_postdata();
}
?>
note: If I comment out the_excerpt(); function, The loop does not display the error and (sort of) works as intended although other issues such as, the_category() function does not return any value.
Thanking you for your time and help.
A plugin I am using creates a custom post type for articles ie. 'ht_kb'.
In my themes front page I have a custom loop displaying recent posts of the standard 'post' type. On the same front page I have another custom loop displaying recent posts of the plugins custom 'ht_kb' post type. The standard post type loop works as expected, but with the custom post type loop I am getting the error as posted below.
I am having difficulty understanding the reason for this error and would really appreciate if someone could explain what i am doing wrong and what is the correct way of looping through a custom post type.
The loop below works just fine with the standard 'post' type.
//The Query
$post_args = array(
'post_type' => array('post'),
'posts_per_page' => 4
);
$post_query = new WP_Query($post_args);
// The Loop
if ($post_query->have_posts()) {
while ($post_query->have_posts()) {
$post_query->the_post();
the_title();
the_category();
the_date();
the_author();
the_excerpt();
}
//reset loop
wp_reset_postdata();
}
?>
The custom post type loop below returns the error:
Notice: Trying to get property of non-object in C:\localhost\mywebsite\wp->includes\template.php on line 679
Notice: Trying to get property of non-object in C:\localhost\mywebsite\wp->includes\template.php on line 679
Loop for the custom post type
<?php
// The Query
$article_args = array(
'post_type' => array('ht_kb'),
'posts_per_page' => 4
);
$article_query = new WP_Query($article_args);
// The Loop
if ($article_query->have_posts()) {
while ($article_query->have_posts()) {
$article_query->the_post();
the_title();
the_category();
the_date();
the_author();
the_excerpt();
}
//reset loop
wp_reset_postdata();
}
?>
note: If I comment out the_excerpt(); function, The loop does not display the error and (sort of) works as intended although other issues such as, the_category() function does not return any value.
Thanking you for your time and help.
Share Improve this question edited Sep 23, 2017 at 9:41 Samwyzz asked Sep 23, 2017 at 9:40 SamwyzzSamwyzz 2110 bronze badges 3 |1 Answer
Reset to default 0Hey this may be a little too late however I had this same issue on my custom made theme.
Firstly it turned out I didn't create any custom posts and secondly, with the loop, because there were no posts I didn't have the content-none file in my theme.
Once this was added the get_template_part( 'content-parts/content', 'none' ); code the error was gone.
Hope this helps someone else :)
本文标签: wp queryCustom post type loop error Trying to get property of nonobject
版权声明:本文标题:wp query - Custom post type loop error: Trying to get property of non-object 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286765a1927736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ht_kb
is defined with title, excerpt and all that extra stuff? – kero Commented Sep 23, 2017 at 9:41