I was able to get this working just fine with this code with advanced custom fields:

<?php $value = get_field('extended_store_descriptions', stores_47);
echo $value;

Now how can I make that number 47 dynamic and automatically generate based on the category id.

I tried doing this

<?php $storenum = get_the_category(); ?>
<?php $value = get_field('extended_store_descriptions', stores_$storenum);
echo $value;

To no avail, I'm sure this is any easy fix if someone can jump in. Thanks a lot!

  • Have you tried to use valid PHP? "stores_$storenum" would be a start. ;)
    – fuxia
    Feb 6 '13 at 7:21
  • get_the_category also does not produces ID, but an array of term objects... try $storenum = get_query_var('cat') instead Feb 6 '13 at 7:23
  • I should have noted that this is using the Advanced Custom Fields plugin, and "stores" is the name of the custom taxonomy and 47 is the id of the particular category page I was working in, but of course I manually entered it to test it on that one category page but I would want the 47 to change for each page. I'll try out these great answers and get back
    – Joe R.
    Feb 6 '13 at 16:54

What is stores_47 exactly? A variable name? Shouldn't it be $stores_47 instead?

I don't understand what you want to do here, but I think you're looking for variable variables.

Try this:

$cat_id = 47;
$value = get_field('extended_store_descriptions', ${ 'stores_' . $cat_id } );
echo $value;
  • This worked for me except I made a minor edit: '<?php $cat_id = 47; $value = get_field('extended_store_descriptions', 'stores_' . $cat_id ); echo $value; ?>' Now my only focus is dynamically changing the 47 to the current category (custom "store" taxonomy) page I'm on
    – Joe R.
    Feb 6 '13 at 21:08
  • Shouldn't the second argument of get_field be an integer(post ID) ?
    – RRikesh
    Feb 7 '13 at 5:14
  • The second part of get_field should be 'stores_47' as defined by the Advanced Custom Fields documentation. My problem is defining $cat_id = with the category id of the custom taxonomy which is "stores".
    – Joe R.
    Feb 7 '13 at 6:03
  • Did you try get_the_terms() or wp_get_post_terms()?
    – RRikesh
    Feb 7 '13 at 6:37
  • When I do this: <?php $terms = get_the_terms($post->ID, 'stores'); print_r($terms); ?> It gets me close to what I want: This is outputted: Array ( [47] => stdClass Object ( [term_id] => 47 [name] => Amazon.com [slug] => amazon-com [term_group] => 0 [term_taxonomy_id] => 49 [taxonomy] => stores [description] => Amazon Coupon Codes Amazon is the world's largest online retailer. The company also produces consumer electronics, and is a major provider of cloud computing services. [parent] => 0 [count] => 1 [object_id] => 40 ) ) But I only want the number 47 in there
    – Joe R.
    Feb 8 '13 at 2:02

I'm not 100% sure what you want to achieve, but hope this helps (works for me):

<?php $cat_id = get_query_var('cat'); ?>
<?php echo $cat_id; ?>

I am using this in the category.php page, so it's kind of a general code. if you have a specific category, then you might need to get category ID by slug:

<?php $category_id = get_cat_ID( 'Category Name' ); ?>
  • So I have a custom taxonomy called "stores", would I replace 'Category Name' above with 'stores' ? Thanks.
    – Joe R.
    Feb 6 '13 at 21:13
  • In case this will still help, I would suggest to use the label instead of the slug.
    – bboy
    Feb 8 '13 at 9:26

Thanks so much for the help, especially to rrikesh.

I figured it out with some help from this page: http://wordpress.org/support/topic/trying-to-output-a-posts-terms-taxonomy-as-text-not-urls#post-1188801

// Get terms for post
$terms = get_the_terms( $post->ID , 'stores' );

// Loop over each item since it's an array
foreach( $terms as $term ) {
// Print the term_id method from $term which is an OBJECT
// $lovers will be grabs the current store page id
$lovers= $term->term_id;
// Get rid of the other data stored in the object, since it's not needed

$cat_id = $lovers;
$value = get_field('extended_store_descriptions', 'stores_' . $cat_id );
echo $value;


Thanks so much for the input, let me know if there's a more elegant way to write the code above, but it will work for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.