• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

How to display ACF fields with category posts

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to display ACF fields with category posts

This topic is: not resolved

Tagged: ACF, Custom fields, genesis_get_custom_field, get_post_meta

  • This topic has 22 replies, 3 voices, and was last updated 9 years, 7 months ago by Bart van Maanen.
Viewing 20 posts - 1 through 20 (of 23 total)
1 2 →
  • Author
    Posts
  • September 23, 2016 at 11:41 am #193604
    Bart van Maanen
    Participant

    I'm trying to get custom fields - made with Advanced Custom Fields plugin - show up properly on the front and. I would like to use category posts, i.e. Films (ID = 4). On the end of those post I like to have the credits of the film displayed., so only text with some css.

    Most tutorials use a custom post type, so I was looking for some code using functions.php only.

    Hoping to have found that in Jef Cohan's post I adapted his code like this:

    //* Maak Advanced Custom Fields zichtbaar voor categorie Films
    
    add_action ('genesis_entry_content', 'acf_film_credits' );
    
    function acf_film_credits() {
    	if ( ! ( is_single() && in_category('4') ) ) {
    		return;
    	}
    	// else
    	
    	// These are the custom field keys defined in the ACF dashboard
    	$metas_film = array(
    		'titel', 
    		'productie', 
    		'regie', 
    		'camera', 
    		'muziek', 
    		'producent'
    		'cast');
    	
    	// If ANY of these fields is filled in, 
    	// then we'll proceed and start the div.
    	$has_meta = false; // init
    	foreach ($metas_film as $test ) {
    		if ( get_field($test) ) {
    			$has_meta = true;
    			continue; // Just need one meta field filled to create the div.
    		}
    	}
    
    	if ( $has_meta ) {
    		echo '<div class="film-credits">';
    		echo '<h4> the_title(); </h4>';
    		foreach ( $metas_film as $meta_film ) {
    			$$meta_films = get_field($meta_film);
    			if ( $$meta_film ) {
    				$f_object = get_field_object($meta_film);
    				$label = $f_object['label'];
    				echo '<div class="' . $meta_film . '"><span class="label">' . 
    				$label . ':</span> ' . 
    				'<span class="value">' . $$meta_film . '</span></div>';
    			}
    		}
    		echo '</div><!-- /.film_credits -->';
    	}
    }
    

    But adding it to functions.php returned an 500 error and the site went down. Question 1: what went wrong here?

    Besides this I have seen Bill Ericson's post about using the get_post_meta function instead of the get_field function from ACF. Makes sense too.

    So, Question 2 : what code for displaying and styling these fields can help me out?

    Thanks for your attention, Bart

    September 23, 2016 at 8:03 pm #193623
    Genesis Developer
    Member

    in_category() is not a conditional function. So you are getting the 500 error. It would be is_category('4'). Try once.


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    September 25, 2016 at 11:26 pm #193707
    Brad Dalton
    Participant

    I'd find new code because that code has multiple errors plus it doesn't do what it says it does correctly.


    Tutorials for StudioPress Themes.

    September 26, 2016 at 9:28 am #193721
    Bart van Maanen
    Participant

    Thanks @ Genesis Developer and @ Brad Dalton.

    @ Brad Dalton: Do you have any suggestions for code or a tutorial that might help me along? Except hiring you? 🙂

    September 27, 2016 at 2:31 am #193736
    Brad Dalton
    Participant

    Hi Bart

    Yes. This is an example of how to print a custom field value

    add_action( 'genesis_entry_content', 'cf' );
    function cf() {
    if ( ! is_single() && in_category( 'category-4' ) ) 
    		return;
    $cf = get_post_meta( get_the_ID(), 'title', true );
    if( ! empty( $cf ) ) {
    echo '<div class="cf-title">'. $cf .'</div>';
        }
    }
    

    You can use this code as a starting point.

    The code prints the value for a custom field named title


    Tutorials for StudioPress Themes.

    September 29, 2016 at 11:36 am #193892
    Bart van Maanen
    Participant

    Hi Brad,
    Great, thanks, this code works fine.

    add_action( 'genesis_entry_content', 'credits' );
    function credits() {
    if ( ! is_single() && in_category( 'films' ) ) 
    		return;
    $credits = get_post_meta( get_the_ID(), 'titel', true );
    if( ! empty( $credits ) ) {
    echo '<div class="credits-titel">'. $credits .'</div>';
    	}
    }
    

    I was able to style the title in the css as well.

    But as I have 7 ACF fields I would like to minimize the code and would like them in an array of some kind (with foreach ?). Or can I duplicate the section after "return;" for each field? I has to be conditional because sometimes fields are left blank.

    And then style the lot of them like in a content box.

    September 30, 2016 at 3:14 am #193934
    Brad Dalton
    Participant

    Yes, you can use get_post_meta for each field


    Tutorials for StudioPress Themes.

    September 30, 2016 at 5:58 am #193941
    Bart van Maanen
    Participant

    Thanks Brad, you mean like this:

    add_action( 'genesis_entry_content', 'credits' );
    function credits() {
    if ( ! is_single() && in_category( 'films' ) ) 
    		return;
    $credits = get_post_meta( get_the_ID(), 'titel', true );
    $credits = get_post_meta( get_the_ID(), 'productie', true );
    $credits = get_post_meta( get_the_ID(), 'regie', true );
    if( ! empty( $credits ) ) {
    echo '<div class="credits-titel">'. $credits .'</div>';
    echo '<div class="credits-productie"> . $credits .'</div>';
    echo '<div class="credits-regie"> . $credits .'</div>';
        }
    }
    September 30, 2016 at 6:05 am #193942
    Brad Dalton
    Participant

    Use unique variables for each field.

    Example:

    add_action( 'genesis_entry_content', 'credits' );
    function credits() {
    if ( ! is_single() && in_category( 'films' ) ) 
    		return;
    $title = get_post_meta( get_the_ID(), 'title', true );
    $product = get_post_meta( get_the_ID(), 'product', true );
    echo '<div class="credits-titel">'. $title .'</div>';
    echo '<div class="credits-productie">' . $product .'</div>';
    }
    

    Tutorials for StudioPress Themes.

    September 30, 2016 at 6:16 am #193943
    Bart van Maanen
    Participant

    Sorry, missed your example, so my code was BS.
    But can I leave out the empty statement as in your example?

    if( ! empty( $credits ) ) {

    September 30, 2016 at 6:26 am #193946
    Brad Dalton
    Participant

    You'll also need a check for each field:

    if ( $credits-titel ) {
    echo '<div class="credits-titel">'. $credits-titel .'</div>';
    }

    Tutorials for StudioPress Themes.

    September 30, 2016 at 6:36 am #193947
    Bart van Maanen
    Participant

    Allright, so the complete code per field to repeat is:

    $titel = get_post_meta( get_the_ID(), 'titel', true );
    if( ! empty( $titel ) ) {
    echo '<div class="credits-titel">'. $titel .'</div>';
    September 30, 2016 at 6:37 am #193948
    Brad Dalton
    Participant

    Yes however you also need to close the bracket

    $titel = get_post_meta( get_the_ID(), 'titel', true );
    if( ! empty( $titel ) ) {
    echo '<div class="credits-titel">'. $titel .'</div>';
    }
    

    Tutorials for StudioPress Themes.

    September 30, 2016 at 7:18 am #193951
    Bart van Maanen
    Participant

    I've now tried to add the second field with bracket closed, like this:

    add_action( 'genesis_entry_content', 'credits' );
    function credits() {
    if ( ! is_single() && in_category( 'films' ) ) 
    		return;
    $titel = get_post_meta( get_the_ID(), 'titel', true );
    if( ! empty( $titel ) ) {
    echo '<div class="credits-titel">'. $titel .'</div>';
    }
    $productie = get_post_meta( get_the_ID(), 'productie', true );
    if( ! empty( $productie ) ) {
    echo '<div class="credits-productie"> . $productie .'</div>';
    }
    }

    Doing so I get a blank screen! In my code editor the last </div> changes color as in error?

    This is an post I would like to have the credits on, now created with a Genesis content box: http://www.mokumfilm.nl/de-matthaus-missie-van-reinbert-de-leeuw/

    You can see the title showing up below the social buttons.

    September 30, 2016 at 7:27 am #193955
    Brad Dalton
    Participant

    Try this:

    add_action( 'genesis_entry_content', 'credits' );
    function credits() {
    if ( ! is_single() && in_category( 'films' ) ) 
    		return;
    $titel = get_post_meta( get_the_ID(), 'titel', true );
    if( ! empty( $titel ) ) {
    echo '<div class="credits-titel">'. $titel .'</div>';
    }
    $productie = get_post_meta( get_the_ID(), 'productie', true );
    if( ! empty( $productie ) ) {
    echo '<div class="credits-productie">' . $productie .'</div>';
    }
    }
    

    Tutorials for StudioPress Themes.

    September 30, 2016 at 8:07 am #193960
    Bart van Maanen
    Participant
    This reply has been marked as private.
    September 30, 2016 at 9:06 am #193962
    Bart van Maanen
    Participant

    Hi Brad, above code seems to do the trick now. I have succesfully added the second and third field. Don't know why they didn't show up after refreshing the page. Visiting another page it popped up.

    I am now wondering what the name of the div is or how to create it for styling it as a contentbox? Using .credits in style.css returns nothing.

    Thanks for all your help. I really appreciate it.
    Bart

    September 30, 2016 at 9:09 am #193963
    Brad Dalton
    Participant

    Use exactly the same as you have printed in the PHP code for each field

    .credits-titel

    You could name the divs all the same if you want to style them all the same


    Tutorials for StudioPress Themes.

    September 30, 2016 at 9:55 am #193971
    Brad Dalton
    Participant

    You could also code it like this:

    add_action( 'genesis_entry_content', 'custom_field_credits' );
    function custom_field_credits() {
    
    if ( ! is_single() && in_category( 'films' ) ) 
    		return;
    
        $title = genesis_get_custom_field( 'title' );
        $product = genesis_get_custom_field( 'product' );
    	
    if ( $title ) {
    	printf( '<div class="credits">%s</div>', $title );
        }
    
    if ( $product ) {
    	printf( '<div class="credits">%s</div>', $product );
        }
    }
    

    Tutorials for StudioPress Themes.

    September 30, 2016 at 10:46 am #193995
    Bart van Maanen
    Participant

    Great thanks, is there an advantage using this?

    Could that be wrapped in an content box with some margin and padding like over here: http://www.mokumfilm.nl/de-matthaus-missie-van-reinbert-de-leeuw/

    In a way to get the title in <h4> and other info as regular text? I now have the title in <h4> in functions.php, but adding css for background-color gives white space in between title an text.

  • Author
    Posts
Viewing 20 posts - 1 through 20 (of 23 total)
1 2 →
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble