繁体   English   中英

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

[英]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' );

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

我在这里做错了什么?

任何帮助将不胜感激!

您需要重置永久链接, 这是操作方法

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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