Support

Account

Home Forums General Issues Howto Inject Category Slug into get_field

Solved

Howto Inject Category Slug into get_field

  • Hello Community,

    Just started using ACF.

    I’m trying to load a custom text field on the category product loops inside WooCommerce. The code below works to display my text on the loop:

    if(get_field('eref-airbox'))
    	{
    		echo '<div><span>REF: ' . get_field('eref-airbox') . '</span></div>';
    	}
    

    However I need to test for many different fields, all of which have names matching the structure eref-{category-slug}. I don’t want to put hundreds of if statements in my content-product loop. So I’d like to inject the slug for the current category being viewed into the statement. I tried:

    $category = get_the_category();
    if(get_field('eref-' . $category[0]->slug . ''))
    	{
    		echo '<div><span>REF: ' . get_field('eref-' . $category[0]->slug . '') . '</span></div>';
    	}
    

    but it is not working. It passes PHP validation, but no longer loads the custom fields.

    Any help greatly appreciated.

    Thank you.

  • Looking at what you have it appears that it should be working.

    The only thing I can think of from the code you supply is the value of 'eref-' . $category[0]->slug . '' does not equal your field name, of the field for 'eref-' . $category[0]->slug . '' is not set.

    Have you tried echoing the value of 'eref-' . $category[0]->slug . '' to see what it is?

  • Added echo 'eref-' . $category[0]->slug . ''; to my content-product.php that drives the Woocommerce category archive loop, and it just returned “eref-“.

    So I guess ' . $category[0]->slug . ' is not matching up to anything.

  • Are you sure what you want to get is the category? I think that WooCommerce uses a custom post type and custom taxonomy for products, but I’m not sure what those are right now.

    You might want to try get_the_terms() https://codex.wordpress.org/Function_Reference/get_the_terms

  • I just looked at a site I have to testing WooCommerce.

    The taxonomy they use is “product_cat”

    to get the categories you would use

    
    $categories = get_the_terms($post_id, 'product_cat');

    `

  • $categories = get_the_terms($post_id, 'product_cat');
    echo $categories;
    

    The above returns “Array”

  • Yes, so $categories[0]->slug should now hold the correct value that you were looking for. You should be able to loop through this array and do what you were looking to do, something like

    
    $categories = get_the_terms($post_id, 'product_cat');
    foreach ($categories as $category) {
        if (get_field('eref-'.$cagegory->slug')) {
            echo '<div><span>REF: '.get_field('eref-'.$category->slug).'</span></div>';
        }
    }
    
  • Solved. Thanks for your help John.

    My final code on content-product.php

    $categories = get_the_terms($post_id, 'product_cat');
    if(get_field('eref-' . $categories[0]->slug . ''))
    	{
    		echo '<div><span>REF: ' . get_field('eref-' . $categories[0]->slug . '') . '</span></div>';
    	}
    
  • Damn.

    I spoke too soon.

    The two categories I was testing did not contain any products which are located in multiple categories. If a product is located in multiple categories (with a REF letter set for multiple categories) then the PHP is loading the REF from the first category in the array. I added echo '<div>eref-' . $categories[0]->slug . '</div>'; into the loop to confirm that is what was happening. See the image below:

    Category Slug Inject Fail

  • <?php echo single_term_title(); ?> When placed outside or inside of the product archive loop correctly returns the proper title of the active Woocommerce category. So now I just need a way to break that result down to the slug, and I’d be in business.

  • The true solution:

    $catslug = get_query_var( 'product_cat' );
    if(get_field('eref-' . $catslug . ''))
    	{
    		echo '<div><span>REF: ' . get_field('eref-' . $catslug . '') . '</span></div>';
    	}
    

    I hope it can help someone else in the future too!

  • purekarting,

    Glad you found the solution. When you marked a response as the solution I stopped getting emails on this subject so I did not see your followup question. Sorry bout that.

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘Howto Inject Category Slug into get_field’ is closed to new replies.