0

I have assigned a custom template on a page in wordpress, but it's not working anymore after I enable permalinks. Instead of the custom template, the "post-type-archive-portfolio" template is used.

This custom template is used to display all posts (custom type posts). I have checked my htaccess file, I tried to enable-disable permalinks again and again but none of these worked.

Here is the template file and the code tha creates the custom post type and taxonomy.

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

Do you have any idea what might cause this?

1 Answer 1

1

It is because of

'has_archive' => true

which instruct wordpress to look for an archive template for the CPT. If you have a page with the same slug (portfolio) it will never get to display it as everything gets priority over pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.