Skip Navigation

[Resolved] How to show only specific category if all category selected?

This support ticket is created 5 years, 9 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 9 replies, has 3 voices.

Last updated by Himanshu Agarwal 5 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1199166

Hi,

I need a help in category solution that if all categories are selected then only a specific category show in page template via [wpv-post-taxonomy type="product_cat"] or only top most category show.

This mean
if (all_category_selected){
print ("specific category" or "top most category")
}else{
print ("only selected category")
}

Is this possible to achieve in toolset?
Please help me to achieve this.

#1199238

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

What's the context for this?

Say your product_cat taxonomy has 10 terms.

You are displaying a post (presumably a product) with a template.

If this particular post has 2 or 3 terms assigned then you want to output those terms using the wpv-post-taxonomy shortcode.

But if this particular post has all 10 terms assigned then you want to not display the list of terms and instead display something else.

Is that a correct description?

What should the something else be? (What would "top most category" be, for example?)

#1199713

Okay lets assume we have below categories.

  • Top most category
  • Automotive
    • Cars
  • Business
    • Finance
    • Online Marketing
    • Work from Home
  • Construction
  • Education
  • Entertainment
    • Celebrity
    • Movies
    • Music

If we selected all category then [wpv-post-taxonomy type="product_cat"] display only "Top most category" (Parent of all).

and if we selected any category but not all (like business and education) then [wpv-post-taxonomy type="product_cat"] display only selected category (like business and education).

Is this possible? If yes then how to achieve this?

#1199758

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There isn't any way to filter the output of a Views shortcode like wpv-post-taxonomy, so the problem would be that you want it to output normally in most cases, but not output anything at all in the particular case where all categories were selected so that you could then output something else (i.e. the top level category).

So you have two options.

One is that you will need to write a custom shortcode which essentially does the same as wpv-post-taxonomy but has the logic built-in to only display the top-level category when required.

The other is to use conditional shortcodes.

You need to compare the output of wpv-post-taxonomy to the list of all the categories, and if they are the same output the top level category directly as a string, and if they are different then output wpv-post-taxonomy as normal.

The best way to generate the list of all categories to include in the wpv-conditional shortcode is to create a post that has all of the categories selected and display it using a template which includes wpv-post-taxonomy so that you can copy and paste the results into your conditional shortcode test.

#1199897

Yes, you are right that second option is better.

Nigel, can you please guide me how to achieve the second option that you mentioned above, with sample code or sample short code.

#1199903

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Let me get back to you a little later on this.

We have an imminent release of Views due (should be later today) which improves on the UI for inserting conditional shortcodes, and includes taxonomies, it will be easier to achieve when that update is available.

#1199909

Okay, I am waiting for toolset update and your reply.

Please notify me once.

#1201829

Hi Himanshu,

Thank you for contacting us.

Nigel is on vacations and will be back tomorrow, but I'll be happy to assist while he is away.

The plugin updates that Nigel mentioned earlier have been released.

After updating all Toolset plugins, to their most recent versions, you can follow these steps:

1. Please create a role model post, which should have all the categories assigned to it. This post will be used for the comparisons.

2. In your content template, you can use the conditional blocks like this:


[wpv-conditional if="( '[wpv-post-taxonomy type="category-slug" item="123" format="slug"]' eq '[wpv-post-taxonomy type="category-slug" format="slug"]' )"]
This one has all categories!
[/wpv-conditional]

[wpv-conditional if="( '[wpv-post-taxonomy type="category-slug" item="123" format="slug"]' ne '[wpv-post-taxonomy type="category-slug" format="slug"]' )"]
This one has only some categories!
[/wpv-conditional]

Note: Please replace "category-slug" with the actual slug of your taxonomy and "123" with the actual ID of the role model post, created in step 1.

As a result, the first conditional block's content will only show for a post, which has all categories and the second one will show for a post without all categories.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1203937

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Himanshu

Let me know if you need any further help here.

#1204199

Thank you!