1

The situation

I have a custom post type named: Projects.

Within a project I added a few tags to split the content.

I do this to create chapters within the project;

  1. Intro (index)
  2. Design
  3. Build
  4. Result

I created rewrite rules to display the urls (project/project-name/design)

Then I register get_query_var and return the required template and show content with the explode function.

The problem

There will be multiple projects with the same chapters:

  1. project/project-x/design
  2. project/project-y/design

Whenever I add a second project (y) and I view project/project-x/design I see the content of (y) the latest post. Also for /build and /result. Index/intro is fine and shows the correct content.

So whenever I do an url request of /design, /build, /result it pulls the latest post.

Could you please help me out and tell me what I am missing? File structure is below

Files:

single-project.php - Intro/Index

<?php
    $chapters = apply_filters('the_content', get_the_content($post_id));
    $chapter = explode('<!--nextpage-->', $chapters);
    echo $chapter[0];
?>

single-project-design.php - Design Chapter

<?php
    $chapters = apply_filters('the_content', get_the_content($post_id));
    $chapter = explode('<!--nextpage-->', $chapters);
    echo $chapter[1];
?>

project-chapters.php - Functions to create routes & templates

// ADD REWRITE RULES
 function project_rewrite_rule() {
  add_rewrite_rule( 'project/([^/]+)/design', 'index.php?post_type=projects&project=$matches[1]&design=yes', 'top' );
  add_rewrite_rule( 'project/([^/]+)/build', 'index.php?post_type=projects&project=$matches[1]&build=yes', 'top' );
  add_rewrite_rule( 'project/([^/]+)/result', 'index.php?post_type=projects&project=$matches[1]&result=yes', 'top' );
 }

 add_action( 'init', 'project_rewrite_rule' );

 // REGISTER QUERY VARS
 function register_query_vars( $vars ) {
    $vars[] = 'design';
    $vars[] = 'build';
    $vars[] = 'result';

    return $vars;
 }

 add_filter( 'query_vars', 'register_query_vars' );

 // LOAD TEMPLATES ON URL
 function url_rewrite_templates() {

     if ( get_query_var( 'design' ) ) {
         add_filter( 'template_include', function() {
             return get_template_directory() . '/single-projects-design.php';
         });
     }

     if ( get_query_var( 'build' ) ) {
         add_filter( 'template_include', function() {
             return get_template_directory() . '/single-projects-build.php';
         });
     }

        if ( get_query_var( 'result' ) ) {
                add_filter( 'template_include', function() {
                        return get_template_directory() . '/single-projects-result.php';
                });
        }
 }

 add_action( 'template_redirect', 'url_rewrite_templates' );
  • two quick questions: 1) what is the behavior with only one project post? 2) if you add a third (z), do you now see the content of (z) for both (x) and (y)? Or do you see the content of (y) for (x) and (z)? – hwl May 27 '17 at 14:22
  • A few things- get_the_content does not accept a post id. template_redirect is for redirection, and is an action, actions don't return anything. Use template_include or single_template filter. – Milo May 27 '17 at 16:18
  • @hwl 1) one project post works fine. When I create a new one, that content will be displayed. 2) when I add (z) that content will be shown for (x) & (y). Always the latest post gets displayed. – Luke van Doorn May 27 '17 at 17:28
  • @Milo Your answer on this topic did the trick. Thanks a ton! wordpress.stackexchange.com/questions/104955/… – Luke van Doorn May 29 '17 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.