下面的代码是一个WordPress书店网站的代码,该网站在每个书页上输出关于作者的信息,从相应的作者页面中提取内容。它在大多数情况下都很好,除非有一个以上的作者,它只显示一个作者(有时不是主要作者)。
是否有一种方法来修改它,以便如果有多个作者,它会显示所有作者的信息?
谢谢!
<?php if ( is_single() ) { ?>
<div class="featured-author">
<div class="widget widget_lpcode">
<h2 class="widget-title">About the Author</h2>
<div class="textwidget">
<?php
$authors = array();
$parents = array(
'Author' => 35
);
$categories = get_the_terms( $post->ID, 'product_cat' );
foreach( $parents as $parent_name => $parent_id ):
foreach( $categories as $category ):
if( $parent_id == $category->parent ):
$authors[] = $category->slug;
endif;
endforeach;
endforeach;
$custom_query = new WP_Query( array( 'post_type' => 'authors','post_name__in' => $authors,'posts_per_page' => '-1' ) );
if($custom_query->have_posts()) :
while($custom_query->have_posts()) :
$custom_query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<header class="entry-header">
<h1 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>
</header>
<div class="entry-content">
<p><?php get_the_content_limit(115, ''); ?></p>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" class="more">More</a></p>
</div>
</article>
<?php
endwhile;
else:
?>
Not Found.
<?php
endif;
?>
</div>
</div>
</div>发布于 2016-03-09 16:22:07
目前,您正在遍历每个类别,然后将相同的$author变量分配给该类别的段塞,因此,如果有多个类别,则每次都要重写该$author变量,结果将等于最后一个结果。
首先,建立一个空白作者数组:
$authors = array();
然后,在foreach循环中,将结果添加到该数组中:
$authors[] = $category->slug;
最后,在$custom_query WP_Query参数中,您需要更改查找帖子的方式,因为'name‘参数只接受一个片段。在WP4.4中有一个接受数组的新post_name__in参数,所以您可以使用
'post_name__in' => $authors,
如果不能使用WP4.4,则必须获取作者数组中每个帖子的If,然后使用接受If数组的post__in参数。
另外,将您的“posts_per_page”参数从1更改为-1,这样它将显示所有结果。
https://stackoverflow.com/questions/35895309
复制相似问题