为自定义帖子类型创建模板


Create template for custom post type

我试图为自定义帖子类型创建一个模板,所以我注册了帖子类型:

function aran_create_post_types(){
    register_post_type('aran_agencies',
        array(
            'labels' => array(
                'name'          => __( 'Agencies', 'aran' ),
                'singular_name' => __( 'Agencies', 'aran' ),
                'add_new_item'  => __( 'Add new agency', 'aran' ),
            ),
            'public'        => true,
            'has_archive'   => true,
            'supports'      => array(
                'title',
                'editor',
                'thumbnail',
                'custom-fields',
            ),
        )
    );
}
    add_action( 'init', 'aran_create_post_types' );

然后,我创建了一个名为"singlearan_agencies.php"的文件,并放入以下代码:

echo "this is single agency page";

但当我试图查看一个机构的帖子时,我会得到404页。归档页面也是如此。这是我的single.php代码,它是我从父主题继承的

get_header(); 
$show_sidebar = get_post_meta($post->ID, 'show_sidebar_checkbox', true);
if ( $show_sidebar == 'yes' ):
    $bootstrap_sidebar_dep = 'col-sm-8';
else:
    $bootstrap_sidebar_dep = 'col-sm-12';
endif;
?>
<div class="container">
    <div class="row">
    <div id="primary" class="content-area <?php echo apply_filters('primary_bootstrap_class', $bootstrap_sidebar_dep); ?>">
        <div id="content-top-wa" class="widget-area">
        <?php dynamic_sidebar('content-top') ?>
        </div><!-- #content-top-wa -->
        <main id="main" class="site-main" role="main">
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', 'single' ); ?>
            <?php faster_post_nav(); ?>
            <?php
                // If comments are open or we have at least one comment, load up the comment template
                if ( comments_open() || '0' != get_comments_number() ) :
                    comments_template();
                endif;
            ?>
        <?php endwhile; // end of the loop. ?>
        </main><!-- #main -->
        <div id="content-bottom-wa" class="widget-area">
        <?php dynamic_sidebar('content-bottom') ?>
        </div><!-- #content-bottom-wa -->
    </div><!-- #primary -->
<?php 
    if ( $show_sidebar == 'yes' ):
        get_sidebar();      
    endif;
 ?>
    </div><!-- .row -->
</div><!-- .container -->
<?php get_footer(); ?>

我在这里做错了什么?

任何帮助都将不胜感激!

您需要重置永久链接,以下是操作方法。

试试这个:

为你的新帖子类型创建一个新的WP_Query,这样你就可以添加:

// Args for your Post Type:
$args = array (
    'post_type' => array( 'aran_agencies' ),
);
// The New Query
$agencies = new WP_Query( $args );

然后你可以替换:

while ( have_posts() ) : the_post();

while ( $agencies->have_posts() ) : $agencies->the_post();