Skip Navigation

[Resolved] Display link to parent term

This thread is resolved. Here is a description of the problem and solution.

Problem:
I create a simple ads site. The custom taxonomy is a simple hierarchy: Ad category > Subcategory. The user can select only one category or subcategory. I need to have several conditionals:

  • - if it's a subcategory, show button back to parent.
  • - if it's a category with subcategories show a button which opens a div with a list from a child view with the subcategories (this was simple but I need this button only in this case). Here's a link to this case: https://produsinbihor.imagomedia.ro/categorie-anunt/legume-zoldsegek/
  • - if it's a category without subcategories show nothing.

Solution:
1. I've solved the last two points in my to-do list using this shortcode:
https://toolset.com/forums/topic/shotcode-to-check-if-a-view-has-output/. If the result is not 0 then it displays the child view.

2. I solved the back to parent button problem too with this solution:
https://toolset.com/forums/topic/how-to-set-condition-on-filter-controls/

Relevant Documentation:

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by jozsefG 5 years, 6 months ago.

Assisted by: Noman.

Author
Posts
#1119769

Hello

I create a simple ads site. The custom taxonomy is a simple hierarchy: Ad category > Subcategory. The user can select only one category or subcategory.

In the category archive I should show a button to the parent term of the selected taxonomy term if it's a subcategory.
Is there a way to achieve this because $parent is only for post parents?

#1120058

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting Toolset support. You can achieve it using custom shortcode that might work.

1. Please add following code in your theme’s or child theme functions.php file

function get_parent_term_func($atts) {
   
    $taxonomy_term_slug = $atts['taxonomy']; 
    $taxonomy_slug = $atts['type'];
	
	$cat_id = get_term_by('slug', $taxonomy_term_slug, $taxonomy_slug); 
    $terms = get_ancestors( $cat_id->term_id , $taxonomy_slug, 'taxonomy' ); 
	if( $terms ) {
        $term = $terms[0];
		$parent_id = get_term_by('id', $term, $taxonomy_slug); 
		return '<a href="'.get_term_link($parent_id->term_id).'" class="btn-parent-cat">'.$parent_id->name.'</a>';
     
    }
}
add_shortcode('get_parent_term', 'get_parent_term_func');

2. Then use following shortcode in category archive view:

[get_parent_term taxonomy='[wpv-post-taxonomy type="category" format="slug"]' type="category"]

⇒ You can change “category” name to your taxonomy/category name

You may also check following functions:
https://codex.wordpress.org/Function_Reference/get_term_by
https://codex.wordpress.org/Function_Reference/get_ancestors
https://developer.wordpress.org/reference/functions/get_term_link/

Please let me know how it goes?

Thank you

#1120640

Unfortunately the shortcode doesn't output anything.

And I have to complicate it further...

I need to have several conditionals:
- if it's a subcategory, show button back to parent
- if it's a category with subcategories show a button which opens a div with a list from a child view with the subcategories (this was simple but I need this button only in this case). Here's a link to this case: hidden link
- if it's a category without subcategories show nothing: I tried several tips from the support forums to check if the child view has items or not but they are not working.

#1120938

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

I am not sure why the shortcode doesn't output anything at your end. However I further tested the code today that I have provided and I found out that this code is not going to work 100% when we have multiple parent and child terms, so we can skip that.

If you want to apply several conditions like you have mentioned in last reply, I am afraid you cannot achieve it using built-in toolset features.

But if you want to give it a try with custom solution then please note this requires a custom code or modifications which falls into Custom coding & custom development and it is out of support policy (https://toolset.com/toolset-support-policy/). So we recommend to contact Toolset recommended service providers to further discuss the custom approach. We have some recommended list of service providers here if you would like to take a look: https://toolset.com/contractors/

-- This may help with the custom code approach:
hidden link

Views API, just in case if you can’t find it:
https://toolset.com/documentation/programmer-reference/views-filters/
https://toolset.com/documentation/programmer-reference/views-api/

Thank you for your understanding.

#1121332

I've solved the last two points in my to-do list using this shortcode from the forum: https://toolset.com/forums/topic/shotcode-to-check-if-a-view-has-output/. If the result is not 0 then it displays the child view.

But I still need that parent link so please help me to solve why that shortcode is not working. It simply outputs nothing. Maybe you could take a look at my admin.

[get_parent_term taxonomy="[wpv-post-taxonomy type='categorie-anunt' format='slug']" type="categorie-anunt"]

And the post type can only have one term assigned, and the shortcode is registered in settings. So it should work I guess...

#1121349

Ok, I solved the back to parent button problem too with this solution from Nigel.
https://toolset.com/forums/topic/how-to-set-condition-on-filter-controls/
Maybe there will be others who need a similar solution.

#1121350

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.