最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - custom post type don’t appear in the home page

programmeradmin4浏览0评论

I would like to have different types of posts on my blog, e.g.: articles, film descriptions, book descriptions, links with short descriptions, quotations, historical events, characters, artwork, musical works, terms, etc. They should be grouped in hierarchical catalogues and hierarchical tags. Theoretically, this plug-in provides me with the possibility to do it: /. There is only a problem with it – custom post types don’t appear on the home page: /. I’m learning php, but it will take a lot of time before I get the right knowledge. I could hire a programmer, but first I would like to do a good research. I believe that there is another simpler way. Does anyone know it?

I would like to have different types of posts on my blog, e.g.: articles, film descriptions, book descriptions, links with short descriptions, quotations, historical events, characters, artwork, musical works, terms, etc. They should be grouped in hierarchical catalogues and hierarchical tags. Theoretically, this plug-in provides me with the possibility to do it: https://wordpress/plugins/custom-post-type-ui/. There is only a problem with it – custom post types don’t appear on the home page: https://wordpress/support/topic/custom-post-type-dont-appear-in-the-main-colllection/. I’m learning php, but it will take a lot of time before I get the right knowledge. I could hire a programmer, but first I would like to do a good research. I believe that there is another simpler way. Does anyone know it?

Share Improve this question asked Jan 20, 2020 at 17:40 retireti 276 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Find functions.php in your theme and add this code:

function add_custom_pt( $query ) {
  if ( !is_admin() && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'post', 'cptslug1', 'cptslug2' ) );
  }
}
add_action( 'pre_get_posts', 'add_custom_pt' );

Change array values to match your post type slugs (add as many as you need), but keep 'post'.

Use the pre_get_posts hook to modify the $query and include your custom post types in the search.

Add the following to your theme's functions.php file.

function modify_query( $query ) {
    if ( is_home() ) {
        $query->set( 'post_type', array( 'post', 'articles', 'film_descriptions', 'book_descriptions', 'characters' );
    }
}

add_action( 'pre_get_posts', 'modify_query', 10, 1 );
发布评论

评论列表(0)

  1. 暂无评论