【问题标题】:Comparing an ACF field within a WordPress loop to see if they are the same比较 WordPress 循环中的 ACF 字段以查看它们是否相同
【发布时间】:2017-08-31 03:49:33
【问题描述】:

我有一个正在拉取 ACF 字段的 WordPress 循环。我需要确定字段名称是否相同,如果是,我想将它们包装在一个 div 中。我已经创建了一个自定义索引页面,但我们希望能够将具有相同作者姓名的字段设置为下拉列表。所以我需要以某种方式比较它们是否相同。

这是我正在开发的网站http://test.improveyourenglish.com/library/ 因此,例如,我想将“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>';

【问题讨论】:

  • 不清楚您要做什么。您还想比较 ACF 领域的什么?什么是“Jane Austen” - 页面标题/术语/ACF 字段?
  • 该页面由自定义帖子类型“书籍”组成,然后我有一个自定义字段“作者姓名”,它自动将“作者姓名”自定义字段的第一个字母链接到分类法“a ,b,c,d,....”。如果自定义字段“作者姓名”出现不止一次,我可能会过度思考如何执行此操作,然后我希望将作者姓名字段包装在 div 中,以便我可以不同地设置作者组的样式。
  • 下面的答案对你有用还是你还需要帮助?
  • 我决定以一种稍微不同的方式去做。但是,您的评论非常有效,帮助我确定了前进的方向。非常感谢您的帮助!
  • 没问题,很高兴你把它整理好了!如果回答有帮助,也许您可​​以接受它,以便将其标记为已解决,并且其他用户知道如果他们有同样的问题会有所帮助?见What should I do when someone answers my question?它也给了我们一些代表,并且有接受问题的历史有时可以鼓励对未来问题的回答:)

标签: php wordpress advanced-custom-fields genesis


【解决方案1】:

如果我正确理解您想要什么,您需要做的是将“当前”作者姓名记录在一个变量中,并在您的循环中使用它来将其与下一个作者姓名进行比较。如果是其他作者,则结束前一个作者的包装器并为下一个作者启动一个新的包装器。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多