WordPress 自定义帖子类型分页 404 错误 [英] WordPress custom post type pagination 404 error

查看:54
本文介绍了WordPress 自定义帖子类型分页 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义主题进行分页.

我有一个带有自定义分类法的帖子类型,并且我对这些自定义分类法进行了分页,效果很好.

然而,在网站的另一部分,我有另一种自定义帖子类型;我正在尝试对这种帖子类型的帖子进行分页.目前,帖子属于自定义分类法,也属于此帖子类型,自定义分类法的行为类似于类别.

网址结构为http://www.domain.com/latest-news/,但是当我访问 http://www.domain.com/latest-news/2/ 我收到 404 错误,这就是问题所在.

这是我的代码(抱歉太长了):

错误的,'父母' =>0,'orderby' =>'ID','订单' =>'DESC','分页' =>$分页);$terms = get_terms($taxonomies, $args);foreach ($terms as $term) {$args = 数组('post_type' =>'latest_news_item','奥瑟比' =>'ID','订单' =>'ASC','posts_per_page' =>-1,'tax_query' =>大批(大批('分类' =>'最新猫','条款' =>$term->term_id)));$wp_query = new WP_Query($args);而 ($wp_query->have_posts()) : $wp_query->the_post();$numCats++;终了;$args = 数组('post_type' =>'latest_news_item','父母' =>115,'orderby' =>'ID','订单' =>'ASC','posts_per_page' =>$posts_per_page,'偏移' =>0,'分页' =>$分页);$wp_query = new WP_Query($args);而 ($wp_query->have_posts()) : $wp_query->the_post();?><div class="news-events-item"><h3><?php echo get_the_title();?></h3><?php $content = get_the_content();?><p class="summary"><?php echo wp_trim_words($content, '30', '...');?><a href="<?php echo get_the_permalink(); ?>"class="read-more">More</a></p>

<?php终了;?>

$numCats 变量只是为了计算帖子的数量,以便我可以设置分页链接,这部分工作.

谢谢

解决方案

我已经通过这种方式为自己解决了这个问题:

在自定义帖子类型博客"的存档页面上.

 $paged = 1;if ( get_query_var('paged') ) $paged = get_query_var('paged');if ( get_query_var('page') ) $paged = get_query_var('page');query_posts( '&post_type=blog&paged=' . $paged );$args = 数组('post_type' =>'博客','分页' =>$分页,'orderby' =>'日期','订单' =>'目的地','posts_per_page' =>'1',);$loop = new WP_Query( $args );while ( $loop->have_posts() ) : $loop->the_post();

  • 这是经典循环,为帖子类型博客"添加了参数;并添加了分页"属性.显然这些要点:

    $paged = 1;if ( get_query_var('paged') ) $paged = get_query_var('paged');if ( get_query_var('page') ) $paged = get_query_var('page');query_posts( '&post_type=blog&paged=' . $paged );

最后,循环之外是functions.php的分页输出

 max_num_pages);?>

在functions.php 中是永久链接修改以及posts_per_page"的修改.设置为一篇文章,以便您可以测试分页.

function paginate() {全局 $wp_query, $wp_rewrite;$wp_query->query_vars['paged'] >1 ?$current = $wp_query->query_vars['paged'] : $current = 1;$分页=数组('基础' =>@add_query_arg('page','%#%'),'格式' =>'','总计' =>$wp_query->max_num_pages,'当前' =>$当前,'show_all' =>真的,'类型' =>'清楚的');if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );if (!empty($wp_query->query_vars['s'])) $pagination['add_args'] = array('s' => get_query_var('s'));echo paginate_links( $pagination );}功能投资组合_posts_per_page( $query ) {if ( $query->query_vars['post_type'] == 'blog' ) $query->query_vars['posts_per_page'] = 1;返回 $query;}if (!is_admin()) add_filter('pre_get_posts', 'portfolio_posts_per_page');

最后这里是在循环外的存档页面上使用的分页代码.**与引导程序 5.0 一起使用**函数分页($pages = '', $range = 1){$showitems = ($range * 2) + 1;全局 $paged;如果(空($paged))$paged = 1;if ($pages == '') {全局 $the_query;$pages = $the_query->max_num_pages;如果 (!$pages) {$页面= 1;}}如果 (1 != $pages) {echo "<nav aria-label=\"...\><ul class=\"分页\">";if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)回声<li class=\"page-item \"><a class=\"page-link\";href=\"";.get_pagenum_link(1) .\">První</a></li>";if ($paged > 1 && $showitems < $pages)回声<li class=\"page-item \"><a class=\"page-link\";href=\"";.get_pagenum_link($paged - 1) .\">Předchozí</a></li>";for ($i = 1; $i <= $pages; $i++) {if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)){echo ($paged == $i) ?<li class=\"page-item active\"><a class=\"page-link\"href=\"";.get_pagenum_link($i) .">>.$i .</a></li>": "<li class=\"page-item\"><a class=\"page-link\"href=\"";.get_pagenum_link($i) .">>.$i .</a></li>";}}if ($paged < $pages && $showitems < $pages)回声<li class=\"page-item \"><a class=\"page-link\";href=\"";.get_pagenum_link($paged + 1) .\">Další</a></li>";if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)回声<li class=\"page-item \"><a class=\"page-link\";href=\"";.get_pagenum_link($pages) .\">Poslední</a></li>";echo "</ul>";回声</div>";//echo "<div class='all_pages text-muted'><small>Stránek ".$pages .</small></div>";}

}

我希望它有帮助,它对我有用.

(抱歉格式化,堆栈溢出编辑器太牛了……)

I am trying to get pagination working on a custom theme.

I have a post type with custom taxonomies and I have pagination for those custom taxonomies and that works fine.

