WordPress.org

Forums

MarketPress - WordPress eCommerce
[resolved] active category (5 posts)

  1. Majklas
    Member
    Posted 11 months ago #

    I know in wordpress it is available to determine which menu item or category is active when viewing post. This is not available in marketpress with it's custom posts. How can I get some css class next to a ancestor menu item which product I am viewving?

    https://wordpress.org/plugins/wordpress-ecommerce/

  2. David
    WPMU DEV Support Staff
    Posted 11 months ago #

    Hi @Majklas,

    Greetings and thanks for your question! Currently, it's possible to create custom templates for MarketPress' Product post type like detailed here:
    http://premium.wpmudev.org/blog/the-easy-guide-to-theming-marketpress/

    You could thus make changes there, adding category classes and such.

    However, I think this may be easier done with a filter like so:
    https://codex.wordpress.org/Function_Reference/post_class#Add_Classes_By_Filters

    Given that, you could try using this:

    <?php
    // add MarketPress product categories to post class
    function mp_post_class_categories($classes) {
    	global $post;
        $terms = get_the_terms( $post->ID, 'product_category' );
        foreach( $terms as $term)
            $classes[] = $term->name;
            return $classes;
    }
    add_filter('post_class', 'mp_post_class_categories');
    ?>

    You can add that to your theme's functions.php or through Code Snippets:
    http://wordpress.org/plugins/code-snippets/

    And given that, you should then be able to style posts based on the categories available through the post div via CSS.

    How would that work for you?

    Cheers,
    David

  3. Majklas
    Member
    Posted 11 months ago #

    Hi, thank You for this tip, but I cannot get a class to style.. I think You posted option to get a class for a pot, not menu item.
    My menu is:

    <ul id="mp_category_list">
    <li class="cat-item cat-item-8"> ...
    <li class="cat-item cat-item-18">
    <a title="test" href="http://project/store/products/category/xxx/">New product category</a>
    </li>
    </ul>

    when I'm in a product of this "New product category" it doesn't get any classes on LI element. I can see "current-cat" when I'm viewing category product list:

    <ul id="mp_category_list">
    <li class="cat-item cat-item-8"> ...
    <li class="cat-item cat-item-18 <strong>current-cat</strong>">
    <a title="test" href="http://project/store/products/category/xxx/">New product category</a>
    </li>
    </ul>
  4. David
    WPMU DEV Support Staff
    Posted 11 months ago #

    Hi @Majklas,

    Thanks for your reply. Here's an adjusted version of the above code that will add the class to the body tag as well:

    <?php
    function mp_post_class_categories($classes) {
        global $post;
        $terms = get_the_terms( $post->ID, 'product_category' );
        foreach( $terms as $term)
            $classes[] = $term->name;
        return $classes;
    }
    add_filter('post_class', 'mp_post_class_categories');
    add_filter('body_class', 'mp_post_class_categories');
    ?>

    Given that, you can target the category like so:

    body.yourcategory ul.mp_category_list li {
    }

    Does that help?

  5. WPMU DEV - Your WordPress Team
    WPMU DEV Support Staff
    Plugin Author

    Posted 9 months ago #

    Hi @Majklas,

    Just wanted to check back on this. It's marked as resolved but we'd love to assist if you have any questions or concerns. Just let us know.

    We're happy to help!

Reply

You must log in to post.

About this Plugin

About this Topic