CPT和页面之间的命名冲突导致分页中断

时间:2015-09-06 作者:chap

我以前遇到过这个问题,在分页中,您的自定义帖子类型被称为“insights”,然后您创建的用于在存档中显示这些自定义帖子的页面也被称为“insights”。很快就会出现命名冲突。当WordPress搜索第二个页面时,它实际上搜索的是“insights”页面,而不是post类型的“insights”,当然它会返回404 not found错误。只是为了再次检查,我已经更改了本地页面的名称,并且确保分页工作正常。

我的问题是,我不能将页面重命名为其他内容,因为这是我无法控制的(除非这是唯一的方法),而且他们有数百篇帖子,所以我不能只重命名帖子类型。那么,根据我下面的代码,我是否可以在代码中修改任何内容来修复此问题?

<?php
/*
    Template name: Insights
*/      

get_header();
the_post();
$insights = get_field(\'insights\');
$filters = array();

// Category
if( isset($_GET[\'insights_category\']) && $_GET[\'insights_category\'] != 0 ) {
    $filters[] = array(
        \'taxonomy\'  => \'insights_category\',
        \'field\'     => \'id\',
        \'terms\'     => $_GET[\'insights_category\']
    );
}

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$insights = new WP_Query(array(
    \'post_type\' => \'insights\',
    \'post_status\' => \'publish\',
    \'order\' => \'DESC\',
    \'tax_query\' => $filters,
    \'posts_per_page\' => 9,
  \'paged\' => $paged
));

?>

<?php echo get_template_part(\'content\', \'utility-bar\'); ?>

<div class="nine">
    <!-- Display the Page\'s Content in a div box. -->
    <div class="entry">
        <h2><?php the_title(); ?></h2>
        <?php echo get_template_part(\'content\', \'description\'); ?>
    </div>
    <div class="borderEntry"></div>

<?php /*
???
    <div class="content-landing box" >
        <?php the_content(); ?>
    </div>
*/ ?>

    <div id="insights_main">
        <?php
        if ($insights->have_posts()) :
            while( $insights->have_posts() ) :
                $insights->the_post();
        ?>
            <div class="insights_content">
                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <div class="insights_meta">
                    Posted on <?php the_time(\'F j, Y \\a\\t g:i a\'); ?>
                    <?php
                        $tags = wp_get_post_terms( $post->ID, \'insights_tag\');
                        if ($tags) {
                            echo \' | \';
                            $i = 1;
                            $last = count($tags);
                            foreach ($tags as $tag) {
                                echo \'<a href="/\' . $tag->taxonomy . \'/\' . $tag->slug . \'">\' . $tag->name . \'</a>\';
                                if ($i < $last) {
                                    echo \', \';
                                }
                                $i++;
                            }
                        }
                    ?>
                </div>
                <div class="isights_img">
                    <?php if (has_post_thumbnail()): ?>
                        <?php the_post_thumbnail(); ?>
                    <?php else:?>
                        <img src="<?php echo get_template_directory_uri(); ?>/images/web/default-people.png" alt=" " />
                    <?php endif; ?>
                </div>
                <div class="insights_excerpt">
                    <?php the_excerpt(); ?>
                </div>
                <div class="insights_excerpt_bottom">
                    <div class="insights_social">
                        <a href="https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>" class="insights_twitter"></a>
                        <a href="https://facebook.com/sharer.php?u=<?php the_permalink(); ?>" class="insights_facebook"></a>
                        <a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" class="insights_gplus"></a>
                        <a href="http://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>" class="insights_linkedin"></a>
                        <a href="mailto:?subject=<?php the_title(); ?>&body=<?php the_permalink(); ?>" class="insights_email"></a>
                    </div>
                    <a class="insights_more" href="<?php the_permalink(); ?>">Read more →</a>
                </div>
            </div>
            <div class="border-bottom"></div>

            <?php endwhile; ?>

      <div class="nav-previous alignleft"><?php next_posts_link( \'Older posts\', $insights->max_num_pages ); ?></div>
      <div class="nav-next alignright"><?php previous_posts_link( \'Newer posts\', $insights->max_num_pages ); ?></div>

        <?php else: ?>
            <div class="insights_content">
                <p>No insights post matches your selected criteria.</p>
                <p>Please try your search with different criteria, or <a href="<?php echo get_template_directory_uri(); ?>/insights/">view all insights posts</a>.</p>
            </div>
            <div class="border-bottom"></div>
        <?php endif; ?>
    </div>

  <?php wp_reset_postdata(); ?>

  <?php require \'tpl-insights-right-menu.php\'; ?>
</div>

<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您可以添加重写规则来覆盖帖子类型规则并强制其加载页面。

function wpd_fix_insights_pagination(){
    add_rewrite_rule(
        \'insights/page/([0-9]+)/?$\',
        \'index.php?pagename=insights&paged=$matches[1]\',
        \'top\'
    );
}
add_action( \'init\', \'wpd_fix_insights_pagination\' );
添加此代码以刷新重写规则后,请访问设置>永久链接管理页面。

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?