WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [bug?] Custom Post Type name conflict. (25 posts)

  1. Mark Jansen
    Member
    Posted 2 years ago #

    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

  2. Jonas Grumby
    Member
    Posted 2 years ago #

    That's because you didn't give your template file the correct name.

    http://codex.wordpress.org/Template_Hierarchy

  3. Mark Jansen
    Member
    Posted 2 years ago #

    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?

  4. Jonas Grumby
    Member
    Posted 2 years ago #

    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:

    http://codex.wordpress.org/Templates#Template_File_Articles

  5. Mark Jansen
    Member
    Posted 2 years ago #

    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.

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    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: http://www.mysite.com/portfolio
    Custom Post Type archive Page: http://www.mysite.com/portfolio

  7. Jonas Grumby
    Member
    Posted 2 years ago #

    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?

  8. Mark Jansen
    Member
    Posted 2 years ago #

    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?

  9. Mark Jansen
    Member
    Posted 2 years ago #

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

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    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: http://codex.wordpress.org/Template_Hierarchy#Custom_Post_Types_display

    archive-portfolio.php

  11. Mark Jansen
    Member
    Posted 2 years ago #

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

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    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

  13. Mark Jansen
    Member
    Posted 2 years ago #

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

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    I've tested it on my test site. It works but it makes an url like this in wp_list_pages: http://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.

  15. Mark Jansen
    Member
    Posted 2 years ago #

    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?

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    With this code it's the last page item.

  17. Mark Jansen
    Member
    Posted 2 years ago #

    I actually ment if I can set it to like 3rd or so. I'd like it as 3rd item in the menu.

  18. keesiemeijer
    moderator
    Posted 2 years ago #

    I actually ment if I can set it to like 3rd or so. I'd like it as 3rd item in the menu.

    Not with this code.

    You can try it with a custom walker for wp_list_pages in your theme's functions.php or creating your own menu with get_pages() or wp_nav_menu

    See this custom walker. It puts the portfolio archive link after a page with an ID of 39: custom walker

    Call wp_list_pages with this:

    <?php wp_list_pages(array('walker' => new My_Page_Walker())); ?>

    You have to change the Page ID in the walker:
    if($page->ID == 39)

  19. keesiemeijer
    moderator
    Posted 2 years ago #

    I changed the custom walker to put the portfolio link behind a Page with a custom field "portfolio archive". You can put anything for the value of the custom field. This way you don't have to change any code.

    custom walker for wp_list_pages: http://pastebin.com/7L9Wvut2

  20. Mark Jansen
    Member
    Posted 2 years ago #

    Hey,

    awsome the help I'm getting here. Thanks a lot.

    I get everything working just fine, except the walker part. I don't really understand how that works (yet).

    The current code I have to wp_list_pages() is
    <?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?>

    Quite simple, but I'm not sure how to use the walker code in there.

  21. keesiemeijer
    moderator
    Posted 2 years ago #

    try it with:

    <?php
    $args = array(
    'sort_column' => 'menu_order',
    'title_li' => '',
    'walker' => new My_Page_Walker());
    
    wp_list_pages($args);
    ?>
  22. Mark Jansen
    Member
    Posted 2 years ago #

    Works like a charm except for one thing; it shows Portfolio behind the page with the cumstom field, but also at the end of the list.

    I can't really find where it adds it again.

  23. keesiemeijer
    moderator
    Posted 2 years ago #

    Did you remove the previous (first) code that would add the link at the end of wp_list pages. Remove it from functions.php: http://wordpress.org/support/topic/bug-custom-post-type-name-conflict/#post-2519008

    add_filter( 'wp_list_pages', 'new_pages_items' );
    function new_pages_items($items) {
    // rest of code
    }
  24. Mark Jansen
    Member
    Posted 2 years ago #

    Ohw right, that was still in.

    This all works absolutely fantastic now. Thanks for all the help you guys and Keesie in particular.

  25. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. Glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic