简体   繁体   English

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

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

I would like to put pagination on each single product page in WooCommerce so that a user can move between products in that category easier than having to go back out to the main category page every time. 我想在WooCommerce中的每个产品页面上进行分页,以便用户可以轻松地在该类别的产品之间移动,而不必每次都回到主类别页面。

I know it's possible to use the standard WordPress pagination links like… 我知道可以使用标准的WordPress分页链接,例如…

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

This works if I want to page through all products, but I only want to page through products that are within the category I'm in. Does anyone know how I can limit this so products outside this category aren't included? 如果我想浏览所有产品,但我只想浏览我所属类别内的产品,则此方法有效。有人知道我如何限制此范围,因此不包括该类别以外的产品吗?

I've tried using the in_same_term parameter as mentioned in the WordPress codex to get the links only showing if the next/prev product is in the same category, but it's returning an integer for some reason. 我尝试使用WordPress Codex中提到的in_same_term参数来获取仅显示next / prev产品是否属于同一类别的链接,但由于某种原因它返回整数。 Here's the code I'm using… 这是我正在使用的代码…

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

This returns nothing at all even though it follows the Codex structure. 即使遵循Codex结构,也不会返回任何内容。 I've also tried… 我也尝试过...

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

And this is what I'm getting in return… 这就是我得到的回报……

1 %title

I'm stumped where to go next. 我很困惑下一步该去哪里。

Here is a function that I have recently written that also does the job and is quite flexible 这是我最近编写的一个函数,它也可以完成工作并且非常灵活

THE IDEA: 想法:

You first need to get the current post id, which I get through get_queried_object_id() . 您首先需要获取当前的帖子ID,我可以通过get_queried_object_id()获得该ID。 The post ID will be used to retrieve: 帖子ID将用于检索:

  • The post terms the post belongs to with wp_get_post_terms() . 该帖子所属的帖子条款使用wp_get_post_terms() To speed things up, only the ID's of the terms will be returned. 为了加快速度,只会返回条款的ID。 The first ID will be used ( you can modify the code here to decide which term will be used if a post have more than one term ) and this will be used to retrieve all the posts which has this certain term 将使用第一个ID( 如果帖子包含多个术语,则可以在此处修改代码以决定使用哪个术语),并将使用 ID检索具有该特定术语的所有帖子

  • The post ID's of the posts that is directly adjacent to this post to determine and retrieve the next and previous post from this one 与该帖子直接相邻的帖子的帖子ID,以确定并从该帖子中检索下一个和上一个帖子

All the info above will be used in a tax_query with get_posts to retrieve all the posts that shares the term from the current post. 上面的所有信息都将在带有get_poststax_query使用,以从当前帖子中检索共享该术语的所有帖子。 In the function, the default taxonomy is category and the post_type is set to any to get all the posts that has this specific term 在函数中,默认分类法是category ,并且post_type设置为any以获取具有该特定术语的所有帖子

Again, to make the code faster and to safe on resources, we only going to get the post ID's as this is all that is needed 再说一次,为了使代码更快并且更安全地使用资源,我们只需要获取帖子ID,因为这就是所有需要的内容

Now comes the important parts of the code. 现在是代码的重要部分。 We now need to determine the following: 现在,我们需要确定以下内容:

  • The current position of the current post in the returned array of post ID's from the custom get_posts query. 自定义get_posts查询返回的帖子ID数组中当前帖子的当前位置。 The function used here is array_search 这里使用的函数是array_search

  • If there is a post before or after this post ( next or previous posts, the definitions are the same as for the build in functions next_post_link() and previous_post_link() ), get the ID's of these posts 如果在此帖子之前或之后有一个帖子( 下一个或上一个帖子,其定义与内置函数next_post_link()previous_post_link()的定义相同 ),请获取这些帖子的ID

  • Use the ID's with get_post to retrieve the next and previous post's titles from the current post 使用ID与get_post一起从当前帖子中检索下一个和上一个帖子的标题

Lastly will be to return the links. 最后将是返回链接。 I have set messages if the current post is either the first or last post in the array and there are no next or previous post. 如果当前帖子是数组中的第一个或最后一个帖子,并且没有下一个或上一个帖子,我已经设置了消息。 You can decide what you want to do here, and for all that matters, the rest of the code 您可以在这里决定要执行的操作,对于所有重要的事情,其余代码

To make the code even faster and more efficient, I have made use of the Transient API which you can read further on. 为了使代码更快,更高效,我使用了Transient API ,您可以进一步阅读。 I have also used the transition_post_status action hook to hook a function to delete these transients whenever the post status of a post change. 我还使用了transition_post_status动作钩子来钩住函数,以在帖子的帖子状态发生变化时删除这些瞬变。 This includes new posts being published, post being updated and post deleted/undeleted 包括发布新帖子,更新帖子以及删除/取消删除帖子

THE CODE: 编码:

Here is the code. 这是代码。 This goes into your functions.php 这进入您的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 );

HOW TO USE: 如何使用:

You can now use the code as follows in you single.php. 现在,您可以在single.php中使用以下代码。 The default taxonomy is category and post type is any . 默认分类法是category ,发布类型是any If your custom taxonomy is called mytax , you can use the code like this 如果您的自定义分类法称为mytax ,则可以使用如下代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅显示特定类别的WooCommerce产品页面 - Show WooCommerce product pages for only specific category Woocommerce面包屑,用单个产品页面上的页码替换产品类别 - Woocommerce Breadcrumbs, Replace Product Category with Page Number on Single Product Pages 为产品类别替换 Woocommerce 单一产品页面上的“添加到购物车”按钮 - Replace Add to cart button on Woocommerce Single Product Pages for a product category 在WooCommerce的单个帖子中获取产品类别的完整产品单页 - Get full products single pages for a product category on a single post in WooCommerce Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce 仅在 WooCommerce 单个产品页面上禁用可变产品价格范围 - Disable Variable Product Price Range on the WooCommerce Single Product Pages Only 在Woocommerce的任何页面上仅显示一个链接的产品类别文本 - Display only a single linked product category text on any page in Woocommerce 仅在 WooCommerce 单个产品页面上将添加到购物车文本更改为“应用” - Change Add to Cart text to “Apply” on WooCommerce single product pages only 仅在 WooCommerce 单个产品页面中隐藏某些产品的选项卡 - Hiding tabs only for some products in WooCommerce single product pages WooCommerce-如果单个产品页面上的产品类别声明 - WooCommerce - If product category statement on single product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM