2017-08-27 51 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 --> 
相關問題