WP 自定义帖子类型分页/404 错误

发布于 2025-01-02 13:46:45 字数 613 浏览 0 评论 0原文

我在 WordPress 中使用自定义帖子类型(称为“ns_news_article”,由 Magic Fields 生成),该类型已注册并正在运行。这会重写 URL 以在域后面添加“category/news”(例如:www.mydomain.com/category/news/custom-post-1)。我必须在那里有“假目录”。

我在functions.php中有这个:

add_rewrite_rule("[ns_news_article]" . '$', "index.php?pagename=[ns_news_article]", "top");
add_rewrite_rule("[ns_news_article]" . '/page/([0-9])*/?', "index.php?pagename=[ns_news_article]" . '&paged=$matches[1]', "top"); 
global $wp_rewrite;
$wp_rewrite->flush_rules();

第二行解决了查看多页列表时的分页404问题,但在尝试查看单个帖子时出现404错误。如果我去掉这个,你可以查看帖子,但分页不起作用。

我可以解决这个问题还是应该以其他方式完成?

I'm using a custom post type in WordPress (called "ns_news_article" generated from Magic Fields) which are registered and working. This rewrites the URL to add "category/news" after the domain (so: www.mydomain.com/category/news/custom-post-1). I have to have the "fake directories" in there.

I have this in the functions.php:

add_rewrite_rule("[ns_news_article]" . '

The 2nd line takes care of pagination 404 problem when viewing as multiple pages of lists, but then gives 404 errors when trying to view the single post. If I get rid of this, you can view the posts, but pagination doesn't work.

Can I fix this or should it be done another way?

, "index.php?pagename=[ns_news_article]", "top"); add_rewrite_rule("[ns_news_article]" . '/page/([0-9])*/?', "index.php?pagename=[ns_news_article]" . '&paged=$matches[1]', "top"); global $wp_rewrite; $wp_rewrite->flush_rules();

The 2nd line takes care of pagination 404 problem when viewing as multiple pages of lists, but then gives 404 errors when trying to view the single post. If I get rid of this, you can view the posts, but pagination doesn't work.

Can I fix this or should it be done another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2025-01-09 13:46:45

我最近完成了自定义帖子类型分页,所以这就是我所做的。假设您的自定义帖子类型称为“customp”。

  1. 在您的主题中创建一个名为 page-customp.php 的文件。然后发布一个标题为“Customp”的空页面。现在,当您访问 www.yourdomain.com/customp 时,您将看到一个使用 page-customp.php 作为模板的页面。现在我们将使用此页面来显示自定义帖子并进行分页。

  2. 将此代码放入您的 page-customp.php 文件中:

    <前><代码>$分页 = 1;

    $每页帖子数 = 5;

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

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

    query_posts( '&post_type=customp&paged=' . $paged . '&posts_per_page='.$postsPerPage );

    while (have_posts()) : the_post();

    the_title();
    回声 get_the_excerpt();

    最后;

    自定义p_paginate();

  3. 在您的 functions.php 文件中放置此代码:

    函数customp_paginate() {
    
      全局 $wp_query, $wp_rewrite;
    
      $wp_query->query_vars['paged']>; 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    
      $分页=数组(
        '基础' => @add_query_arg('页面', '%#%'),
        '格式' => '',
        '总计' => $wp_query->max_num_pages,
        '当前' => $当前,
        'show_all'=>;真的,
        '类型' => '列表',
        'prev_text' =>; '«',
        '下一个文本' => '»',
      );
    
      if ($wp_rewrite->using_permalinks())
        $pagination['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', '分页');
    
      if (!empty($wp_query) && $pagination['total'] != 1) {
        $pagination['add_args'] = array('s' => get_query_var('s'));
    
        echo "
    页数:" 。 paginate_links($pagination) 。 “
    ”; } }

我希望它有帮助:)

I've recently done custom post types pagination, so this is how I've done. Let's say your custom post type it's called "customp".

  1. Create a file in your theme called page-customp.php. Then publish an empty page with "Customp" title. Now, when you're visiting www.yourdomain.com/customp you will see a page that is using page-customp.php as a template. Now we will be using this page to show the custom posts and with a pagination.

  2. Place this code in your page-customp.php file:

    $paged = 1;
    
    $postsPerPage = 5;
    
     if ( get_query_var('paged') ) $paged = get_query_var('paged');
    
     if ( get_query_var('page') )  $paged = get_query_var('page');
    
     query_posts( '&post_type=customp&paged=' . $paged . '&posts_per_page='.$postsPerPage );
    
    while ( have_posts() ) : the_post();
    
      the_title();
      echo get_the_excerpt();
    
    endwhile;
    
    customp_paginate();
    
  3. In your functions.php file place this:

    function customp_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' => 'list',
        'prev_text' => '«',
        'next_text' => '»',
      );
    
      if ($wp_rewrite->using_permalinks())
        $pagination['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', 'paged');
    
      if (!empty($wp_query) && $pagination['total'] != 1) {
        $pagination['add_args'] = array('s' => get_query_var('s'));
    
        echo "<div class='paginate'><strong>Pages:</strong>" . paginate_links($pagination) . "</div>";
      }
    
    }
    

I hope it helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文