Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to display ACF fields with category posts
Tagged: ACF, Custom fields, genesis_get_custom_field, get_post_meta
- This topic has 22 replies, 3 voices, and was last updated 7 years, 6 months ago by Bart van Maanen.
-
AuthorPosts
-
September 23, 2016 at 11:41 am #193604Bart van MaanenParticipant
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 #193623Genesis DeveloperMemberin_category() is not a conditional function. So you are getting the 500 error. It would be is_category('4'). Try once.
September 25, 2016 at 11:26 pm #193707Brad DaltonParticipantI'd find new code because that code has multiple errors plus it doesn't do what it says it does correctly.
September 26, 2016 at 9:28 am #193721Bart van MaanenParticipantThanks @ 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 #193736Brad DaltonParticipantHi 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
September 29, 2016 at 11:36 am #193892Bart van MaanenParticipantHi 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 #193934Brad DaltonParticipantSeptember 30, 2016 at 5:58 am #193941Bart van MaanenParticipantThanks 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 #193942Brad DaltonParticipantUse 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>'; }
September 30, 2016 at 6:16 am #193943Bart van MaanenParticipantSorry, 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 #193946Brad DaltonParticipantYou'll also need a check for each field:
if ( $credits-titel ) { echo '<div class="credits-titel">'. $credits-titel .'</div>'; }
September 30, 2016 at 6:36 am #193947Bart van MaanenParticipantAllright, 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 #193948Brad DaltonParticipantYes 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>'; }
September 30, 2016 at 7:18 am #193951Bart van MaanenParticipantI'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 #193955Brad DaltonParticipantTry 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>'; } }
September 30, 2016 at 8:07 am #193960Bart van MaanenParticipantThis reply has been marked as private.September 30, 2016 at 9:06 am #193962Bart van MaanenParticipantHi 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.
BartSeptember 30, 2016 at 9:09 am #193963Brad DaltonParticipantUse 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
September 30, 2016 at 9:55 am #193971Brad DaltonParticipantYou 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 ); } }
September 30, 2016 at 10:46 am #193995Bart van MaanenParticipantGreat 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.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.