2017-08-27 125 views
0

我有一个WordPress循环拉ACF字段。我需要确定字段名称是否相同,如果是这样我想将它们包装在一个div中。我创建了一个自定义索引页面,但我们希望能够使用与下拉菜单相同的作者姓名来设置字段样式。所以我需要以某种方式比较是否相同。比较一个WordPress循环内的ACF字段,看它们是否相同

这是我工作的网站​​ 因此,例如,我想将“Jane Austin”封装在div中,以便我可以将其设置为下拉菜单。

非常感谢这么多帮助。

这是我目前使用的

add_action('genesis_loop', 'book_archive_page'); 
function book_archive_page() { 
echo '<div class="left-side">'; 
echo '<p>The following titles are sorted by author surnames.</p>'; 
?><div class="enter"><a href="#$term->name"><?php echo $term->name; ?> 
</div></a><?php 
$post_type = 'book'; 

// Get all the taxonomies for this post type 
$taxonomies = get_object_taxonomies(array('post_type' => $post_type) 
); 

foreach($taxonomies as $taxonomy) : 

    // Gets every "category" (term) in this taxonomy to get the 
respective posts 
    $terms = get_terms($taxonomy); 

    foreach($terms as $term) : ?> 

     <section class="category-section"> 

     <div class="row"> 
     <div class="span12"> 
      <a name="<?php echo $term->name; ?>"><h2 style="padding- 
    top: 300px; margin-top: -300px;"><?php echo $term->name; ?></h2> 
</a> 




     </div> 

     <?php 
     $args = array(
       'post_type' => $post_type, 
       'posts_per_page' => -1, //show all posts 
       'tax_query' => array(
        array(
         'taxonomy' => $taxonomy, 
         'field' => 'slug', 
         'terms' => $term->slug, 
        ) 
       ) 

      ); 
     $posts = new WP_Query($args); 

     if($posts->have_posts()): while($posts->have_posts()) : 
    $posts->the_post(); ?> 

      <div class="span4"> 

       <article class="inner-post clearfix"> 



        <div class="inner-content"> 

        <a href="<?php echo get_permalink(); ?>" title="Read <?php echo get_the_title(); ?>"><div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php echo get_the_title(); ?></div></a> 


        </div> 
       </article> 


      </div> 

     <?php endwhile; endif; ?> 
     </div> 
     <hr> 
     </section> 

    <?php endforeach; 

endforeach; ?> 
<?php 
} 
echo '</div>'; 
+0

不清楚你正在尝试做什么。你想要比较ACF领域呢?什么是“简·奥斯汀” - 一个页面标题/术语/ ACF字段? – FluffyKitten

+0

页面由自定义文章类型“books”组成,然后我有一个自定义字段“author name”,它自动将“author name”自定义字段的第一个字母链接到分类“a,b,c,d, ....“。 如果自定义字段“作者姓名”出现多于一次,我可能会过度思考如何做到这一点,然后我想作者姓名字段被包装在一个div中,所以我可以以不同方式对作者群体进行样式设置。 –

+0

下面的答案是否适用于您或您是否仍需要帮助? – FluffyKitten

回答

0

代码如果我知道你想正确的,你需要做的是在一个变量记录“当前”作者姓名,并在使用它你循环将其与下一个作者姓名进行比较。如果它是一个不同的作者,那么结束前一作者的包装并为下一个作者开始一个新的包装。

$current_author = ""; // variable to store the current author 

$posts = new WP_Query($args); ?> 

<div class="author-wrapper"> <!-- start the wrapper div for the first author --> 

<?php if($posts->have_posts()): while($posts->have_posts()) : $posts->the_post(); ?> 

    <?php 
    // check if we have a new author 
    if (the_field('author_full_name') != $current_author) { 
     // update current_author var to make the new author the current one 
     $current_author = the_field('author_full_name'); 
     // close the previous author's wrapper and start a new one 
     ?> 
     </div> 
     <div class="author-wrapper"> 
    <?php } ?> 

     <div class="span4"> 
      <article class="inner-post clearfix"> 
       <div class="inner-content"> 

       <a href="<?php echo get_permalink(); ?>" title="Read <?php echo get_the_title(); ?>"><div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php echo get_the_title(); ?></div></a> 

       </div> 
      </article> 

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

</div> <!-- end the wrapper div for the last author --> 
相关问题