• 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 9 replies - 16 through 24 (of 24 total)
  • Thread Starter Mark Jansen

    (@mark-jansen)

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

    Moderator keesiemeijer

    (@keesiemeijer)

    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)

    Moderator keesiemeijer

    (@keesiemeijer)

    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: https://pastebin.com/7L9Wvut2

    Thread Starter Mark Jansen

    (@mark-jansen)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    try it with:

    <?php
    $args = array(
    'sort_column' => 'menu_order',
    'title_li' => '',
    'walker' => new My_Page_Walker());
    
    wp_list_pages($args);
    ?>
    Thread Starter Mark Jansen

    (@mark-jansen)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    Did you remove the previous (first) code that would add the link at the end of wp_list pages. Remove it from functions.php: https://www.ads-software.com/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
    }

    Thread Starter Mark Jansen

    (@mark-jansen)

    Ohw right, that was still in.

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

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Glad you got it resolved.

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