MarketPress/FrameMarket/Childtheme : how to retrieve cat ID

HI
I'm using Advanced Custom Fields plugin to install a image on the product category top pages (I guess it would be a Category Archive page in plain on WP)

When I use the code :
<img class="category-image"
src="<?php $value = get_field('category-image', 'product_category_24' );
echo $value ; ?>" width="640" height="200" border="0">
It works fine.

But I need to make the "product_category_" ID dynamic to reflect the current category.

So I tried:
<?php $value = get_field('category-image', 'product_category_'.$cat );
echo $value ; ?>
but no luck

I've also tried it replacing $cat with $category_id, but no luck

How can I retrieve the current category ID for this to work?

Thanks

  • digitsoft
    • The Crimson Coder

    This should get you started...lmk if you need more help:

    <?php
    $terms = get_the_terms($post->ID, "product_category");
    foreach ( $terms as $term ) {
    if ($term->name == 'your-cat-name') //use this if you want to limit by category
    {
    echo 'cat name matched';
    }else{
    echo 'cat name didnt match';
    }
    }
    ?>

    If you just want the category...

    <?php
    $terms = get_the_terms($post->ID, "product_category");
    foreach ( $terms as $term )
    {
    echo 'Category: '.$term.'<br>';
    }
    ?>
  • thirteen
    • Flash Drive

    Thanks - I don't think I described well - sorry about that (BTW the second solution borked the whole page)

    I need to get the "ID" of the current category archive that is being displayed (via mp_category.php)

    so the code

    <?php $value = get_field('category-image', 'product_category_24' );
    echo $value ; ?>

    returns the value stored in the "category-image" field connected with "product_category_24".
    "24" being the ID of the current category being displayed.

    I just need a way to have "24" be the current product_category ID. The usual $cat wouldn't work

  • digitsoft
    • The Crimson Coder

    I've used this code within pages before and they work fine..not sure why it "borked" your page. For them to work you'd need a php plugin like Execphp...

    Try this out...it works for me within a product post (I have Execphp loaded), but the concept is the same.

    I had to look around to figure out that mp_category.php was in the gridmarket theme...lol.

    <?php
    $terms = get_the_terms($post->ID, "product_category");
    foreach ( $terms as $term ) {
    if ($term->name == 'Top') //there can be multiple categories, so match what you're looking for
    {
    $cat = $term->term_id;
    $value = get_field('category-image', 'product_category_'.$cat);
    echo 'Value: '.$value ;
    }else{
    echo 'cat name does not match';
    }
    }
    ?>

    FYI - if using a product to run this code, verify the saved page/post/product has readable php code - I've had plugins interfere and change < >.

  • thirteen
    • Flash Drive

    thanks
    I think there was a misunderstanding on what I was trying to do. We needed the product_category ID for ANY product_category - we aren't trying to match a particular one. And there's no real documentation on the Marketpress plugin.

    I needed to take out the if and else part, but this now works

    <?php
        $terms = get_the_terms($post->ID, "product_category");
              foreach ( $terms as $term )
                        {
                        $cat = $term->term_id;
                        $value = get_field('category-image', 'product_category_'.$cat);
    		    }
    ?>
    <img class="category-image" src="<?php echo $value ; ?>" width="640" height="200" border="0">
  • digitsoft
    • The Crimson Coder

    No misunderstanding..was just giving you code to work with....and you came up with a solution using it, which was the goal.

    If everything is good, let us know and mark it as resolved....and feel free to click the "Say thanks" button next to my name :wink:

    Rob

  • aecnu
    • WP Unicorn

    Greetings thirteen,

    It appears this particular topic is now resolved/closed, if you need any further assistance please let us know.

    If it wasn't resolved, or you have any more questions related to this thread, please feel free to post them below including any new symptoms or errors and tick the 'Mark as Not Resolved (re-open)' box below the post area (or else we'll miss it!)

    Thank you for being a WPMU Dev member!

    Cheers, Joe :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.