Skip Navigation

[Closed] How to conditionally change appearance of Taxomony title in list

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.

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 7 years, 1 month ago.

Assigned support staff: Luo Yang.

Author
Posts
#266990

I am trying to: Highlight the taxonomy title in a list of taxonomies when content, or archives of content, belonging to that taxonomy is displayed

I visited this URL: https://toolset.com/forums/topic/how-to-display-current-taxonomy-name-from-url/

I changed the code Luoy gave to the following, in order to be able to compare the taxonomy names of my 'waterproofingtypes' category (that is the slug). I then pasted this in my functions.php

add_shortcode('current-taxonomy-name', 'current_taxonomy_name_func');
function current_taxonomy_name_func($atts, $value){
    extract( shortcode_atts( array(
        'value' => 'wpvwaterproofingtype',
        'field' => 'slug',
        'taxonomy' => 'waterproofingtype',
    ), $atts) );
    $res = '';
    if(isset($_GET[$value]) && get_term_by( $field, $_GET[$value], $taxonomy)){
        $obj = get_term_by( $field, $_GET[$value], $taxonomy);
        $res = $obj->name;
    }
    return $res;
}

I then used the following code in my view to try and get a comparison on the current taxonomy and the one in the loop:

<wpv-loop>
           [wpv-if ctx="wpv-taxonomy-title" evaluate='$ctx=[current-taxonomy-name]']
            <div class="currenttax"> 
                  [wpv-taxonomy-link]<br>
            </div>
           [/wpv-if]
            
           [wpv-if ctx="wpv-taxonomy-title" evaluate='$ctx!=[current-taxonomy-name]']
            <div class="notcurrent"> 
                [wpv-taxonomy-link]<br>
            </div>
           [/wpv-if]
</wpv-loop>

I expected to see: The taxonomy link displayed with 'currenttax' class for the taxonomy currently displayed.

Instead, I got: No taxonomies displayed at all.

I tried changing "wpv-taxonomy-title" to "wpv-taxonomy-name" - but no change. And when I did a test to simply output the shortcode itself: [current-taxonomy-name] - nothing showed. So I guess I must be querying the wrong way or something.

Can someone help me achieve this?
Thanks

#267192

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Please check this:
1) the custom shortcode [current-taxonomy-name] can output the current waterproofingtypes' term title
2) the Views's shortcode [wpv-taxonomy-title] can output the current post term title
3) goto your WP admin side, Views-> Settings
in section

If above does works, please modify your codes in loops as below:

...
[wpv-if evaluate=" '[wpv-taxonomy-title]' = '[current-taxonomy-name]' "]
            <div class="currenttax"> 
                  [wpv-taxonomy-link]<br>
            </div>
[/wpv-if]
[wpv-if evaluate=" '[wpv-taxonomy-title]' = '[current-taxonomy-name]' "]
            <div class="notcurrent"> 
                [wpv-taxonomy-link]<br>
            </div>
[/wpv-if]
...
#267243

Hi Luoy, thanks for that.

I modified the code in custom functions to the following, having checked the slug for Waterproofing Types. It is actually 'waterproofingtypes' :

add_shortcode('current-taxonomy-name', 'current_taxonomy_name_func');
function current_taxonomy_name_func($atts, $value){
    extract( shortcode_atts( array(
        'value' => 'wpvwaterproofingtypes',
        'field' => 'slug',
        'taxonomy' => 'waterproofingtypes',
    ), $atts) );
    $res = '';
    if(isset($_GET[$value]) && get_term_by( $field, $_GET[$value], $taxonomy)){
        $obj = get_term_by( $field, $_GET[$value], $taxonomy);
        $res = $obj->name;
    }
    return $res;
}

And I'm using this in the view:

   <wpv-loop>
     <div class="taxlist">
     [wpv-if evaluate="'[wpv-taxonomy-title]'='[current-taxonomy-name]'"]
            <div class="currenttax"> 
                  [wpv-taxonomy-link]<br>
            </div>
     [/wpv-if]
     [wpv-if evaluate="'[wpv-taxonomy-title]'!='[current-taxonomy-name]'"]
            <div class="notcurrent"> 
                [wpv-taxonomy-link]<br>
            </div>
     [/wpv-if]
     </div>
  </wpv-loop>

...but no dice I'm afraid.
When I place the shortcode in a hookbox like this:

 <?php echo do_shortcode( '[current-taxonomy-name]' ); ?>
 

It displays nothing. Is that the problem? - Do I need to modify the function therefore?

I just need some way for the the chosen taxonomy to be highlighted after it is selected in a list (view) in the sidebar...
Can you suggest anything?

Thanks
Tim

#267479

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Could you duplicate same problem in a test site, and fill below private detail box with login details, also point out the problem page URL and where I can edit the PHP codes, I need a live website to debug this problem, thanks.

The topic ‘[Closed] How to conditionally change appearance of Taxomony title in list’ is closed to new replies.