Home › Forums › General Issues › ACF Within Loop
I’m hoping someone can help me with this.
I’m trying to create the following functionality:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<strong><?php if(get_field('pick_your_theme') == "LCBU Trends") { ?></strong>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
The above works as a general loop for a site. Unfortunately, the site I’m working on is a theme purchased by the client. The site itself is like “build-a-bear” and it’s a large open php string for the whole page.
I believe the ACF goes somewhere within the section of php below, but I can’t get it to work no matter what I do.
<div class="content-wrapper <?php echo $sidebar_class; ?>">
<div class="page-wrapper">
<?php
$left_sidebar = get_post_meta( $post->ID , "page-option-choose-left-sidebar", true);
$right_sidebar = get_post_meta( $post->ID , "page-option-choose-right-sidebar", true);
echo "<div class='gdl-page-float-left'>";
echo "<div class='gdl-page-item'>";
global $page_background;
$page_background = get_post_meta( $post->ID, "page-option-enable-background", true);
// Page title and content
$gdl_show_title = get_post_meta($post->ID, 'page-option-show-title', true);
$gdl_show_content = get_post_meta($post->ID, 'page-option-show-content', true);
if ( have_posts() ){
while (have_posts()){ the_post();
if( $gdl_show_title != "No" ){
echo '<div class="sixteen columns mb0">';
echo '<div class="page-header-wrapper">';
echo '<h1 class="page-header-title title-color gdl-title">' . get_the_title() . '</h1>';
echo '<div class="header-gimmick mr0"></div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '</div>'; // sixteen columns
}
if( $page_background != 'No' ){
echo "<div class='sixteen columns'>";
echo '<div class="page-bkp-frame-wrapper">';
echo '<div class="page-bkp-frame">';
}
$content = get_the_content();
// Show content
if( $gdl_show_content != 'No' && !empty($content) ){
echo '<div class="sixteen columns">';
echo '<div class="gdl-page-content">';
echo '<div class="bkp-frame-wrapper">';
echo '<div class="bkp-frame p20">';
the_content();
echo '</div>';
echo '</div>';
echo '</div>'; // page-content
echo '</div>'; // sixteen columns
}
} // while loop
} //if have posts
If anyone has any idea how to get this to work that would be fantastic. I can provide the whole page code if needed in an attachment.
Your code shows no use of the get_field or the_field functions. Perhaps this is why no values are being shown?
Can you please read over the getting started chapter on the documentation page?
Thanks
E
Sorry, there is nothing within the second code pasted because I’ve tried multiple times to add the
<?php if(get_field('pick_your_theme') == "LCBU Trends") { ?>
within the document and all it does is break the page layout as well as doesn’t filter correctly. The issue here is that how the theme author has created the page in one giant open PHP function rather than breaking it up (as in example code 1 above) where I can easily insert the selector get_field.
I’ve attached an attachment of the PHP document I am trying to edit so you can have a clear understanding.
The basic functionality is that I have built a custom field on every post to choose where to place the post. Thus, if they choose “Option A” in the post, then the code needs to check for that option to be true before passing all of the content related to the post (Title, Content, Images, etc.)
The topic ‘ACF Within Loop’ 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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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.