That’s because you didn’t give your template file the correct name.
http://codex.wordpress.org/Template_Hierarchy
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:
http://codex.wordpress.org/Templates#Template_File_Articles
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.
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
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?
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?
@jonas: nope, this problem only occurs when I use the portfolio CPT in combination with the portfolio page.
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
Ok, and how can I make archive-portfolio show up in the wp_list_pages() list then, so it’ll be in my menu?
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
You are the best, thank you. I will test this asap and let you know if it has worked!
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.
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?
With this code it’s the last page item.