• Hi,

    i’m currently developing a plugin and have created a custom post type named “player”.

    I want to create two static subpages for every player-post.

    Currently i’ve got the following structure:

    xyz.com/team/player1/
    xyz.com/team/player2/
    xyz.com/team2/player1/
    xyz.com/team2/player2/

    Is it possible to create the static subpages for every post with given templates without having to create the subpages manually?

    So that it looks like:

    xyz.com/team/player1/single/
    xyz.com/team/player1/double/
    xyz.com/team/player2/single/
    xyz.com/team/player2/double/
    .
    .
    .

    greetings

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi!

    It is actually very easy to do. Since you are adding just one custom keyword to the URL (after what seems the slug for your custom post type and the name of the post), the 3rd part of your url can be implemented as an additional query_var. You then use rewrite_rules to pick up that value from your URL. Last, in the template for your custom post type (somewhat like player-single.php), load a custom template based in the query var.

    Let’s say, you put a custom action in your custom post type single template, which, according to the value from the query_var (and this from your URL, according to your rewrite rule), loads the template you make for display of the subpage for the specific player post.

    <?php wp_head(); ?></head>
    <?php switch(get_query_var('subpage')) {
        case 'single':
            get_template_part('player', 'single');
            break;
        case 'double':
            // your implementation
        case default:
            break; // don't take everything your users could throw in the URL
    } ?>
    <?php wp_footer(); ?>
    Thread Starter Glur4k

    (@glur4k)

    Hi,

    thanks for your answer!

    Just some additional information:
    My CPT is named ‘rp_spieler’ and has a taxnomony ‘rp_spieler_category’.
    The current URL is xyz.com/’rp_spieler_category’/’rp_spieler’/ which is actually the structure mentioned in my first post:

    xyz.com/team/player1/ OR
    xyz.com/team2/player1/

    I’ve added some code like you said and it is now:

    add_filter('query_vars', 'rp_spieler_query_vars', 10, 1);
    function rp_spieler_query_vars($qvars) {
      array_push($qvars, 'bilanzen');
      return $qvars;
    }
    
    add_action('init', 'rp_spieler_bilanzen_add_url');
    function rp_spieler_bilanzen_add_url() {
      add_rewrite_rule(
        '^mannschaften/([^/]+)/([^/]+)/([^/]+)/?',
        'index.php?pagename=mannschaften&mannschaften=$matches[1]&rp_spieler=$matches[2]&bilanzen=$matches[3]',
        'top');
    }

    and in my player-single i’ve added the code you wrote.

    Now everything works like before and if i have a link on my single-post-template like ‘…/teamX/playerA/single/

    a blank page is shown. (my wordpress theme standard page without content).

    Do you have an idea?

    you need to add your custom query var in functions.php!

    // custom query vars
    function add_query_vars_filter( $vars ){
      $vars[] = "subpage";
      return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_filter' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create static subpage for a custom post type’ is closed to new replies.