• Hi,

    I’m busy with this WordPress website: https://tftinstallatie.inpointbox.nl/producten/systeemplafondarmaturen/test-02/

    The link (above) shows a post. It’s a product, which I have made with custom post types. At the left you see al the product cats, created with a taxomony.

    That works realy nice. On the archive page the menu shows correct the active taxomony with “current_cat”. But when you’re on a product, the current taxomony don’t get a “current_cat” class.

    How can I fix this, so that the menu get also a current class when the product is showing.

    This is the code to list the taxomony list.

    <?php
    $taxonomy     = 'productcats';
    $orderby      = 'name';
    $show_count   = 1;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = 'Productcategoriën:';
    
    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );
    ?>

    Thanks in advance!

    Derk

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter Derk Oosterveld

    (@derkdec)

    I think I need to set up a query to list all the items in the taxomony. But I need to check which category (from productcats) is active. Is there a function that give the category from a post?

    get_the_category gives you the categories for a post. Not sure if this works with custom post types though (haven’t tested it and couldn’t find anything mentioned about that on the get_the_category page).

    Thread Starter Derk Oosterveld

    (@derkdec)

    The function get_the_category doesn’t work.
    But I have a function (get_the_term_list) that display the productcat of the product:
    echo get_the_term_list( $post->ID, 'productcats');

    But the output is a HTML string. Can anyone help me to get only the name of the productcat, not the whole HTML for a link?

    Ah, I had been looking for that function (for custom post types), but couldn’t find it. I’ll take a closer look at this later this afternoon.

    You can just copy the function wp_list_categories, since you use that to build your list right? https://core.trac.www.ads-software.com/browser/tags/3.0.1/wp-includes/category-template.php

    Where it adds the classes to the list item check to which category the current post/product belongs, if that matches with the current list category item add your active class. I haven’t got much time at the moment, else I would give it a try myself. Maybe this evening!

    One more thing, to check to which taxonomy the current post belongs you can indeed use get_the_term_list();

    What I usually do is calling this function for every taxonomy and store all returned info in a variable, like this:

    $productnr1 = get_the_term_list($post->ID,'productnr1');
    $productnr2 = get_the_term_list($post->ID,'productnr2');

    Then, check which string is not empty. That’s your current ‘category’!

    Thread Starter Derk Oosterveld

    (@derkdec)

    $productnr1 = get_the_term_list($post->ID,'productnr1');
    This code returns which productcat the product has. I can’t use that output, because it’s a HTML string.

    The other option, you gave, is not my preference, associated with updating.

    You don’t have to use the output. Since you provide the current post’s ID and the taxonomy you’re checking it will return an empty string if the current post is not in the taxonomy you provided.. (2nd parameter)

    So if you call get_the_term_list with the current post ID for all your different taxonomies you can check which returned string is not emtpy..

    For the other thing, I rather used WordPress built-in function (tweaked) then writing a whole new query for it, which is pretty useless imo. Many premium themes just use a tweaked version of wp_list_categories.. An update that removes the functionality of older versions of wp_list_categories is unlikely, cause it would affect to many websites.

    Thread Starter Derk Oosterveld

    (@derkdec)

    The taxomony is “productcats”. With this code,
    echo get_the_term_list( $post->ID, 'productcats');
    I get de active cat, I want to work with.

    With your code I can not in each category along and check what is empty. It is not a code that checks which categories are in productcats.

    Hm my bad, I thought you assigned a different taxonomy to every product category.

    If every ‘category’ is a term from the taxonomy ‘productcats’ you could try working with wp_get_post_terms($post->ID, 'productcats', $args ) that retrieves the term (or producatcategory in this example) of the current post.

    get_the_post_terms()

    Thread Starter Derk Oosterveld

    (@derkdec)

    Hm, that doesn’t work. It’s a array, but it is empty, I think.

    This code:

    $terms = wp_get_object_terms($post->ID, 'productcats' );
    echo $terms[0];

    Give this error:
    Catchable fatal error: Object of class stdClass could not be converted to string in

    This code works, but gives the whole HTML for a link:
    echo get_the_term_list( $post->ID, 'productcats');

    Maybe it is possible to remove all the things like a tag etc.?

    Try to var_dump/print_r the result of wp_get_post_terms($post->ID,'producatcats','') and see if you can do something with the output.. I’ll give it a go myself later on, requires some work to set-up a test environment for this case. ??

    print_r(wp_get_post_terms($post->ID,'productcats',''));

    Thread Starter Derk Oosterveld

    (@derkdec)

    I’ve got it!

    This code gives the current productcat:

    <?php $term = get_term_by( 'name', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>

    And with that piece of code I check if it’s active:

    <?php if($tax_menu_item->name == $term_active) { echo"class=\"current_cat\""; } ?>

    The query to get all the cats is:

    <?php $args = array('taxonomy' => 'productcats');
    $tax_menu_items = get_categories( $args ); ?>

    Thanks everybody!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Active class for taxomony, when showing the post’ is closed to new replies.