Link to home
Start Free TrialLog in
Avatar of Alicia St Rose
Alicia St RoseFlag for United States of America

asked on

Change CPT single layout based on which taxonomy was clicked

I'm not even sure this can be accomplished.

1. I've got 1 custom post type: Participant.

2. I've created custom taxonomy called: Offering which consist of Artist, Musician, Wisdom Keeper, Performer, Healer.

3. I've created a custom template to display each taxonomy. Eg: musician-list.php, artist-list.php. These can be turned on for the appropriate pages. (I decided not to use the taxonomy archive template. It's easier for my client to work with the menu when they can see a page.)

Now here is my quandary: Some of the participants can have more than one offering. Each offering has a different page layout. Is there a way to show a different page layout on the single CPT depending on which taxonomy was clicked?

For instance when I'm looking at the healer list and click from that list, I get the single CPT healer layout and if I'm looking at the artist list and see the same participant, clicking from the artists list gives me the single artist layout.

Can this be done?
Avatar of eemit
eemit
Flag of Germany image

Try to create taxonomy templates as:
taxonomy-offering-healer.php

where offering = Offering taxonomy slug
and healer = Healer term slug

and
taxonomy-offering-artist.php

where offering = Offering taxonomy slug
and artist = Artist term slug

...
Avatar of Alicia St Rose

ASKER

Hi eemit,
I just made the templates and it didn't help.
Here's the issue: I have one participant with two offerings: Healer, Wisdom keeper.
I see him appearing in both archives. I click to got to single page and no matter which archive, the slug shows up with healer.

I think this may be a rewrite issue and I'm pretty green in that area.

Here's my code for registering the post types and the taxonomies:

function lucidity_create_offering_taxonomies() {

	// Add new taxonomy, make it hierarchical (like categories)
	$topic_labels = array(
		'name' => _x( 'Offering', 'taxonomy general name' ),
		'singular_name' => _x( 'Offering', 'taxonomy singular name' ),
		'search_items' =>  __( 'Search Offerings' ),
		'all_items' => __( 'All Offerings' ),
		'parent_item' => __( 'Parent Offering' ),
		'parent_item_colon' => __( 'Parent Offering:' ),
		'edit_item' => __( 'Edit Offerings' ),
		'update_item' => __( 'Update Offering' ),
		'add_new_item' => __( 'Add New Offering' ),
		'new_item_name' => __( 'New Offering Name' ),
	); 	

	register_taxonomy( 'offering', array( 'participant' ), array(
		'hierarchical' => true,
		'labels' => $topic_labels, /* NOTICE: Here is where the $labels variable is used */
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'offering' ),
	));
	
}

function lucidity_create_participant_post_type() {
    $args = array(
                  'description' => 'Participant Post Type',
                  'show_ui' => true,
                  'query_var' => true,
                  'menu_position' => 4,
                  'exclude_from_search' => false,
                  'labels' => array(
                                    'name'=> 'Participants',
                                    'singular_name' => 'Participants',
                                    'add_new' => 'Add New Participant',
                                    'add_new_item' => 'Add New Participant',
                                    'edit' => 'Edit Participants',
                                    'edit_item' => 'Edit Participant',
                                    'new-item' => 'New Participant',
                                    'view' => 'View Participant',
                                    'view_item' => 'View Participant',
                                    'search_items' => 'Search Participants',
                                    'not_found' => 'No Participants Found',
                                    'not_found_in_trash' => 'No Participants Found in Trash',
                                    'parent' => 'Parent Participant'
                                   ),
                 'public' => true,
                 'publicly_queryable' => true,
                 'capability_type' => 'post',
                 'hierarchical' => false,
                 'rewrite' => array('slug' => 'participant/%offering%'),
                 'has_archive' => true,
                 'supports' => array('title', 'editor')
                 );
    register_post_type( 'participant' , $args );
}

Open in new window

- Refresh your Permalinks.
- What are the slugs of Healer and Wisdom keeper?
- What are the template names that you have created?
- Refresh your Permalinks.
Check!
But no change.

- What are the slugs of Healer and Wisdom keeper?
healer
wisdom-keeper

- What are the template names that you have created?
taxonomy-offering-healer.php
taxonomy-offerign-wisdom-keeper.php
Try to change the following:
wisdom-keeper
to:
wisdom_keeper
and:
taxonomy-offerign-wisdom-keeper.php
to:
taxonomy-offerign-wisdom_keeper.php

After that, refresh Permalinks.
This didn't affect anything either.
I still click on the participant from wisdom keeper archive and the single post slug has him in healer.
It should look like this:

http://localhost:8888/participant/wisdom_keeper/dr-mike-north/

but instead looks like this:

http://localhost:8888/participant/healer/jesse-carrieri/
You have typo:
offerign
should be
offering
in
taxonomy-offerign-wisdom_keeper.php
Sorry,
The typo is only here on this forum.
It's correct in the file name.

