dongzhizhai4070 2015-07-28 09:27 采纳率: 0%
浏览 70
已采纳

在Wordpress中对自定义分类术语页面进行排序

I have created a custom post type for a product with two custom taxonomies, "colour" and "style". Products are tagged in both of these. When you go to the taxonomy term page it works as it should i.e. if you go to /colour/blue it lists all blue products.

What I want to happen is when you go to /colour/blue is lists all blue products BUT groups them by the second taxonomy "style" showing the first three products with a read more link.

So

Blue > Product Style 1

  • Product 1
  • Product 2
  • Product 3

See More..

Blue > Product Style 2

  • Product 3
  • Product 4
  • Product 5

See More..

Does anyone know if this is possible?

Current code is this, I've created a taxonomy template for the term taxonomy-colour.php

<?php get_header(); ?>

<main role="main" class="blinds">

    <h1 class="main-product-title">Products</h1>

    <!-- section -->
    <section class="main-product-content">

        <h2><?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h2>

        <?php if (have_posts()): while (have_posts()) : the_post(); ?>

            <!-- article -->
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <!-- post image -->
                <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>

                        <?php the_post_thumbnail(array(720,400)); // Declare pixel size you need inside the array ?>

                <?php endif; ?>
                <!-- /post thumbnail -->

                <!-- post title -->
                <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                <!-- /post title -->

                <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">View ></a></p>

            </article>

            <!-- /article -->

        <?php endwhile; ?>

        <?php else: ?>

            <!-- article -->
            <article>
                <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
            </article>
            <!-- /article -->

        <?php endif; ?>

        <?php get_template_part('pagination'); ?>

    </section>
    <!-- /section -->

    <div class="sidebar-wrapper">
        <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('product-sidebar')) ?>
    </div>

</main>
  • 写回答

2条回答 默认 最新

  • dongtu7567 2015-07-29 16:07
    关注

    As stated in comments, you need to use usort() on the the_posts filter to sort your posts according to needs.

    You can try the following: (NOTE: The following code is untested and requires PHP 5.4+ due to array dereferencing)

    add_filter( 'the_posts', function ( $posts, $q ) 
    {
        if (    $q->is_main_query() // Target only the main query
              && $q->is_tax( 'colour' ) // Only target the colour 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 term name for sorting. We will sort by style taxonomy term
                $array_a = get_the_terms( $a->ID, 'style' );
                $array_b = get_the_terms( $b->ID, 'style' );
    
                // 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 ); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动