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

时间:2016-03-29 15:08:29

标签: php wordpress 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' );

然后,我创建一个名为“single-aran_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(); ?>

我在这里做错了什么?

任何帮助都将不胜感激!

2 个答案:

答案 0 :(得分:1)

您需要重置永久链接here's how to do it

答案 1 :(得分:0)

试试这个:

为您的新帖子类型创建一个新的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();