最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom Post Type Categories URL not displaying posts

matteradmin9PV0评论

I'm not as good as many wordpress developers here and I hope someone can help me, I've created a Custom post types for hotel destinations with categories for country and city. When I go to mysite/destinations the posts show normally. But when I try to go to a category URL (say, new york for example) by going to mysite/destinations/new-york I "get no posts where found".

On functions.php The only thing I have regarding the custom post types is that I only call the custom post types php page

require_once('hotels-manager.php');

On hotels-manager.php where I actually create the custom post type I have:

<?php 
if(!function_exists('hotel_manager_register')){
function hotel_manager_register()
{

    $labels = array(
        'name' => __( 'Hotels Manager' ),
        'singular_name' => __( 'Hotel' ),
        'add_new' => __( 'Add New' ),
        'add_new_item' => __( 'Add New Hotel' ),
        'edit_item' => __( 'Edit Hotel' ),
        'new_item' => __( 'New Hotel' ),
        'view_item' => __( 'View Hotel' ),
        'search_items' => __( 'Search Hotel' ),
        'not_found' => __( 'No Hotel Found' ),
        'not_found_in_trash' => __( 'No Hotel Found In Trash' ),
        'parent_item_colon' => '',
        'menu_name' => __( 'Hotels Manager' )
    );
    $args = array(
        'labels' => $labels,
        'menu_icon' => 'dashicons-admin-multisite',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'destinations', 'with_front' => false),
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_position' => null,
        'has_archive' => true,
        'supports' => array( 'title','editor', 'thumbnail', 'revisions', 'custom-fields')
    );

    register_post_type( 'destinations', $args);
}
}
add_action('init', 'hotel_manager_register');
 // hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_destinations_taxonomies', 0 );



// create two taxonomies, genres and writers for the post type "book"
if(!function_exists('create_destinations_taxonomies'))
{
    function create_destinations_taxonomies() {
        $labels = array(
            'name' => __('Hotel Regions'),
            'singular_name' => __('Region'),
            'search_items' => __('Search Regions'),
            'all_items' => __('All Regions'),
            'parent_item' => __('Parent'),
            'parent_item_colon' => __('Parent:'),
            'edit_item' => __('Edit Region'),
            'update_item' => __('Update Region'),
            'add_new_item' => __('Add New Region'),
            'new_item_name' => __('New Region'),
            'menu_name' => __('Regions'),
        );
        $args = array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'destinations','with_front' => false ),
        );
        register_taxonomy( 'destinations', array( 'destinations', 'public' => true, ), $args ); 
    }
}

?>

And finally index.php simply calls:

<?php get_header(); ?>
    <section class="stage">
        <div class="container">
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <h1><?php the_title(); ?></h1>              
             <?php the_content('Read More...'); ?>
            <?php endwhile; else: ?>
            <p><?php _e('No posts were found. Sorry!'); ?></p>
            <?php endif; ?>
        </div>
    </section>  
<?php get_footer(); ?>

Any help would be really, really appreciated.

I'm not as good as many wordpress developers here and I hope someone can help me, I've created a Custom post types for hotel destinations with categories for country and city. When I go to mysite/destinations the posts show normally. But when I try to go to a category URL (say, new york for example) by going to mysite/destinations/new-york I "get no posts where found".

On functions.php The only thing I have regarding the custom post types is that I only call the custom post types php page

require_once('hotels-manager.php');

On hotels-manager.php where I actually create the custom post type I have:

<?php 
if(!function_exists('hotel_manager_register')){
function hotel_manager_register()
{

    $labels = array(
        'name' => __( 'Hotels Manager' ),
        'singular_name' => __( 'Hotel' ),
        'add_new' => __( 'Add New' ),
        'add_new_item' => __( 'Add New Hotel' ),
        'edit_item' => __( 'Edit Hotel' ),
        'new_item' => __( 'New Hotel' ),
        'view_item' => __( 'View Hotel' ),
        'search_items' => __( 'Search Hotel' ),
        'not_found' => __( 'No Hotel Found' ),
        'not_found_in_trash' => __( 'No Hotel Found In Trash' ),
        'parent_item_colon' => '',
        'menu_name' => __( 'Hotels Manager' )
    );
    $args = array(
        'labels' => $labels,
        'menu_icon' => 'dashicons-admin-multisite',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'destinations', 'with_front' => false),
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_position' => null,
        'has_archive' => true,
        'supports' => array( 'title','editor', 'thumbnail', 'revisions', 'custom-fields')
    );

    register_post_type( 'destinations', $args);
}
}
add_action('init', 'hotel_manager_register');
 // hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_destinations_taxonomies', 0 );



// create two taxonomies, genres and writers for the post type "book"
if(!function_exists('create_destinations_taxonomies'))
{
    function create_destinations_taxonomies() {
        $labels = array(
            'name' => __('Hotel Regions'),
            'singular_name' => __('Region'),
            'search_items' => __('Search Regions'),
            'all_items' => __('All Regions'),
            'parent_item' => __('Parent'),
            'parent_item_colon' => __('Parent:'),
            'edit_item' => __('Edit Region'),
            'update_item' => __('Update Region'),
            'add_new_item' => __('Add New Region'),
            'new_item_name' => __('New Region'),
            'menu_name' => __('Regions'),
        );
        $args = array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'destinations','with_front' => false ),
        );
        register_taxonomy( 'destinations', array( 'destinations', 'public' => true, ), $args ); 
    }
}

?>

And finally index.php simply calls:

<?php get_header(); ?>
    <section class="stage">
        <div class="container">
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <h1><?php the_title(); ?></h1>              
             <?php the_content('Read More...'); ?>
            <?php endwhile; else: ?>
            <p><?php _e('No posts were found. Sorry!'); ?></p>
            <?php endif; ?>
        </div>
    </section>  
<?php get_footer(); ?>

Any help would be really, really appreciated.

Share Improve this question asked Apr 11, 2019 at 18:50 JoxmarJoxmar 1033 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are specifying the same slug of destinations for both your custom post type and for your taxonomy, unfortunately by default this doesn't behave how you'd expect due to WordPress' rewrite API. Which caters for the default nature of posts, archives, taxonomies, slugs and the WordPress template hierarchy.

If this URL structure is not important to you then you could change one of your slugs to destinations and the other to destination. Ensuring you flush your rewrite rules after doing so

Alternatively if this URL structure is important you may find this answer useful

Post a comment

comment list (0)

  1. No comments so far