简体   繁体   中英

ACF field display on a page

I've got an issue. I've created a custom field via ACF wordpress plugin. Its a field to custom post type categories (lets say its an additional description to the category). I've tried to add it to my page via such code:

$return_html.= '<h2 class="ppb_menu_title" ';
$return_html.= '>'.$menu_term->name.'</h2><br class="clear"/><br/>';
$displaytitle = the_field('category_subtitle');
$return_html.= '<div class="subtitledesc">'.$displaytitle.'</div>';

below code is a part of full page of code which you can find here [lines 1712-1715]: https://codeshare.io/50QzqL

what i am doing wrong?

get_field() with a single parameter only works on the current post within the loop iirc, so you will have to provide a target if you're trying to get data for a category.

You'll need the termid of your category (when you're on a taxonomy-page, $term = get_queried_object(); $termid = $term->term_id; should work), then use get_field like so:

get_field( 'category_subtitle', "taxonomyname_$termid" );
get_field( 'category_subtitle', $term ); // if you have a term object

Further reading: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

You'll want to use get_field() instead of the_field() and include the term ID.

get_field() returns a value.

the_field() echoes a value.

Try this: get_field('category_subtitle', 'term_' . $menu_term->term_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM