0
votes

I am using Advanced Custom Fields and Facet WP.

Currently there is a directory listing showing on a page that shows a business preview by displaying a business title, image and an excerpt - the same as you typically see from a post archive page.

I have added a new ACF checkbox field 'Are you ready to publish' and would like to amend the php so the Title, excerpt an image will only show for user profile listings that have checked 'yes' to publish.

I am fairly new to php and ACF and cannot get it to work.

I have tried to variations:

<?php
global $post;
?>

<div class="business-directory-results">
    
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>


     <?php while ( have_posts() ): the_post(); ?>

     <a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
            <img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
            <div class="business-directory-result-content">
            <h2><?php the_field('meta-business_name');?></h2>
            <?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
            <p><?php echo $excerpt; ?></p>
            </div>
            </a>

     <?php endwhile; ?>

<?php else { ?>
            
<!--            do nothing -->
            
<?php } ?>
            


</div>

and

 <?php
    global $post;
    ?>
    
    <div class="business-directory-results">    
    
    <?php while ( have_posts() ): the_post(); ?>

         <?php if( get_field('ready_to_publish') == 'yes' ) { ?>


         <a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
                <img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
                <div class="business-directory-result-content">
                <h2><?php the_field('meta-business_name');?></h2>
                <?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
                <p><?php echo $excerpt; ?></p>
                </div>
                </a>

    
        <?php else { ?>
                
    <!--            do nothing -->
                
  

      <?php } ?>
                    
    <?php endwhile; ?>
</div>
1
Did you add the ACF field first? If you create posts first and then add an additional ACF field, they will not work. To make it work, you will need to update all your posts after adding an ACF field.Khalilullah
And don't place the condition outside of the loop. That means your second variation is correct.Khalilullah
@Khalilullah thanks for your help. I created the ACF field after. When opening an existing user profile, I can see the field there and have set the checkbox field to 'yes' by default. When testing either variation, I just get a black screen. Do I need to open each profile and just click save again?Mr Toad
Yes. The default value doesn't mean they are attached in the post. You will need to update each post one by one.Khalilullah
In your case, the post means the user profile.Khalilullah

1 Answers

1
votes

If you create posts first and then add an additional ACF field, they will not work. To make it work, you will need to update all your posts after adding an ACF field. In your case, you will need update each user profile and only then they will have the ACF field attached with them.

Don't place the condition outside of the loop. That means your second variation is correct.

<?php
    global $post;
    ?>
    
    <div class="business-directory-results">    
    
    <?php while ( have_posts() ): the_post(); ?>

         <?php if( get_field('ready_to_publish') == 'yes' ) { ?>


         <a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
                <img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
                <div class="business-directory-result-content">
                <h2><?php the_field('meta-business_name');?></h2>
                <?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
                <p><?php echo $excerpt; ?></p>
                </div>
                </a>

    
        <?php else { ?>
                
    <!--            do nothing -->
                
  

      <?php } ?>
                    
    <?php endwhile; ?>
</div>