Solved

Change CPT single layout based on which taxonomy was clicked

Posted on 2014-10-09
21
106 Views
Last Modified: 2014-12-14
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?
0
Comment
Question by:Alicia St Rose
  • 12
  • 9
21 Comments
 
LVL 15

Expert Comment

by:eemit
ID: 40373041
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

...
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40373588
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

0
 
LVL 15

Expert Comment

by:eemit
ID: 40373652
- Refresh your Permalinks.
- What are the slugs of Healer and Wisdom keeper?
- What are the template names that you have created?
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40373674
- 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
0
 
LVL 15

Expert Comment

by:eemit
ID: 40373713
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.
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40373765
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/
0
 
LVL 15

Expert Comment

by:eemit
ID: 40373822
You have typo:
offerign
should be
offering
in
taxonomy-offerign-wisdom_keeper.php
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40373847
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!
0
 
LVL 15

Expert Comment

by:eemit
ID: 40373873
What happens if you have:
'rewrite' => array('slug' => 'participant'),
instead of:
'rewrite' => array('slug' => 'participant/%offering%'),

Resave permalinks after changing this.
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40373985
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:eemit
ID: 40374907
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.
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40377540
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.
0
 
LVL 15

Expert Comment

by:eemit
ID: 40378197
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.
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40378682
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?
0
 
LVL 15

Expert Comment

by:eemit
ID: 40378719
Yes, you can.
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40379162
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.
0
 
LVL 7

Accepted Solution

by:
Alicia St Rose earned 0 total points
ID: 40380286
Okay,
This seems to be working for me.

1. I created a taxonomy-offering-[term].php file for each taxonomy term.

2. In each file I wrapped the title with a hard coded link and then created a function in to call the term slug.
<a href="<?php bloginfo('url') ?>/participant/performer/<?php echo lucidity_the_slug(); ?>"><?php the_title(); ?></a>

Open in new window

This insures that from that taxonomy page I'll get the participant under the correct URL when visiting the single page.

3. On the single-participant.php I'm calling the post based on a word in the slug
<?php $url = $_SERVER["REQUEST_URI"];

	$isItHealer = strpos($url, 'healer');
	$isItWisdom = strpos($url, 'wisdom');
						
	if ($isItHealer!==false)
	{
	get_template_part('details-participant-healer');
	}
	if ($isItWisdom!==false)
	{
	get_template_part('details-participant-wisdom-keeper');
 } ?>

Open in new window

0
 
LVL 15

Expert Comment

by:eemit
ID: 40383930
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

0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40397355
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.
0
 
LVL 7

Author Comment

by:Alicia St Rose
ID: 40397844
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.
0
 
LVL 7

Author Closing Comment

by:Alicia St Rose
ID: 40498698
This hack ultimately worked for me. I believe it's a slug redirect issue and there's probably a more elegant solution.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to install BuddyPress on your self-hosted WordPress site It’s been called everything from “social networking in a box” to “Facebook without the terms of service,” but until Feb. 16, BuddyPress was a relatively unknown outside the WordPress MU…
How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now