Сравнение поля ACF в цикле WordPress, чтобы увидеть, совпадают ли они
У меня есть цикл WordPress, который тянет поле ACF. Мне нужно определить, совпадают ли имена полей, и если да, то я хочу обернуть их в div. Я создал пользовательскую индексную страницу, но мы хотим иметь возможность стилизовать поля с тем же именем автора, что и в раскрывающемся списке. Поэтому мне нужно как-то сравнить, если они одинаковы.
Это сайт, над которым я работаю http://test.improveyourenglish.com/library/ Так, например, я бы хотел обернуть "Джейн Остин" в 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>';
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 -->