按分类法对WordPress自定义帖子进行分组

时间:2015-07-13 11:06:17

标签: php wordpress

我有一个名为“产品”的自定义帖子类型,它有两个自定义分类 - “产品系列”和“产品类别”。该范围用作顶级分组,而该类别是其中的子分组。

我已经设置了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>

1 个答案:

答案 0 :(得分:3)

简单删除您拥有的代码并将其替换为默认循环。您不应该使用自定义查询替换主查询。使用pre_get_posts根据需要更改主查询。

这是您的分类页面应该是什么

if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();

        // Your template tags and markup

    }
}

由于您的问题是排序,我们将使用usortthe_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中的代码 - &gt;在0.7940 s中的318 db查询,页面生成时间为1.1670s。内存使用量为12.8Mb

  • 我的回答代码 - &gt;在0.1045 s内有46个db查询,页面生成时间为0.1305s。内存使用量为12.6Mb

正如我先前所述,证据是在布丁中