通过博客循环传递高级自定义字段

时间:2014-01-27 15:35:24

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

我希望有人可以帮助我。我试图将ACF集成到客户购买的主题中。

基本功能是我在每个帖子上都建立了一个自定义字段来选择放置帖子的位置。因此,如果他们在帖子中选择“选项A”,则代码需要在传递与帖子相关的所有内容(标题,内容,图像等)之前检查该选项是否为真。

这在标准php文档中如何工作的基本代码示例如下:

<?php if (have_posts()): while (have_posts()) : the_post(); ?>
   <?php if(get_field('pick_your_theme') == "Theme1") { ?>

         <h2><?php the_title(); ?></h2>
         <p><?php the_excerpt(); ?></p>

   <?php } ?><?php endwhile; ?>
<?php endif; ?>

这里的问题是主题作者在一个巨大的开放PHP函数中创建了页面,而不是将其分解(如上面的示例代码1所示),我可以在其中轻松插入选择器get_field。

以下是该页面的PHP代码的链接:

http://pastebin.com/wyWQiQvx

2 个答案:

答案 0 :(得分:0)

通常,您应该已经能够使用以下代码访问循环内的ACM字段:

<?php the_field('NAME_OF_FIELD'); ?>

答案 1 :(得分:0)

在@ ChrisHerbert的评论的基础上,将你的条件隐藏在那里应该是一件简单的事情,所以代码的循环部分看起来像这样:

if ( have_posts() ){ while (have_posts()){ the_post();
            if(get_field('pick_your_theme') == "Theme1") {  
                                            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
                                            }

                             } //pick_your_theme conditional

                      } // while loop

         } //if have posts 

显然,您可以自定义'pick_your_theme'条件中的代码以满足您的需求,并根据需要添加} elseif(get_field('pick_your_theme') == "Some_Other_Theme") {或默认值,以便'pick_your_theme'未设置} else {块。

相关问题