Multiple single.php for the same post
-
Hello,
I am starting a project where I would need different single.php files for the same post, depending on the category. I found some answers based on if(in_category()) but in my case one post may belong to different categories and be displayed in different ways. In fact I’m not sure if categories are what I should use here…
So, what I need is to display one post in 2 different ways, in 2 differents URL, like this
http://www.mysite.com/my-first-category/my-first-post/ > template 1
http://www.mysite.com/my-second-category/my-first-post/ > template 2Any idead how I could do this ?
Thanks very much in advance!
Etienne
-
Hi edelcambre. You should be able to accomplish this using page templates. There are a number of resources available that discuss creating and using page templates. Here are a couple of pages from the WP Theme Handbook:
https://developer.ww.wp.xz.cn/themes/basics/template-hierarchy/
https://developer.ww.wp.xz.cn/themes/template-files-section/page-template-files/page-templates/
Also check this Google search.
Thanks bdbrown for your answer! Though, I am not sure to understand, are you saying my custom posts should not be displayed as custom posts of a category, but within a page ?
My first need is actually to get both URL to display something :
http://www.mysite.com/my-first-category/my-first-custom-post/
http://www.mysite.com/my-second-category/my-first-custom-post/
So far with permalinks I can only get one to exist…Any other clue ?
Thanks again
This might help clarify it:
https://ww.wp.xz.cn/support/topic/using-a-different-template-for-posts-within-a-specific-categoryThanks again. I think maybe I did not expose my problem very well. In fact my main problem may be a URL problem. Once I can get to exist both of those URL I think it will be easy to have separate templates.
In the link you gave me, it has the conditions if(has_term(1)) and if(has_term(2)), but my custom post will have both terms, and I want it to be displayed in two ways, with both URL, for instance :
mysite.com/term-1/my-post > template single-term1-custom-post.php
mysite.com/term-2/my-post > templace single-term2-custom-post.phpTo be more specific, my site is a catalogue of movies. The site is separated in two parts : http://www.mysite.com/france and http://www.mysite.com/international. I thought france and international could be 2 categories. The 2 front pages would be the category-france.php and category-international.php displaying some stuff and the movies of the category, with links to post page displayed differently depending on the active category.
Is there a way to achieve that ?
Thank again for help,OK, I think I have it. You have a post about a movie and it could be assigned both the “France” and “International” categories. Depending on which category archive page the user is visiting, when they click the link for that movie post, you want to display the post content differently based on the caategory page the user was on. Is that it?
The 2 front pages
Not sure if you mean 2 category pages? Your site would have only one front/home page.
Yes, that’s exactly it! Regarding the front pages yes I mean the category pages, they are kind of front page of each two parts of the website.
As for the permalinks, I found some elements on another forum and could achieve something, but not completely, I explained it in that thread …
Thanks again for your time!
I have some notes somewhere but can’t locate them at the moment. Basically what you’d do is set a query argument or query string parameter in the url that’s used to load the single post. In the single post file you would take out the content code and use that to create two new content files, one for France and one for International, and change those layouts however you want. Then, in the single post file where previously you had the content, you use a php “if” statement to check the query parameter or url query string and pull in the related content file. Does that make sense?
Here are a couple of references that might help:
http://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress
http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/
Here’s a Google search that might have some relevant results.
Hello,
Thanks. I think that is what I do, passing an argument, with my query_vars is_france , is_international … The issue I have I think, is with my url_rewrite, which works with articles, but not with custom post types… I think with that code I am close to the solution, do you have a clue how I can get it to work with custom post types ?
function wpd_query_var( $query_vars ) { $query_vars[] = 'is_international'; $query_vars[] = 'is_france'; $query_vars[] = 'is_projections_dvd'; return $query_vars; } add_filter('query_vars', 'wpd_query_var' , 10, 1 ); function wpd_post_rewrite(){ add_rewrite_rule( 'international/([^/]+)/?$', 'index.php?name=$matches[1]&is_international=1', 'top' ); add_rewrite_rule( 'france/([^/]+)/?$', 'index.php?name=$matches[1]&is_france=1', 'top' ); add_rewrite_rule( 'projections_dvd/([^/]+)/?$', 'index.php?name=$matches[1]&is_projections_dvd=1', 'top' ); } add_action( 'init', 'wpd_post_rewrite' ); function wpd_abstract_template( $single_template ){ global $wp_query; //print_r($wp_query); if ( isset( $wp_query->query_vars['is_international'] ) ) { if(in_category('international')) { return locate_template( 'film_international_template.php', false ) ; } else { return locate_template( '404.php', false ) ; } } if ( isset( $wp_query->query_vars['is_france'] ) ) { if(in_category('france')) { return locate_template( 'film_france_template.php', false ) ; } else { return locate_template( '404.php', false ) ; } } if ( isset( $wp_query->query_vars['is_projections_dvd'] ) ) { if(in_category('projections_dvd')) { return locate_template( 'film_projections_dvd_template.php', false ) ; } else { return locate_template( '404.php', false ) ; } } return locate_template( 'single.php', false ) ; } add_filter( 'single_template', 'wpd_abstract_template' );Try using the post_type parameter for the query.
https://codex.ww.wp.xz.cn/Class_Reference/WP_Query#Type_Parameters
http://stackoverflow.com/questions/27422021/getting-only-custom-post-types-posts-with-wp-query
Thank you very much, I could endly achieve it with this code for the url_rewrite !
Thanks again for your timeadd_rewrite_rule( 'france/([^/]+)/?$', 'index.php?name=$matches[1]&post_type=film&categorie=france', 'top' );
The topic ‘Multiple single.php for the same post’ is closed to new replies.