• Resolved Mark Jansen

    (@mark-jansen)


    Hello everybody,

    I have a problem with Custom Post Types and pages names. Apparently this poses a conflict between them.

    As it seems a CPT can not have the same name as a template file or a page. I can’t seem to figure out why that is though. I foun some blogs and articles, but none of them really explains why this is, or what the solution for it is.

    What’s the case
    I want to create a portfolio page. For that I created a CPT with Custom Post Type UI. I named that CPT portfolio. So far so good.

    After that I create a page called porftolio and a template named portfolio.php

    portfolio.php

    <?php
    	$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 10));
    	while ( $loop->have_posts() ) : $loop->the_post();
    		the_title();
    		echo '<div class="entry-content">';
    		the_content();
    		echo '</div>';
    	endwhile;
    ?>

    So the page Portfolio will get the template of portfolio.php.

    The problem
    Apparently WordPress can’t handle it if the CPT and page have the same name. Perhaps some one can explain why, because I can’t seem to find it.

    What happens if I navigate to my portfolio page? It skips every template file and goes straight to index.php. Something I of course don’t want to see.

    I’m not sure if that’s what it’s meant for, but I have tried an archive-posttype template, but that doesn’t seem to work either.

    If I rename the CPT to for instance projects, I can display the CPT’s on the page Portfolio, but as soon as I use the the_permaling(); I go to mysite.com/projects/singlecpt, and I don’t want that. I want it to lead to mysite.com/portfolio/singlecpt

    So, now what?
    Well I’m hoping some one can help me out with this. I need a nudge in the right direction. I can’t seem to put my finger on it why it won’t work. All I want is a simple portfolio page with Custom Post Types. There must be a way to make this work, right? Or is this a massive bug in WordPress?

    Thanks.

    Mark

Viewing 15 replies - 1 through 15 (of 24 total)
  • That’s because you didn’t give your template file the correct name.

    https://codex.www.ads-software.com/Template_Hierarchy

    Thread Starter Mark Jansen

    (@mark-jansen)

    Edit: Thanks for the answer of course.

    What name should my template have then?

    I know the hierarchy, but I’m not sure which way I should go then.

    The template page for displaying CPT’s should be singular page -> static page -> $custom.php right? Or am I following the wrong track?

    page-portfolio.php Would be the template used for a page called Portfolio. In that template you could have your custom loop code.

    Also, it seems like your template should contain more than just the code you have pasted here. Often the easiest thing to do is make a copy of page.php, name it page-portfolio.php and modify it as needed.

    Lots of good info here:

    https://codex.www.ads-software.com/Templates#Template_File_Articles

    Thread Starter Mark Jansen

    (@mark-jansen)

    Hello Jonas,

    once again, thanks.

    My template contains more code for the header and the footer.

    I’ve tried to name it page-portfolio.php, but alass that still doesn’t work.

    If I navigate to the portfolio page, it gives current-page-item to the page I selected as blogpage where it of course should give that status to Portfolio. It’s apparently still using index.php.

    Moderator keesiemeijer

    (@keesiemeijer)

    Apparently WordPress can’t handle it if the CPT and page have the same name. Perhaps some one can explain why, because I can’t seem to find it.

    That’s because the urls will be the same:
    Page: https://www.mysite.com/portfolio
    Custom Post Type archive Page: https://www.mysite.com/portfolio

    If I navigate to the portfolio page, it gives current-page-item to the page I selected as blogpage where it of course should give that status to Portfolio.

    That sounds like a problem with your menu code, which would be a separate problem. Do you have other pages that the problem doesn’t happen on?

    Thread Starter Mark Jansen

    (@mark-jansen)

    Hey Keesie,

    thanks for the answers. That makes absolutely a lot of sense, but is there a workaround for it?

    Is there a way to make a nice portfolio overview, with all the CPT’s (with pagination), and have it show up in the wp_list_pages?

    Thread Starter Mark Jansen

    (@mark-jansen)

    @jonas: nope, this problem only occurs when I use the portfolio CPT in combination with the portfolio page.

    Moderator keesiemeijer

    (@keesiemeijer)

    The only workaround I know is to change one of the urls to something else. So you want to show all (10) custom post type posts on the portfolio “Page”. You don’t need to make a “Page” for that. Look at the template hierarchy what template’s you can use: https://codex.www.ads-software.com/Template_Hierarchy#Custom_Post_Types_display

    archive-portfolio.php

    Thread Starter Mark Jansen

    (@mark-jansen)

    Ok, and how can I make archive-portfolio show up in the wp_list_pages() list then, so it’ll be in my menu?

    Moderator keesiemeijer

    (@keesiemeijer)

    This will add the portfolio archive page to wp_list_pages:

    add_filter( 'wp_list_pages', 'new_pages_items' );
    function new_pages_items($items) {
        global $wp_query;
        $class ='';
    
        //Checks if we are viewing CPT 'portfolio', if so give it the 'active' class.
        if(isset($wp_query->query_vars['post_type'])&& $wp_query->query_vars['post_type']=='portfolio')
            $class = 'current_page_item';
    
        //This generates the url of the CPT archive page
        $url = add_query_arg('post_type','portfolio',site_url());
    
        $myitem = '<li class="'.$class.'"><a href="'.$url.'">portfolio</a></li>';
    
        $items = $items . $myitem;
        return $items;
    }

    Put it in your theme’s functions.php

    Thread Starter Mark Jansen

    (@mark-jansen)

    You are the best, thank you. I will test this asap and let you know if it has worked!

    Moderator keesiemeijer

    (@keesiemeijer)

    I’ve tested it on my test site. It works but it makes an url like this in wp_list_pages: https://www.yoursite.com/?post_type=portfolio
    change this (if you’re not using the default permalink structure):

    $myitem = '<li class="'.$class.'"><a href="'.$url.'">portfolio</a></li>';

    to (depending on your permalink structure):

    $myitem = '<li class="'.$class.'"><a href="/portfolio">portfolio</a></li>';

    To get pagination working on archive-portfolio.php I don’t use a query on the loop but use this in my theme’s functions.php:

    function my_custom_posts_per_page( &$q ) {
          if ( $q->is_archive ) // any archive
          if($q->query_vars['post_type'] == 'portfolio'){  //custom post type "portfolio" archive
                   $q->set( 'posts_per_page', 10 );
          }
          return $q;
      }
    
    add_filter('parse_query', 'my_custom_posts_per_page');

    set 'posts_per_page', 10 to how many posts you want on this page.

    Thread Starter Mark Jansen

    (@mark-jansen)

    Ok thanks. I can’t get around it to test it right now, but I will tomorrow. Is there also a way to determine what position ‘Portfolio’ should have in the menu? Can I influence that?

    Moderator keesiemeijer

    (@keesiemeijer)

    With this code it’s the last page item.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘[bug?] Custom Post Type name conflict.’ is closed to new replies.