WooCommerce在单个产品页面上分页 - 但仅限于父类别


WooCommerce pagination on single product pages - but only inside parent category

我想在WooCommerce中的每个产品页面上进行分页,以便用户可以更轻松地在该类别中的产品之间移动,而不必每次都返回主类别页面。

我知道可以使用标准的WordPress分页链接,例如...

<?php previous_post_link('&laquo; %link'); ?>
<?php next_post_link('%link &raquo;'); ?>

如果我想翻阅所有产品,但我只想翻阅我所属类别的产品,这很有用。有谁知道我如何限制这一点,以便不包括此类别以外的产品?

我尝试使用 WordPress codex 中提到的 in_same_term 参数来获取链接,仅在下一个/上一个产品属于同一类别时显示链接,但由于某种原因它返回一个整数。这是我正在使用的代码...

<?php next_post_link( '%link', '%title', TRUE, '' ); ?>

即使它遵循 Codex 结构,也不会返回任何内容。我也试过...

<?php next_post_link( '%link %title', TRUE, '' ); ?>

这就是我得到的回报...

1 %title

我很困惑下一步该去哪里。

这是我最近编写的一个函数,它也可以完成这项工作并且非常灵活

这个想法:

您首先需要获取当前的帖子 ID,我通过 get_queried_object_id() 获得该 ID。帖子 ID 将用于检索:

  • 帖子所属的职位条款与 wp_get_post_terms() .为了加快速度,只会返回条款的 ID。将使用第一个 ID(您可以在此处修改代码以决定如果帖子有多个术语将使用哪个术语(,这将用于检索具有此特定术语的所有帖子

  • 与此帖子直接相邻的帖子的帖子
  • ID,用于确定和检索此帖子的下一个和上一个帖子

上述所有信息都将在tax_query中使用,get_posts从当前帖子中检索共享该术语的所有帖子。在函数中,默认分类categorypost_type设置为 any,以获取具有此特定术语的所有帖子

同样,为了使代码更快并保护资源,我们只会获得帖子 ID,因为这就是所需要的

现在是代码的重要部分。我们现在需要确定以下内容:

  • 当前帖子在自定义get_posts查询返回的帖子 ID 数组中的当前位置。此处使用的函数array_search

  • 如果在此帖子之前或之后有帖子(下一个或上一个帖子,定义与内置函数next_post_link()previous_post_link()相同(,则获取这些帖子的 ID

  • 使用 ID with get_post从当前帖子中检索下一篇文章和上一篇文章的标题

最后是返回链接。如果当前帖子是数组中的第一个或最后一个帖子,并且没有下一个或上一个帖子,我已经设置了消息。您可以决定要在此处执行的操作,对于所有重要的操作,其余代码

为了使代码更快、更高效,我使用了瞬态 API,您可以进一步阅读。我还使用 transition_post_status 动作钩子来挂钩一个函数,以便在帖子状态发生变化时删除这些瞬态。这包括正在发布的新帖子、正在更新的帖子以及已删除/取消删除的帖子

代码:

这是代码。这将进入您的函数.php

function get_post_link( $taxonomy = 'category', $post_type = [ 'any' ] ) {
    $id             = get_queried_object_id(); // Get the current post ID
    $transient_id   = 'post_number_' . md5( $id . $taxonomy . implode( ',', $post_type ) ); //Create a unique transient id
    if ( false === ( $links = get_transient( $transient_id ) ) ) {
        // Get the terms a post belongs to
        $terms = wp_get_post_terms( $id, $taxonomy, array( 'fields' => 'ids' ) ); 
        // Use a tax_query to get all posts from the given term
        // Just retrieve the ids to speed up the query
        $post_args = [ 
            'post_type'         => $post_type,
            'fields'            => 'ids',
            'posts_per_page'    => -1,
            'tax_query'         => [
                [
                    'taxonomy'          => $taxonomy,
                    'field'             => 'term_id',
                    'terms'             => $terms[0],
                    'include_children'  => false,
                ],
            ],
        ];
        // Get all the posts having the given term from all post types
        $q = get_posts( $post_args );
        //Get the current post position. Will be used to determine next/previous post
        $current_post_position = array_search( $id, $q );
        // Get the previous/older post ID
        if ( array_key_exists( $current_post_position + 1 , $q ) ) {
            $previous = $q[$current_post_position + 1];
        }
        // Get post title link to the previous post
        if( isset( $previous ) ) {
            $previous_post      = get_post( $previous );
            $previous_post_link = get_permalink( $previous );
            $previous_title     = '<a href="' . $previous_post_link . '">' . $previous_post->post_title . '</a></br>';
        }
        // Get the next/newer post ID
        if ( array_key_exists( $current_post_position - 1 , $q ) ) {
            $next = $q[$current_post_position - 1];
        }
        // Get post title link to the next post
        if( isset( $next ) ) {
            $next_post      = get_post( $next );
            $next_post_link = get_permalink( $next );
            $next_title     = '<a href="' . $next_post_link . '">' . $next_post->post_title . '</a></br>';?><pre><?php var_dump($next_title); ?></pre><?php 
        }
        // The returned post links 
        if( isset( $previous_title, $next_title ) ) {
            $links = [
                'previous_post' => $previous_title, 
                'next_post'     => $next_title, 
            ];
        }elseif( !isset( $previous_title ) && $next_title ) {
            $links = [
                'previous_post' => 'You are currently viewing the newest post', 
                'next_post'     => $next_title, 
            ];
        }elseif( $previous_title && !isset( $next_title ) ) {
            $links = [
                'previous_post' => $previous_title, 
                'next_post'     => 'You are currently viewing the last post', 
            ];
        }
        set_transient( $transient_id, $links, 7 * DAY_IN_SECONDS );
    }
    return (object)$links;
}
add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
        global $wpdb;
        $wpdb->query( "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient%_post_number_%')" );
        $wpdb->query( "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient_timeout%_post_number_%')" );
}, 10, 3 );

如何使用:

您现在可以在单个.php中按如下方式使用代码。默认分类为category,帖子类型为any。如果你的自定义分类被称为 mytax ,你可以像这样使用代码

if( function_exists( 'get_post_link' ) ) {          
    $post_links = get_post_link( 'mytax' );
    echo $post_links->previous_post . '</br>' . $post_links->next_post;
}