带有分页的存档页面上的 wordpress 粘性帖子 [英] wordpress sticky post on archive page with pagination

查看:22
本文介绍了带有分页的存档页面上的 wordpress 粘性帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有重定向到 archieve.php 的类别页面.

I have category page which is redirecing to archieve.php.

你可以在这里看到:https://www.dealfinder.lk/category/dining/

顶部有两个置顶帖.

1) 使用 COMBANK 卡在 &Co Pub and Kitchen 享受高达 25% 的折扣

1) Up to 25% OFF at &Co Pub and Kitchen with COMBANK Cards

2) Robata – 科伦坡瑞享酒店使用所有汇丰信用卡享受 20% 折扣

2) 20% OFF at Robata – Movenpick Hotel Colombo for all HSBC Credit Cards

我的分页是每个帖子 10 个项目

现在,它向我显示每个帖子 12 个项目.

这是我的代码:

function yell_category_sticky_posts( $posts, $wp_query ) {

    global $wp_the_query;

    // Don't continue if this isn't a category query, we're not in the main query or we're in the admin
    if ( ! $wp_query->is_category || $wp_query !== $wp_the_query || is_admin() )
        return $posts;

    global $wpdb;

    $q = $wp_query->query_vars;

    $page = absint( $q['paged'] );

    if ( empty( $page ) )
        $page = 1;

    $post_type = $q['post_type'];

    $sticky_posts = get_option( 'sticky_posts' );

    if ( $wp_query->is_category && $page <= 1 && is_array( $sticky_posts ) && !empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) {

        $num_posts = count( $posts );

        $sticky_offset = 0;

        // Loop over posts and relocate stickies to the front.
        for ( $i = 0; $i < $num_posts; $i++ ) {

            if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {

                $sticky_post = $posts[$i];

                // Remove sticky from current position
                array_splice( $posts, $i, 1 );

                // Move to front, after other stickies
                array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );

                // Increment the sticky offset.  The next sticky will be placed at this offset.
                $sticky_offset++;

                // Remove post from sticky posts array
                $offset = array_search( $sticky_post->ID, $sticky_posts );
                unset( $sticky_posts[$offset] );

            }

        }

        // If any posts have been excluded specifically, Ignore those that are sticky.
        if ( !empty( $sticky_posts ) && !empty( $q['post__not_in'] ) )
            $sticky_posts = array_diff( $sticky_posts, $q['post__not_in'] );

        // Fetch sticky posts that weren't in the query results
        if ( !empty( $sticky_posts ) ) {

            $stickies__in = implode( ',', array_map( 'absint', $sticky_posts ));

            // honor post type(s) if not set to any
            $stickies_where = '';

            if ( 'any' != $post_type && '' != $post_type ) {

                if ( is_array( $post_type ) )
                    $post_types = join( "', '", $post_type );

                else
                    $post_types = $post_type;

                $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
            }

            $stickies = $wpdb->get_results( "SELECT wp_posts.* FROM $wpdb->posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1  AND ( wp_term_relationships.term_taxonomy_id IN (" . get_term( $wp_query->query_vars['cat'], 'category' )->term_taxonomy_id . ") ) AND $wpdb->posts.ID IN ($stickies__in) $stickies_where" );

            foreach ( $stickies as $sticky_post ) {

                // Ignore sticky posts are not published.
                if ( 'publish' != $sticky_post->post_status )
                    continue;

                array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );

                $sticky_offset++;

            }
        }
    }

    return $posts;

}
add_filter( 'the_posts', 'yell_category_sticky_posts', 10, 2 );

我的问题:

我想每页显示 10 个帖子,目前显示 12 个帖子每页带有置顶帖.

I want to show 10 posts per page, currently it shows 12 posts per page with sticky post.

这个问题是给大师的,不是给新手的.

This question is for master not for new learner.

这里有高手吗?提前致谢

Anybody master here? Thanks in advance

推荐答案

下面的函数将即时贴推到顶部,你应该可以使用它来帮助你的情况.

The below function pushes stickies to the top, you should be able to use this to help in your case.

add_filter('the_posts', 'bump_sticky_posts_to_top');
function bump_sticky_posts_to_top($posts) {
    $stickies = array();
    foreach($posts as $i => $post) {
        if(is_sticky($post->ID)) {
            $stickies[] = $post;
            unset($posts[$i]);
        }
    }
    return array_merge($stickies, $posts);
}

这篇关于带有分页的存档页面上的 wordpress 粘性帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