我有一个名为“产品”的自定义帖子类型,它具有两个自定义分类法:“产品范围”和“产品类别”.该范围用作顶级分组,而类别是其中的子分组.
我已经建立了一个taxonomy-product-range.PHP模板,该模板具有以下代码:
<?PHP
$terms = get_terms('product-categories');
foreach( $terms as $term ):
?>
<h2><?PHP echo $term->name;?></h2>
<ul>
<?PHP
$posts = get_posts(array(
'post_type' => 'products',
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'nopaging' => true
));
foreach($posts as $post): setup_postdata($post);
?>
<li><a href="<?PHP the_permalink(); ?>"><?PHP the_title(); ?></a></li>
<?PHP endforeach; ?>
</ul>
<?PHP endforeach; ?>
通过输出产品并将其按产品类别分组,可以按预期工作.但是,它会输出所有产品,而不管您正在查看哪个存档.我只需要为您正在查看的档案输出帖子即可.
感觉好像快要出现了,但是我不确定如何解决它.
==编辑==
每个产品将属于一个“范围”和一个“类别”.当人们访问产品范围的存档页面时,我尝试显示以下内容:
<h1>Range Title</h1>
<h2>Category 1 Title</h2>
<ul>
<li>Product 1 Title</li>
<li>Product 2 Title</li>
<li>Product 3 Title</li>
</ul>
<h2>Category 2 Title</h2>
<ul>
<li>Product 4 Title</li>
<li>Product 5 Title</li>
<li>Product 6 Title</li>
</ul>
解决方法:
只需删除您拥有的代码,然后将其替换为默认循环即可.您不应将主查询替换为自定义查询.使用pre_get_posts根据需要更改主查询.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your template tags and markup
}
}
当您的问题是排序时,我们将使用usort和thee_posts过滤器解决此问题,以在循环运行之前但在主查询运行之后进行排序.我们不会使用多个循环,因为它们非常昂贵且占用大量资源,并且会破坏页面功能
我已经注释了该代码,因此可以轻松理解和理解. (注意:以下代码未经测试,由于数组取消引用而需要PHP 5.4)
add_filter( 'the_posts', function ( $posts, $q )
{
$taxonomy_page = 'product-range';
$taxonomy_sort_by = 'product-categories';
if ( $q->is_main_query() // Target only the main query
&& $q->is_tax( $taxonomy_page ) // Only target the product-range taxonomy term pages
) {
/**
* There is a bug in usort that will most probably never get fixed. In some instances
* the following PHP Warning is displayed
* usort(): Array was modified by the user comparison function
* @see https://bugs.PHP.net/bug.PHP?id=50688
* The only workaround is to suppress the error reporting
* by using the @ sign before usort
*/
@usort( $posts, function ( $a, $b ) use ( $taxonomy_sort_by )
{
// Use term name for sorting
$array_a = get_the_terms( $a->ID, $taxonomy_sort_by );
$array_b = get_the_terms( $b->ID, $taxonomy_sort_by );
// Add protection if posts don't have any terms, add them last in queue
if ( empty( $array_a ) || is_wp_error( $array_a ) ) {
$array_a = 'zzz'; // Make sure to add posts without terms last
} else {
$array_a = $array_a[0]->name;
}
// Add protection if posts don't have any terms, add them last in queue
if ( empty( $array_b ) || is_wp_error( $array_b ) ) {
$array_b = 'zzz'; // Make sure to add posts without terms last
} else {
$array_b = $array_b[0]->name;
}
/**
* Sort by term name, if term name is the same sort by post date
* You can adjust this to sort by post title or any other WP_Post property_exists
*/
if ( $array_a != $array_b ) {
// Choose the one sorting order that fits your needs
return strcasecmp( $array_a, $array_b ); // Sort term alphabetical ASC
//return strcasecmp( $array_b, $array_a ); // Sort term alphabetical DESC
} else {
return $a->post_date < $b->post_date; // Not sure about the comparitor, also try >
}
});
}
return $posts;
}, 10, 2 );
编辑
if ( have_posts() ) {
// display the range term title
echo '<h1>' . get_queried_object()->name . '</h1>';
// Define the variable which will hold the term name
$term_name_test = '';
while ( have_posts() ) {
the_post();
global $post;
// Get the terms attached to a post
$terms = get_the_terms( $post->ID, 'product-categories' );
//If we don't have terms, give it a custom name, else, use the first term name
if ( empty( $terms ) || is_wp_error( $terms ) ) {
$term_name = 'SOME CUSTOM NAME AS FALL BACK';
} else {
$term_name = $terms[0]->name;
}
// display term name only before the first post in the term. Test $term_name_test against $term_name
if ( $term_name_test != $term_name ) {
// Close our ul tags if $term_name_test != $term_name and if not the first post
if ( $wp_query->current_post != 0 )
echo '</ul>';
echo '<h2>' . $term_name . '</h2>';
// Open a new ul tag to enclose our list
echo '<ul>';
} // endif $term_name_test != $term_name
$term_name_test = $term_name;
echo '<li>' . get_the_title() . '</li>';
// Close the ul tag on the last post
if ( ( $wp_query->current_post + 1 ) == $wp_query->post_count )
echo '</ul>';
}
}
编辑2
上面的代码现已通过测试并可以正常工作.根据要求,这是在本地安装上运行的测试.对于此测试,我使用了OP中的代码和我的代码.
结果
(此结果是使用Query Monitor Plugin获得的.此外,所有结果都包括小部件,导航菜单,自定义功能等所做的相同的额外查询)
> OP中的代码->在0.7940 s内有318个数据库查询,页面生成时间为1.1670s.内存使用率为12.8Mb
>我的答案代码-> 0.1045秒内有46个数据库查询,页面生成时间为0.1305s.内存使用率为12.6Mb
正如我之前所说的,证明在布丁里