【问题标题】:Sorting custom taxonomy term page in Wordpress在 Wordpress 中对自定义分类术语页面进行排序
【发布时间】:2015-07-28 09:27:00
【问题描述】:

我为具有两种自定义分类法“颜色”和“样式”的产品创建了自定义帖子类型。产品在这两个中都被标记。当您转到分类术语页面时,它会正常工作,即如果您转到 /colour/blue,它会列出所有蓝色产品。

我想要发生的是,当您转到 /colour/blue 时会列出所有蓝色产品,但按第二个分类“样式”对它们进行分组,显示前三个产品并带有阅读更多链接。

所以

蓝色>产品风格1

  • 产品1
  • 产品 2
  • 产品 3

查看更多..

蓝色>产品风格2

  • 产品 3
  • 产品 4
  • 产品 5

查看更多..

有谁知道这是否可行?

当前代码是这样的,我已经为术语 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>

【问题讨论】:

  • 到目前为止你有什么代码?
  • 您好,我已经为分类法创建了一个模板,即 taxonomy-colour.php 并将其放在上面。
  • 我之前已经回答过类似的问题,无论是在这里还是在WordPress Development,只是不记得在哪里,但本质上,您需要使用the_posts 过滤器并使用usort() 处理帖子
  • 谢谢。我将查看 the_posts 过滤器,看看是否可以将其应用于我的问题。

标签: php wordpress


【解决方案1】:

如 cmets 中所述,您需要在 the_posts 过滤器上使用 usort() 以根据需要对帖子进行排序。

您可以尝试以下方法:(注意:以下代码未经测试,由于数组取消引用,需要 PHP 5.4+)

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 ); 

【讨论】:

  • 您应该使用新信息开始一个新问题。限制您的帖子与此代码无关,您需要使用 pre_get_posts 过滤主查询
【解决方案2】:

首先要做的是为您当前的分类术语获取可用的style术语。为此,您可以获取当前分类的所有帖子 ID,然后使用 wp_get_object_terms 从它们那里获取 style 术语。然后你遍历它们,查询三个具有这两个分类术语(colourstyle)的帖子。

以下是您可以用于您的案例的示例:

<?php
// get the term...
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

// get all posts from this term and store the IDs
$term_posts = get_posts(array('tax_query' => array(array(
    'taxonomy' => get_query_var('taxonomy'),
    'terms' => $term->ID
))));
$term_posts_IDs = array();
foreach ($term_posts as $term_post) {
    $term_posts_IDs[] = $term_post->ID;
}

// get the available styles from this set of posts
$styles = wp_get_object_terms($term_posts_IDs, 'style');

?>
<h2><?php echo $term->name; ?></h2>
<?php 
if(!empty($styles)): // styles are availables for this term
    foreach($styles as $style):
        ?>
        <h3><?php echo $term->name; ?> &gt; <?php echo $style->name; ?></h3>
        <?php
        // get the first three products with colour + style
        $tax_posts = get_posts(array(
            'posts_per_page' => 3,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => get_query_var('taxonomy'),
                    'terms' => $term->ID
                ),
                array(
                    'taxonomy' => 'style',
                    'terms' => $style->ID
                )
            )
        ));
        ?>
        <ul>
        <?php foreach($tax_posts as $tax_post): ?>
            <li><?php echo $tax_post->post_title; ?></li>
        <?php endforeach; ?>
        </ul>
        <!-- ... link to your style taxonomy template here ... -->
    <?pgpp endforeach ; ?>
<?php else : ?>
    <!-- ... default behaviour ... -->
<?php endif; ?>

您必须通过发送 style 术语 color 术语到您的 style 分类模板。

【讨论】:

  • 这非常低效,为什么你在运行自定义查询
  • @PieterGoosen 我很乐意看到你对这个问题的回答,但遗憾的是我没有看到任何。
  • 今晚晚些时候我会发布一些东西。在手机上编码这样的东西是一场噩梦。 ;-)
  • @PieterGoosen 我发现了您的类似帖子(链接发布在下面),但我仍然很困惑,无法将其应用于我的问题。你能提供解决方案吗? :) stackoverflow.com/questions/31381726/…
  • 我已经剥离并修改了该代码。我没有测试过,但你应该明白。请注意,您需要 PHP 5.4+ @user1837290
猜你喜欢
  • 2012-07-11
  • 2012-08-09
  • 1970-01-01
  • 2016-10-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
相关资源
最近更新 更多