自定义帖子类型类别URL不显示帖子

时间:2019-04-11 作者:Joxmar

我没有这里的wordpress开发人员那么好,我希望有人能帮助我,我为酒店目的地创建了一个自定义的帖子类型,其中包含国家和城市的类别。当我去mysite的时候。POST正常显示的com/目的地。但当我试图通过访问mysite访问类别URL(例如,纽约)时。com/destinations/newyork我“找不到帖子”。

关于函数。php关于自定义post类型,我唯一能做的就是只调用自定义post类型php页面

require_once(\'hotels-manager.php\');
关于酒店经理。php,其中我实际创建了我拥有的自定义帖子类型:

<?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 ); 
    }
}

?>
最后是索引。php只需调用:

<?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(); ?>
任何帮助都将非常非常感谢。

1 个回复
最合适的回答,由SO网友:Craig Pearson 整理而成

您正在指定相同的slug 属于destinations 不幸的是,对于您的自定义帖子类型和分类法,由于WordPress的原因,默认情况下这并不是您所期望的rewrite API. 它迎合了帖子、归档、分类法、slug和WordPress template hierarchy.

如果这个URL结构对您不重要,那么您可以将其中一个slug更改为destinations 另一个是destination. 确保您flush your rewrite rules 这样做之后

或者,如果此URL结构很重要,您可能会发现this answer 有用的

相关推荐

Dropdown menu for categories

当我使用下面的代码时<?php wp_nav_menu( array(\'menu\' => \'categories\' )); ?> 我可以创建一个新的菜单来列出我创建的wordpress中的所有类别。我用它在页面中间列出所有类别。我现在的问题是:有没有一种简单的方法可以为存在的每个子类别创建下拉菜单?那么,当我点击一个特定的类别时,它的子类别会显示出来吗?