【问题标题】:WooCommerce pagination on single product pages - but only inside parent category单个产品页面上的 WooCommerce 分页 - 但仅限于父类别内
【发布时间】:2015-02-15 19:51:17
【问题描述】:

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

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

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

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

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

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

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

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

这就是我得到的回报……

1 %title

我不知道下一步该去哪里。

【问题讨论】:

    标签: php wordpress pagination woocommerce woothemes


    【解决方案1】:

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

    想法:

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

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

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

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

    同样,为了使代码更快并确保资源安全,我们只需要获取帖子 ID,因为这就是我们所需要的全部

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

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

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

    • 使用带有get_post 的 ID 从当前帖子中检索下一个和上一个帖子的标题

    最后将返回链接。如果当前帖子是数组中的第一个或最后一个帖子并且没有下一个或上一个帖子,我已经设置了消息。你可以在这里决定你想做什么,剩下的就是代码

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

    代码:

    这里是代码。这进入你的functions.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 );
    

    如何使用:

    您现在可以在 single.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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多