However on another part of the site I have another custom post type; I am trying to paginate through the posts in this post type. Currently the posts are in a custom taxonomy also in this post type, the custom taxonomy behaves like a category.

The URL structure is http://www.domain.com/latest-news/, however when I go to http://www.domain.com/latest-news/2/ I get a 404 error, this is the problem.

Here is my code (sorry it's so long):

<?php
                $numCats = 0;
                $paged = (get_query_var('page')) ? get_query_var('page') : 1;
                $posts_per_page = 5;
                $offset = ($posts_per_page * $paged - $posts_per_page);
                $taxonomies = array(
                    'latest-cat'
                    );
                $args = array(
                  'hide_empty' => false,
                  'parent' => 0,
                  'orderby' => 'id',
                  'order' => 'DESC',
                  'paged' => $paged
                  );
                $terms = get_terms($taxonomies, $args);
                foreach ($terms as $term) {
                    $args = array(
                    'post_type' => 'latest_news_item',
                    'oserby' => 'id',
                    'order' => 'ASC',
                    'posts_per_page' => -1,
                    'tax_query' => array(
                      array(
                        'taxonomy' => 'latest-cat',
                        'terms' => $term->term_id
                        )
                      )
                    );
                    $wp_query = new WP_Query($args);
                    while ($wp_query->have_posts() ) : $wp_query->the_post();
                    $numCats++;
                    endwhile;
                    $args = array(
                    'post_type' => 'latest_news_item',
                    'parent' => 115,
                    'orderby' => 'id',
                    'order' => 'ASC',
                    'posts_per_page' => $posts_per_page,
                    'offset' => 0,
                    'paged' => $paged
                    );
                    $wp_query = new WP_Query($args);
                while ($wp_query->have_posts() ) : $wp_query->the_post();
                ?>
                <div class="news-events-item">
                    <h3><?php echo get_the_title(); ?></h3>
                    <?php $content = get_the_content(); ?>
                    <p class="summary"><?php echo wp_trim_words($content, '30', '...'); ?><a href="<?php echo get_the_permalink(); ?>" class="read-more">More</a></p>
                </div>
                <?php
                endwhile;
                } ?>

The $numCats variable is just to count the number of posts so that I can set up the pagination links, and that part works.

Thank you

解决方案

I have fixed this for myself in this way:

On an archive page of the custom post type "blog".

                $paged = 1;
                if ( get_query_var('paged') ) $paged = get_query_var('paged');
                if ( get_query_var('page') ) $paged = get_query_var('page');

                query_posts( '&post_type=blog&paged=' . $paged );

                $args = array(
                    'post_type' => 'blog',
                    'paged' => $paged,
                    'orderby'           => 'date',
                    'order'             => 'DEST',
                    'posts_per_page'                => '1',

                );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post();

  • this is the classic loop with added arguments for post type "blog" and added "Paged" attribute. And obviously these points:

    $paged = 1;
                  if ( get_query_var('paged') ) $paged = get_query_var('paged');
                  if ( get_query_var('page') ) $paged = get_query_var('page');
    
                  query_posts( '&post_type=blog&paged=' . $paged );
    

At the end, outside of the loop is the pagination output from functions.php

            <?php echo pagination($loop->max_num_pages); ?>

In functions.php is the permalink modification and also modification of the "posts_per_page" set to ONE post, so you can test the pagination.

function paginate() {
    global $wp_query, $wp_rewrite;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages,
        'current' => $current,
        'show_all' => true,
        'type' => 'plain'
    );
    if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
    if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
    echo paginate_links( $pagination );
}
function portfolio_posts_per_page( $query ) {
    if ( $query->query_vars['post_type'] == 'blog' ) $query->query_vars['posts_per_page'] = 1;
    return $query;
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page' );

And as last here is the pagination code which is used on the archive page outside of the loop.

**used with bootstrap 5.0**


    

function pagination($pages = '', $range = 1)
{
    $showitems = ($range * 2) + 1;

    global $paged;
    if (empty($paged)) $paged = 1;

    if ($pages == '') {
        global $the_query;
    $pages = $the_query->max_num_pages;
    if (!$pages) {
        $pages = 1;
    }
}

if (1 != $pages) {
    echo "<nav aria-label=\"...\">
        <ul class=\"pagination\">";
    if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
        echo "
                 <li class=\"page-item \">
              <a class=\"page-link\" href=\"" . get_pagenum_link(1) . "\">První</a>
            </li>";
    if ($paged > 1 && $showitems < $pages)

        echo "
              <li class=\"page-item \">
              <a class=\"page-link\" href=\"" . get_pagenum_link($paged - 1) . "\">Předchozí</a>
            </li>";

    for ($i = 1; $i <= $pages; $i++) {
        if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
             echo ($paged == $i) ? "<li class=\"page-item active\"><a class=\"page-link\" href=\"" . get_pagenum_link($i) . "\">" . $i . "</a></li>" : "<li class=\"page-item\"><a class=\"page-link\" href=\"" . get_pagenum_link($i) . "\">" . $i . "</a></li>";

        }
    }

    if ($paged < $pages && $showitems < $pages)
       echo "
              <li class=\"page-item \">
              <a class=\"page-link\" href=\"" . get_pagenum_link($paged + 1) . "\">Další</a>
            </li>";
    if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
        echo "
                 <li class=\"page-item \">
              <a class=\"page-link\" href=\"" . get_pagenum_link($pages) . "\">Poslední</a>
            </li>";
    echo "</ul>";
    echo "</div>";

    //echo "<div class='all_pages text-muted'><small>Stránek " . $pages . "</small></div>";
}

}

I hope it helps, it works for me.

(sorry for formating, stack overflow editor is totall bull...)

这篇关于WordPress 自定义帖子类型分页 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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