Viewing 11 replies - 1 through 11 (of 11 total)
  • 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.www.ads-software.com/themes/basics/template-hierarchy/

    https://developer.www.ads-software.com/themes/template-files-section/page-template-files/page-templates/

    Also check this Google search.

    Thread Starter edelcambre

    (@edelcambre)

    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 :
    https://www.mysite.com/my-first-category/my-first-custom-post/
    https://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

    Thread Starter edelcambre

    (@edelcambre)

    Thanks 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.php

    To be more specific, my site is a catalogue of movies. The site is separated in two parts : https://www.mysite.com/france and https://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.

    Thread Starter edelcambre

    (@edelcambre)

    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:

    https://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress

    https://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/

    https://codex.www.ads-software.com/Function_Reference/get_query_var

    Here’s a Google search that might have some relevant results.

    Thread Starter edelcambre

    (@edelcambre)

    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' );
    Thread Starter edelcambre

    (@edelcambre)

    Thank you very much, I could endly achieve it with this code for the url_rewrite !
    Thanks again for your time

    add_rewrite_rule(
            'france/([^/]+)/?$',
            'index.php?name=$matches[1]&post_type=film&categorie=france',
            'top'
    );
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Multiple single.php for the same post’ is closed to new replies.