简体   繁体   English

发布存档页面 - If else 语句高级自定义字段

[英]Post Archive page - If else statement advanced custom fields

I am using Advanced Custom Fields and Facet WP.我正在使用高级自定义字段和 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.我添加了一个新的 ACF 复选框字段“您准备好发布了吗”,并希望修改 php,以便标题、摘录图像仅显示已选中“是”发布的用户配置文件列表。

I am fairly new to php and ACF and cannot get it to work.我对 php 和 ACF 还很陌生,无法让它工作。

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>

If you create posts first and then add an additional ACF field, they will not work.如果您先创建帖子,然后添加额外的 ACF 字段,它们将不起作用。 To make it work, you will need to update all your posts after adding an ACF field.要使其正常工作,您需要在添加 ACF 字段后更新所有帖子。 In your case, you will need update each user profile and only then they will have the ACF field attached with them.在您的情况下,您将需要更新每个用户配置文件,然后他们才会附上 ACF 字段。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM