Conditional logic in marketpress

I’m trying to get my head around this but struggling, so any pointers would be most grateful.

I am trying to code a bit of conditional logic into a widget. I only want this to occur when I am on a specific product category page.

I have been playing with the wordpress conditional tags that I thought might work e.g

has_term('mycategory','product_category') but that doesn’t seem to do it?

Any one got any ideas?


  • Tom Eagles
    • Syntax Hero

    Hi there @roibot

    Are you trying to run a conditional display function or something else? Just so I don’t go barking up the wrong tree here,

    If you aren’t comfortable sharing in public whip it over to me using the contacts form at the bottom of the page and mark it for my attention with a link to it and i will see what i can come up with for you.



  • Fullworks
    • The Bug Hunter

    Yes, it is a conditional display. I want to display a specific menu in the side bar only for category pages and products that are in a certain category.

    Although more genercally what I am trying to achieve is to display the selection of tags in a side ‘menu’ where only the tags applicable to the top level category appy.

    So for example, I may have

    a category of fruit, that may have sub categories of summer fruit and citrus fruits.

    and also

    a category of clothes, that may have sub-categories of mens and ladies.

    I may tag fruits as organic and non-organic

    I may tag clothes as new and second hand

    So in the side bar of any fruit, at category page, sub-category or the fruit, I want in the sidebar just links to organic and non-organic tags (i.e. not including the tags, new and second hand)

    Now I’m happy coding the display logic if I can just only identify what actual product category I am in in an archive or product page.

  • Tom Eagles
    • Syntax Hero

    Hi @roibot

    While i was digging around i cam across this:

    Conditional Widgets plugin does exactly as you want there from what i read.

    or you could use this to display the sub categorys of the currrent one

    // display sub-categories of current category
    function display_sub_categories() {
    $current_cat = get_query_var('cat');
    $args = array('child_of'=>$current_cat);
    $categories = get_categories($args);
    if (!empty($categories)) {
    echo '<ul>';
    foreach ($categories as $category) {
    echo '<li><a href="'.$category->slug.'">'. $category->name .'</a></li>';
    echo '</ul>';
    } else {
    // fallback for when no subcategories are available

    Call it with this <?php display_sub_categories(); ?>

    Is this what you were aiming for?



  • Fullworks
    • The Bug Hunter

    Well it would be, it it dealt with product categories, rather than the standard wordpress categories, but it gave me an idea.

    I think in the mp_category.php is can use

    get_query_var('term') to get the product category in use

    in mp_product

    get_the_terms( $post->ID, 'product_category' );

    should give me data, then it is a matter of processing it in my sidebar/widget

    I just need a way of knowing if the current page is a product_category archive or a single product page (these are reflected in the <body> class, but I am wondering is there is an easier way , like the wordpress conditional tags functions, but for marketpress)

  • Aaron
    • CTO

    has_term('mycategory','product_category') is the proper function, the problem is if you don’t pass it a post object then it needs to be in the loop, and your widget is definitely not in the loop.

    I think this might do the trick:

    global $wp_query;
    if (has_term('mycategory','product_category', $wp_query->post)) {
    //do stuff here

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.