【问题标题】:Post Archive page - If else statement advanced custom fields发布存档页面 - If else 语句高级自定义字段
【发布时间】:2021-03-22 23:11:17
【问题描述】:

我正在使用高级自定义字段和 Facet WP。

目前有一个目录列表显示在一个页面上,该页面通过显示业务标题、图像和摘录来显示业务预览 - 与您通常在帖子存档页面中看到的相同。

我添加了一个新的 ACF 复选框字段“您准备好发布了吗”,并希望修改 php,以便标题、图片摘录仅显示已选中“是”发布的用户配置文件列表。

我对 php 和 ACF 还很陌生,无法让它工作。

我尝试过变体:

<?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>

 <?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>

【问题讨论】:

  • 您是先添加ACF字段吗?如果您先创建帖子,然后添加额外的 ACF 字段,它们将不起作用。要使其正常工作,您需要在添加 ACF 字段后更新所有帖子。
  • 并且不要将条件放在循环之外。这意味着您的第二个变体是正确的。
  • @Khalilullah 感谢您的帮助。之后我创建了 ACF 字段。打开现有用户配置文件时,我可以在那里看到该字段,并且默认情况下将复选框字段设置为“是”。在测试任何一种变体时,我都会遇到黑屏。我是否需要打开每个配置文件并再次点击保存?
  • 是的。默认值并不意味着它们附加在帖子中。您将需要一一更新每个帖子。
  • 在您的情况下,帖子表示用户个人资料。

标签: php wordpress if-statement advanced-custom-fields


【解决方案1】:

如果您先创建帖子,然后添加额外的 ACF 字段,它们将不起作用。要使其正常工作,您需要在添加 ACF 字段后更新所有帖子。在您的情况下,您需要更新每个用户个人资料,然后他们才会附上 ACF 字段。

不要将条件放在循环之外。这意味着您的第二个变体是正确的。

<?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>

【讨论】:

  • 所以我检查并更新了每个用户,然后尝试了第二个变体,但我仍然得到黑屏。我也看不到控制台中的任何错误来帮助我。
  • @MrToad “黑屏”对我来说并不熟悉。请给个截图好吗?如果您怀疑有任何错误,可以查看服务器中的错误日志。
猜你喜欢
  • 2014-10-13
  • 1970-01-01
  • 2015-09-27
  • 2012-01-17
  • 2016-01-15
  • 2013-10-23
  • 2015-01-17
  • 1970-01-01
  • 2017-09-14
相关资源
最近更新 更多