admin管理员组文章数量:1026989
Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..
Added to functions.php
this works for home page:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
But this works for neither:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
Would also like to add in !is_admin()
to keep the admin screens clear, and not sure how to go about that.
thanks,
Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..
Added to functions.php
this works for home page:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
But this works for neither:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
Would also like to add in !is_admin()
to keep the admin screens clear, and not sure how to go about that.
thanks,
Share Improve this question edited Mar 23, 2019 at 16:33 Qaisar Feroz 2,1471 gold badge9 silver badges20 bronze badges asked Mar 23, 2019 at 4:23 MatJonMatJon 174 bronze badges1 Answer
Reset to default 2In your code ( $query->is_home() && $query->is_date() && $query->is_main_query() )
always returns false
because $query->is_home()
and $query->is_date()
cannot be true
on same page.
Try this,
function add_custom_post_type_to_query( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_date() || $query->is_home() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
I hope this helps!
Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..
Added to functions.php
this works for home page:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
But this works for neither:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
Would also like to add in !is_admin()
to keep the admin screens clear, and not sure how to go about that.
thanks,
Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..
Added to functions.php
this works for home page:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
But this works for neither:
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
Would also like to add in !is_admin()
to keep the admin screens clear, and not sure how to go about that.
thanks,
Share Improve this question edited Mar 23, 2019 at 16:33 Qaisar Feroz 2,1471 gold badge9 silver badges20 bronze badges asked Mar 23, 2019 at 4:23 MatJonMatJon 174 bronze badges1 Answer
Reset to default 2In your code ( $query->is_home() && $query->is_date() && $query->is_main_query() )
always returns false
because $query->is_home()
and $query->is_date()
cannot be true
on same page.
Try this,
function add_custom_post_type_to_query( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_date() || $query->is_home() ) {
$query->set( 'post_type', array('post', 'pictures') );
}
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
I hope this helps!
本文标签: Custom post type show on both Home and Date Archive
版权声明:本文标题:Custom post type show on both Home and Date Archive 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745663569a2162039.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论