Support

Account

Home Forums Front-end Issues Get custom field on custom taxonomy

Solved

Get custom field on custom taxonomy

  • Howdy!

    I have a custom taxonomy ‘Review Series” with a featured image custom field. In archive.php I want to set the header image to the image specified if one is provided. I can detect that the user is browsing the “review-series’ taxonomy and I can get the ID of the term but get_field causes WordPress to crash.

    $archive_id = get_queried_object_id();

    This set $archive_id to 22488, the appropriate term_id.

    	if get_field( 'review_series_featured_image', $archive_id ) {
    		$fanart = get_field( 'review_series_featured_image', $archive_id );
    	}

    This is the code that causes WordPress to choke.

    I know I can hunt through wp_postmeta to get the value myself but that seems like a lot of hoop jumping.

    Any ideas?
    Chris

  • 
    if (get_field( 'review_series_featured_image', 'term_'.$archive_id ) {
    

    https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

  • Thanks for the info John, unfortunately neither your code nor the linked page work.

    This has been the most frustrating thing for me as a SQL developer for decades, stuff that should be easy is over complicated by WordPress.

  • What template file are you making changes to. I’m assuming “taxonomy-review-series.php”. I’ve never used get_queried_object_id(), and I learned something new today. I generally do something like

    
    $queried_object = get_queried_object();
    $value = get_field('field_name', $queried_object);
    

    If you are working in a template file that is showing a specific term in a taxonomy then this should work.

  • Hey John,

    I’m using archive.php which I want to use as a generic for every archive. In this case /review-series/reviewed-on-vhs/ is the URL with review-series being detected with is_tax() triggering the if statement.

    The page works great for everything but this featured image thing. I can test for what type of archive is requested and customize my header. I can write my own $wpdb query to get what I want.

    I had to create a page describing my issue because ACF’s ticket system rejected it as spam 🤪 but you can see more details there:

    http://ex.clubsidedev.com/acf-problem.html

    Thanks again for your insights,
    Chris

  • Well the solution was for me to not be dumb and include the outer parentheses on the if statement.

    if ( get_field( 'review_series_featured_image', $archive_id ) ) {
    		$fanart = get_field( 'review_series_featured_image', $archive_id );
    	}
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.