0

I have a custom post type called "project" and an accompanying single view template labeled single-project.php On the landing page for each project, I have sub-navigation that points to separate sub-sections that query related items such as "Related Projects" and "Related Resources". Is there a way I can create templates to handle these pages based on the path and custom post type? For instance, if a project is labeled "Test Project" and the sub-nav URL points to test-project/related-resources, is there a way for me to create a template that would handle related-resources automatically by referencing the path/slug along with the custom post type?

Thanks for any insight here.

3
  • How are these items related to project, you select them manually for each project? Or are you using custom taxonomy? And are related resources another custom post type or some manually entered links in the project metadata?
    – Greg36
    Commented Nov 28, 2017 at 23:31
  • Look up rewrite endpoints.
    – Milo
    Commented Nov 29, 2017 at 1:07
  • @Greg36 For "related-resources" for instance, there are categories defined that correspond to types of projects- so could I configure the template to somehow reference the category? Thus the template would handle /project-name/related-resources/ to show that category of resource.
    – nickpish
    Commented Nov 29, 2017 at 1:20

1 Answer 1

1

You can't set a project as a base of permalink because you would have to enter rewrite rules for each one.

What you can do is create a custom taxonomy related-resources:

function wpse_287202_related_resources_taxonomy() {

    $labels = array(
       ...
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'related-resources', array( 'product', 'post', 'page' ), $args );

}
add_action( 'init', 'wpse_287202_related_resources_taxonomy', 0 );

It needs to be associated with your product custom post type and posts, pages or other custom post types you want to add to related resources. You do that by changing the second parameter of register_taxonomy.

In your product custom post type add related-resources to taxonomies:

'taxonomies' => array( 'related-resources' ),

Now when you edit project add "Amazing Project 17" to related resources taxonomy the same way as you would add a category and check it just for the project it relates to. In this setup, you can't have a project be a related resource to other project but that could be changed.

In every post, page or custom post type other than project you associated with the taxonomy, you can select "Amazing Project 17" and it will be displayed as a related resource for that particular project.


Now you need to display it all and to know what template to use check WP Hierarchy. Related resources will be displayed using taxonomy-related-resources.php file. There just make sure to exclude project from displaying as it is part of the related resources taxonomy, do it by checking get_post_type().

To display link to related resources on the project page use:

$term = get_the_terms( $post->ID, 'related-resources' )[0];
echo '<a href="' . esc_url( get_term_link( $term, 'related-resources' ) ) . '">' . __( 'Related Resoucrces', 'text_domain' ) . '</a>';

If things seem confusing read more about custom taxonomies and how they can be used with custom post types.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.