Skip Navigation

[Gelöst] List of child category for a specific parent category

This support ticket is created vor 4 Jahre, 2 Monate. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 Antworten, has 2 Stimmen.

Last updated by jean-francoisB vor 4 Jahre, 1 Monat.

Assisted by: Minesh.

Author
Artikel
#1524251

Hi,

Page showing the issue :

I got your help to put the list of all Main category on this page : hidden link (ticket : https://toolset.com/forums/topic/showing-a-clickable-list-of-child-taxonomy-on-a-taxonomy-view-page/).

Now I try to get something more complete. With a mouse over a taxonomy, I want a list of child taxonomy displayed in a Tooltip window. So for now, it's working well, but showing the complete list of categories in the listing-category taxonomy. So I ONLY want the child category of that specific parent category that the mouse is over.

This is the code in my function.php that display this taxonomy list :

add_filter('wpv_filter_taxonomy_query', function($query, $settings, $views_id){
    if($views_id == 412 && isset($_GET['wpv-listing-category'])){
        $term = get_term_by('slug', $_GET['wpv-listing-category'], 'listing-category'); // here replace "listing-category" with your custom taxonomy slug
         
        if($term){
            unset($query['child_of']);
$query['parent'] = $term->term_id;
        }
    }
    return $query;
}, 99, 3); 

add_shortcode('get_term_by_url_param', function($atts){
    $res= '';
    $atts = shortcode_atts( array(
        'url_param' => 'wpv-listing-category',
        'tax' => 'listing-category',
    ), $atts);
    if(isset($_GET[$atts['url_param']]) && $_GET[$atts['url_param']] != ''){
        $term = get_term_by('slug',$_GET[$atts['url_param']], $atts['tax']);
        if($term){
            $res = '<strong>Category :</strong> '. $term->name;
        }
    }
    return $res;
});

This is the code for the Loop item in Listing category list

{tooltip}<a href="?wpv-listing-category=[wpv-taxonomy-slug]">[wpv-taxonomy-title]</a>{end-text}[xyz-ips snippet="childcat"]{end-tooltip}

So the Tooltip shortcode is from a plugin that allow me to publish tooltip.

So everything displayed in the tooltip is included in : [xyz-ips snippet="childcat"]

This is the code that I have in this PHP snippet :

<?php
// List terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
 
$toolsetslug = do_shortcode('[wpv-taxonomy-slug]');
$category = get_category_by_slug($toolsetslug);

$taxonomy     = 'listing-category';
$orderby      = 'name'; 
$show_count   = false;
$pad_counts   = false;
$hierarchical = true;
$title        = '';
 
$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
'child_of ' => $category->term_id,
'hide_empty'          => 0
);
?>
 
<ul>
    <?php wp_list_categories( $args ); ?>
</ul>

So wp_list_categories publish the full category list. So I need to specify the 'child_of' to make sure that I get the subcategories of the category my mouse is over. To get this category ID, I have created $toolsetslug with do_shortcode to be able to use [wpv-taxonomy-slug] shortcode. $category is there to get the ID from that slug. I didn't find an easier way to get the category ID.

I'm maybe wrong in my code. I'm very newbie and I got pieces of code in different forums. But I still have a feeling that I'm not too far to get what I'm looking for.

Any idea what I could change in this code to get it working?

Thank you very much!

#1525115

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I'm not sure why you make such things too complicated by adding such custom code when you can achieve the same thing using Toolset only.

Can you please check: hidden link

I've created the following child taxonomy view:
=> hidden link

And added the above view to the following view:
=> hidden link
Where:
we are displaying the "child-listing-category-tooltip" listing view as given under:

{tooltip}<a href="?wpv-listing-category=[wpv-taxonomy-slug]">[wpv-taxonomy-title]</a>{end-text}[wpv-view name="child-listing-category-tooltip"]{end-tooltip}
#1525631
Capture d’écran, le 2020-02-24 à 12.38.43.png

Hi,

Thank you for your help. But now I'm getting an error message on this page : hidden link

See attached. Let me know if you can fix that.

Thank you,

#1526203

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

It must be the cache. I can see the page is working as expected. Please check the following screenshot:
=> hidden link

Please clear browser as well as server cache and it should work OK.

#1527903

Hi Minesh! You were right, clearing the cash solved this issue. It's now working very well!

Just a little detail. For the sub-category that is published in the tooltip, is it possible to make the links look like the main category link?

Actual link of subcategory : hidden link
Desired link of subcategory : hidden link

Thank you!

#1527993

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - that's possible:

With the following view, I've changed the "Loop Editor section" content as given under:
=> hidden link

<a href="?wpv-listing-category=[wpv-taxonomy-slug]">[wpv-taxonomy-title]</a>

I can see with child taxonomy terms it's displaying now expected URLs.

#1532545

Thanks Minesh! You are the best! You have been really useful to get my issues solved quickly. Thanks!

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.