I really think this has something to do with rewrite rules for my taxonomies or post types. Maybe End Points API or Rewrite API. I'm trying to dig through this stuff, but man, am I finding it complicated!
What happens if you have:
'rewrite' => array('slug' => 'participant'),
instead of:
'rewrite' => array('slug' => 'participant/%offering%'),

Resave permalinks after changing this.
Strange behavior after doing this:

In the menu I've created custom link:

http://localhost:8888/participant/offering/wisdom-keepers

When I click on the menu item the URL changes to this:

http://localhost:8888/wisdom-keepers/ 

I would like the URL to ultimately look like this:

http://localhost:8888/particpant/offering/wisdom-keepers/jesse-carrieri

or at the very least:

http://localhost:8888/offering/wisdom-keepers/jesse-carrieri
Sorry laughhearty, there are not single taxonomy templates.
These templates are not for single posts but for taxonomy archives
taxonomy-offering-healer.php
taxonomy-offering-wisdom-keeper.php

Is there a way to show a different page layout on the single CPT depending on which taxonomy was clicked?
Yes, to pull in a different single sub-template, depending on the Custom Taxonomy Term Slug, you can do the following:

Create this file:
archive-participant.php
or use your files:
musician-list.php, artist-list.php

Create this file:
single-participant.php


Create this files:
content-participant.php
content-offering-healer.php
content-offering-wisdom-keeper.php
content-offering-performer.php

Then in your single-participant.php insert this code section:

<?php
// Start the Loop.
while (have_posts()) : the_post();

		/**
		 * Pull in a different single sub-template, depending on the Custom Taxonomy Term Slug
		 */
		if ( '' != get_the_content() )

			$terms = wp_get_object_terms( $post->ID, 'offering' );
			if( $terms ){
				switch ($terms[0]->slug) {
					case "healer":
						get_template_part( 'content', 'offering-healer' );
						//require( 'content-offering-healer.php' );
						break;
					case "wisdom-keeper":
						get_template_part( 'content', 'offering-wisdom-keeper' );
						break;
					case "performer":
						get_template_part( 'content', 'offering-performer' );
						break;
					default:
						get_template_part( 'content', 'participant' );
				}
			} else {
				get_template_part( 'content', 'participant' );
				//require( 'content-participant.php' );//or using require
			}

		// Previous/next post navigation.
		//twentyfourteen_post_nav();

		// If comments are open or we have at least one comment, load up the comment template.
		//if ( comments_open() || get_comments_number() ) {
		//	comments_template();
		//}

	endwhile;
?>

Open in new window

Rewrite slugs are other thing, it is better to open another Question for that.
Before I attempt this, what happens when one participant has two taxonomy terms?

How will this conditional discern which one was clicked from the archive?

That is the gist of the problem. Many of the participants where more than one hat. This is my original question.

Your suggestion is making template files and adding code.
If rewrite is the answer, then I don't see the need to start another thread.
You need: Change CPT single layout based on which taxonomy was clicked?
How will this conditional discern which one was clicked from the archive?
In your archive you have linked post titles.
This conditional should be in single-participant.php.
Please try what I have posted.
I'm not using the archive-participant.php in this theme. The Participants are only shown by taxonomy = offering.

So, can I use the taxonomy.php or taxonomy-offering.php instead of archive-participant.php in your example?
Yes, you can.
Hi eemit,
I tried you suggestion and I'm still getting A participant who has two terms only showing up in the first term selected no matter which taxonomy archive I'm viewing.

I'm in the process of a work-around that is yielding the results I'm after, I'll post it once I'm finished.
ASKER CERTIFIED SOLUTION
Avatar of Alicia St Rose
Alicia St Rose
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check if you can use a more elegant solution e.g. 'single_template' WP filter.

The "single_template" filter can be used to load a custom template for a given post.
It will replace the template used whenever the "single" template is called.
Filter function must return the a full path to a template file.

Try this in your child theme's functions.php file:
add_filter( "single_template", "yourprefix_get_cpt_template" );
function yourprefix_get_cpt_template( $single_template ) {
     global $post;

     if ($post->post_type == 'participant') {
          $single_template = get_stylesheet_directory() . '/your-single-cpt-template.php';
     }
     return $single_template;
}

Open in new window

Hi  eemit,
I'm going to give this a try. I'm not using a child theme.
I use Underscores starter theme. So, I'll be changing get_stylesheet_directory() to get_template_directory().

I also have 5 templates in total.
Hi eemit,
actually, I'm not sure your solution will work.
Getting different templates for the single CPT page isn't the problem. It's making sure the URL is correct after clicking from the taxonomy archive. When a participant has more than one taxonomy applied the second one is ignored in the URL struction no matter which taxonomy archive I click from.

That's why, after some research, I'm certain this is a redirect issue. So I basically had to hardcode the link into the loop in the different taxonomy templates.
This hack ultimately worked for me. I believe it's a slug redirect issue and there's probably a more elegant solution.