启用固定链接将禁用自定义页面模板

时间:2014-10-13 作者:ktsixit

我在wordpress的一个页面上分配了一个自定义模板,但在启用永久链接后,它就不再工作了。使用“post type archive portfolio”模板代替自定义模板。

此自定义模板用于显示所有帖子(自定义类型帖子)。我已经检查了我的htaccess文件,我一次又一次地尝试启用禁用永久链接,但这些都不起作用。

这是创建自定义帖子类型和分类的模板文件和代码。

Custom post type creation:

/* Portfolio Custom Posts */
    register_post_type( \'portfolio\', /* this can be seen at the URL as a parameter and a unique id for the custom post */
        array(
            \'labels\' => array(
                \'name\' => __( \'Portfolio\',\'textdomain_simple\' ), /* The Label of the custom post */
                \'singular_name\' => __( "All Portfolio", \'textdomain_simple\' ) /* The Label of the custom post */
            ),
            \'public\' => true,
            \'has_archive\' => true,
            \'rewrite\' => array(\'slug\' => \'portfolio\'), /* The slug of the custom post */
            \'supports\' => array( \'title\', \'thumbnail\', \'editor\', \'custom-fields\', \'revisions\' ), /* enable basic for text editing */
        )
    );

/* Portfolio Taxonomies/Categories */
function portfolio_taxonomie() {

    register_taxonomy(
        \'portfolio_category\',
        array( \'portfolio\' ),
        array(
            \'public\' => true,
            \'show_ui\' => true,
            \'show_tagcloud\' => false,
            \'hierarchical\' => true,
            \'with_front\' => false,
            \'labels\' => array(
                \'name\' => __( \'Portfolio Category\', \'textdomain_topbest\' ),
                \'singular_name\' => __( \'Porfolio Category\', \'textdomain_topbest\' )
            ),
        )
    );
    register_taxonomy_for_object_type( \'post_tag\', \'portfolio\' );
}
add_action( \'init\', \'portfolio_taxonomie\', 0 );

function custom_post_type_tags( $query ) {
    if ( !is_admin() && $query->is_tag() && $query->is_main_query() ) {
        $query->set( \'post_type\', array( \'post\', \'portfolio\' ) );
    }
}
add_action( \'pre_get_posts\', \'custom_post_type_tags\' );

Template file:

<?php /* Template Name: Portfolio Page */ ?>
<?php get_header(); ?>

   <!-- Start main content -->
   <div class="container main-content clearfix">

       <div id="intro">
          <blockquote><?php the_field(\'intro_message\'); ?></blockquote>
        </div>



        <div class="portfolio">

        <div class="sixteen columns">  
          <div class="title clearfix" id="options">
            <span>Filter:</span>
            <ul id="filters" class="option-set clearfix" data-option-key="filter">
              <li><a href="#filter" data-option-value="*" class="selected">All Projects</a></li>
              <?php
                $terms = get_terms("portfolio_category");
                $count = count($terms);
                    if ( $count > 0 ){
                    foreach ( $terms as $term ) {
                    echo \'<li><a href="#filter" data-option-value=".\'.$term->slug.\'">\'.$term->name.\'</a></li>\';

                }
                    }
                ?>
            </ul>
          </div>
        </div> <!-- End options -->

        <div class="clearfix"></div>

        <div id="contain"> 

        <!-- =================================================== -->

        <?php
        $args = array( \'numberposts\' => -1, \'order\'=> \'DESC\', \'post_type\' => \'portfolio\');
        $postslist = get_posts( $args );
        foreach ($postslist as $post) :  setup_postdata($post);

        $terms = get_the_terms( $post->ID, \'portfolio_category\' );                  
            if ( $terms && ! is_wp_error( $terms ) ) : 
                $term_links = array();
            foreach ( $terms as $term ) {
                $term_links[] = $term->slug;
                    }                   
                $the_term = join( " ", $term_links );
                $the_term_separated = join( ", ", $term_links );
        ?>

        <?php if( has_post_thumbnail() ){ ?>
        <!-- Start Portfolio Item -->
          <div class="one-third column item element-3 <?php echo $the_term; ?>" data-categories="<?php echo $the_term; ?>">
              <a href="<?php echo get_permalink(); ?>">
                <?php the_post_thumbnail(\'portfolio-thumb\'); ?>
              </a>
              <div class="info">
                <h3><?php echo vp_metabox(\'portfolio_option.list_title\'); ?>.</h3> 
                <a class="more" href="<?php echo get_permalink(); ?>"> Find out more »</a>  
              </div>            
          </div>
          <!-- End Portfolio Item -->
          <?php } ?>

         <?php endif; ?> 
         <?php wp_reset_query(); endforeach; ?>

          <!-- =================================================== -->

        </div><!-- End contain-->

        </div><!-- End portfolio -->

   </div><!-- <<< End Container >>> -->

<?php get_footer(); ?>
你知道这是什么原因吗?

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

这是因为

\'has_archive\' => true
它指示wordpress查找CPT的存档模板。如果您有一个具有相同slug(公文包)的页面,它将永远无法显示它,因为所有内容都优先于页面。

结束

相关推荐

Custom permalinks structure

我希望有这样的结构:www.mysite.com/2013 (必须显示2013年的所有职位)www.mysite.com/my-category/2013 (必须显示2013年和“我的类别”类别的所有帖子)www.mysite.com/my-category/my-tag/ (必须显示所有类别为“我的类别”和标记为“我的标记”的帖子)www.mysite.com/my-category/ (必须显示“我的类别”类别的所有帖子)www.mysite.com/my-