Home › Forums › Front-end Issues › ACF and Genesis
Hi!
I’ve created a custom post named ‘training’ which is not display as an article in the frontend but as features at the end of an article.
I use ACF to create a group field for this custom post with this three fields :
– link
– number_of_days
– price
I’ve created a second group field named ‘name_of_the_training’ with only one field which is a Relationship type and is display when the type of publication is a classic post.
Then, when I create a new post, I can choose one or more ‘training’ custom post to display them at the end of the post (in theory).
Now, when I try to display the fields of each custom post, I used this code in the single.php af the child theme :
$articles = get_field('name_of_the_training');
if( $articles ):
echo "<div><h2>Featured:</h2><ul>";
foreach( $articles as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
echo "<li><a target='blank' href=" .get_field('link'). ">" .the_title(). "</a><br/>";
echo "<span>" .get_field('link'). "</span><br/>";
echo "<span>" .get_field('number_of_days'). " days</span><br/>";
echo "<span>" .get_field('price'). " euros</span><br/>";
echo "</li>";
endforeach;
echo "</ul></div>";
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif;
All the information of the custom post featured are displaying but not correctly : in the footer af the page.
Then I’ve used Genesis hook to display the informations at the right place (before the entry footer). Her is my code:
add_action('genesis_entry_footer', 'add_training');
function add_training (){
$articles = get_field('name_of_the_training');
if( $articles ):
echo "<div><h2>Featured:</h2><ul>";
foreach( $articles as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
echo "<li><a target='blank' href=" .get_field('link'). ">" .the_title(). "</a><br/>";
echo "<span>" .get_field('link'). "</span><br/>";
echo "<span>" .get_field('number_of_days'). " days</span><br/>";
echo "<span>" .get_field('price'). " euros</span><br/>";
echo "</li>";
endforeach;
echo "</ul></div>";
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif;
} ;
At this point, I’ve the title of the actual post and only the words “days” and “euros” who are display and not the fields of my custom post.
Did you know where I am wrong?
PS: I’ve write the code in the single.php file but, if I could made this in the function.php file, it would be nice.
Thank you,
Jl
did/would this work? :
<?php
$name_of_the_training = get_posts(array(
'post_type' => 'training', //search posttypes of this name
'meta_query' => array(
array(
'key' => 'name_of_the_training', // with a custom field of this name (has to be a relationship field!)
'value' => '"' . get_the_ID() . '"', // look for post that has this ID (matches exaclty "123", not just 123. This prevents a match for "1234")
'compare' => 'LIKE'
)
)
));
if ($name_of_the_training ) {
foreach( $name_of_the_training as $training){
echo "<span>" .get_field('number_of_days', $training->ID). " days</span><br/>";
echo "<span>" .get_field('price', $training->ID). " euros</span><br/>";
}
}
?>
The topic ‘ACF and Genesis’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.