简体   繁体   中英

Wordpress custom post types and categories, posts not showing up from custom post types when queried in index.php

I have a set of custom post types in my wordpress theme, and a category named Home, to show particular posts from all post types i wish on main index.php template page. What i am doing is adding the custom post type like this in my functions.php -

function livingroom_post_type() {

$labels = array(
    'name'                => _x( 'livingrooms', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'livingroom', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'livingroom', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent livingroom:', 'text_domain' ),
    'all_items'           => __( 'All livingrooms', 'text_domain' ),
    'view_item'           => __( 'View livingroom', 'text_domain' ),
    'add_new_item'        => __( 'Add New livingroom', 'text_domain' ),
    'add_new'             => __( 'New livingroom', 'text_domain' ),
    'edit_item'           => __( 'Edit livingroom', 'text_domain' ),
    'update_item'         => __( 'Update livingroom', 'text_domain' ),
    'search_items'        => __( 'Search livingrooms', 'text_domain' ),
    'not_found'           => __( 'No livingrooms found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No livingrooms found in Trash', 'text_domain' ),
);
$args = array(
    'label'               => __( 'livingroom', 'text_domain' ),
    'description'         => __( 'livingroom information pages', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array('title','editor','author','excerpt','custom-fields','thumbnail'),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
    'taxonomies' => array('category', 'post_tag'),          
);
register_post_type( 'livingroom', $args );

}
add_action( 'init', 'room_post_type' );

and then querying my posts in room.php(template file) like -

$args = array( 'post_type' => array('bathroom') );
$loop = new WP_Query( $args );

which works fine. But when in my admin panel i add this post in Home category and try to display it in the main index.php page like this -

$args = array( 'category__in' => array(get_cat_ID('Home')) );
$loop = new WP_Query( $args );

It doesn't show up. Moreover, if I create the post in posts-> add new instead of room->Add New , the same thing works fine. I did some google which told me to add taxonomies to the post type in functions.php but that obviously is not working, so any suggestions how do i make it work please.

Try using this on your index.php:

$args = array(
  'post_type' => array( 'post', 'livingroom' ), 
  'category__in' => array(get_cat_ID('Home')) 
);
$loop = new WP_Query( $args );

In 'post_type' array list all post types that you want to appear on home page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM