WordPress 自定义分类分页获取 404 未找到

发布于 2024-10-15 00:15:17 字数 4533 浏览 1 评论 0原文

我有一个名为“dcategory”的自定义分类法,因此我创建了一个名为“taxonomy-dcategory.php”的模板文件来确定其显示方式。这是此模板的代码:

<?php
global $paged, $wp_query;
get_header();
?>
<div id="leftcontent">
<?php if(is_user_logged_in()) : ?>
    <?php
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    if($term->parent == 0) : ?>
      <h1>Specialist Directory</h1>
      <h2><?php echo $term->name; ?></h2>
      <ul>
        <?php
        $subtermcats = get_terms('dcategory', 'hide_empty=0&parent='.$term->term_id.'&orderby=name');
        foreach($subtermcats as $key => $data) {
            $termlink = get_term_link($data, 'dcategory');
            echo "<li><a href=\"".$termlink."\">".$data->name."</a></li>";
        }
        ?>
      </ul>
    <?php else: ?>
        <h1>Specialist Directory</h1>
        <?php
        $topterm = get_term_by( 'id', $term->parent, get_query_var( 'taxonomy' ) );
        ?>
        <h2><?php echo $topterm->name; ?> &gt; <?php echo $term->name; ?></h2>
        <?php
            // Include Search - Search results returned in $_SESSION['lpoc_search_data'];
            include("functions/directorysearch.php");
            if(count($_SESSION['lpoc_search_data']) > 0) {
                $temp = $wp_query;
                $wp_query = null;
                $args = array(
                   'post_type' => 'listings',
                   'post__in' => $_SESSION['lpoc_search_data'],
                   'showposts' => 10,
                   'paged' => $paged,
                   'orderby' => 'post__in'
                );
                $wp_query = new WP_Query($args);
            } else {
                query_posts("cat=9999999"); // Make a fake query that will be empty to flush out the content from the page we are on
            }
        ?>
        <?php if (have_posts()) : ?>
            <div class="pageination clearfix">
              <div class="smallleftcontent">
                <select name="sortby" class="dropdownreplace">
                  <option value="date-desc">Order by latest added</option>
                  <option value="date-asc">Order by oldest added</option>
                  <option value="price-asc">Order by price ascending</option>
                  <option value="price-desc">Order by price descending</option>
                </select>
              </div>
              <div class="smallrightcontent">
              <?php wp_pagenavi(); ?>
              </div>
            </div>
            <?php while (have_posts()) : the_post(); ?>
            <h2><?php the_title(); ?></h2>
            <?php endwhile; ?>
            <div class="pageination clearfix">
              <div class="smallleftcontent">
                <select name="sortby" class="dropdownreplace">
                  <option value="date-desc">Order by latest added</option>
                  <option value="date-asc">Order by oldest added</option>
                  <option value="price-asc">Order by price ascending</option>
                  <option value="price-desc">Order by price descending</option>
                </select>
              </div>
              <div class="smallrightcontent">
              <?php wp_pagenavi(); ?>
              </div>
            </div>
        <?php else: ?>
        <?php endif; ?>
    <?php endif; ?>
<?php else: // USER NOT LOGGED IN ?>
    <?php include("functions/pleaseregister.php"); ?>
<?php endif; ?>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

这是正在发生的事情:

检查用户是否登录。如果未登录,则显示注册信息。 如果用户位于父 dcategory 上,则会显示所选的 dcategory 子级。 如果用户位于子类别,则运行我自己的自定义编写的数据库查询,该查询根据用户到帖子的距离按顺序获取帖子的 ID。 结果在会话数组中返回,并作为自定义查询传递给 $wp_query。 查询有效,页面显示前十篇文章以及 wp_pagenavi 生成的分页(全部正确)。

但是当导航到第 2 页时,我返回了一个未找到的页面:

http://www.example.com/ d类别/古董/http://www.example.com/dcategory/antiques/page/2/

有什么想法为什么会发生这种情况吗?

谢谢

斯科特

I have a custom taxonomy called "dcategory" so I have created a template file called "taxonomy-dcategory.php" to determine how its shown. Here is the code for this template:

<?php
global $paged, $wp_query;
get_header();
?>
<div id="leftcontent">
<?php if(is_user_logged_in()) : ?>
    <?php
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    if($term->parent == 0) : ?>
      <h1>Specialist Directory</h1>
      <h2><?php echo $term->name; ?></h2>
      <ul>
        <?php
        $subtermcats = get_terms('dcategory', 'hide_empty=0&parent='.$term->term_id.'&orderby=name');
        foreach($subtermcats as $key => $data) {
            $termlink = get_term_link($data, 'dcategory');
            echo "<li><a href=\"".$termlink."\">".$data->name."</a></li>";
        }
        ?>
      </ul>
    <?php else: ?>
        <h1>Specialist Directory</h1>
        <?php
        $topterm = get_term_by( 'id', $term->parent, get_query_var( 'taxonomy' ) );
        ?>
        <h2><?php echo $topterm->name; ?> > <?php echo $term->name; ?></h2>
        <?php
            // Include Search - Search results returned in $_SESSION['lpoc_search_data'];
            include("functions/directorysearch.php");
            if(count($_SESSION['lpoc_search_data']) > 0) {
                $temp = $wp_query;
                $wp_query = null;
                $args = array(
                   'post_type' => 'listings',
                   'post__in' => $_SESSION['lpoc_search_data'],
                   'showposts' => 10,
                   'paged' => $paged,
                   'orderby' => 'post__in'
                );
                $wp_query = new WP_Query($args);
            } else {
                query_posts("cat=9999999"); // Make a fake query that will be empty to flush out the content from the page we are on
            }
        ?>
        <?php if (have_posts()) : ?>
            <div class="pageination clearfix">
              <div class="smallleftcontent">
                <select name="sortby" class="dropdownreplace">
                  <option value="date-desc">Order by latest added</option>
                  <option value="date-asc">Order by oldest added</option>
                  <option value="price-asc">Order by price ascending</option>
                  <option value="price-desc">Order by price descending</option>
                </select>
              </div>
              <div class="smallrightcontent">
              <?php wp_pagenavi(); ?>
              </div>
            </div>
            <?php while (have_posts()) : the_post(); ?>
            <h2><?php the_title(); ?></h2>
            <?php endwhile; ?>
            <div class="pageination clearfix">
              <div class="smallleftcontent">
                <select name="sortby" class="dropdownreplace">
                  <option value="date-desc">Order by latest added</option>
                  <option value="date-asc">Order by oldest added</option>
                  <option value="price-asc">Order by price ascending</option>
                  <option value="price-desc">Order by price descending</option>
                </select>
              </div>
              <div class="smallrightcontent">
              <?php wp_pagenavi(); ?>
              </div>
            </div>
        <?php else: ?>
        <?php endif; ?>
    <?php endif; ?>
<?php else: // USER NOT LOGGED IN ?>
    <?php include("functions/pleaseregister.php"); ?>
<?php endif; ?>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Here is what is happening:

Check if user is logged in. If not show register info.
If user is on a parent dcategory then the chosen dcategory children is shown.
If user is on child dcategory then run my own custom brewed database query that gets IDs of posts in an order based on distance from user to post.
Results are returned in a session array and passed as a custom query to $wp_query.
The query works and the page shows the first ten posts along with the pagination generated by wp_pagenavi (all correct).

But when navigating to page 2 I am returned a page not found:

http://www.example.com/dcategory/antiques/
to
http://www.example.com/dcategory/antiques/page/2/

Any ideas why this is happening?

Thanks

Scott

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

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

发布评论

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

评论(3

独木成林 2024-10-22 00:15:17

重新保存永久链接设置...

Resave the permalink settings...

请持续率性 2024-10-22 00:15:17

我自己已经解决了这个问题。

我的自定义查询返回的结果多于 WordPress 为所选类别返回的结果。因此,当我进入第二页时,没有足够的结果,因此找不到页面。

因此,我没有创建新查询,而是用以下方法过滤现有结果:

<?php
        // Include Search - Search results returned in $_SESSION['lpoc_search_data'];
        include("functions/directorysearch.php");
        query_posts(
            array_merge(
                array( 'post__in' => $_SESSION['lpoc_search_data'], 'orderby' => 'post__in' ),
                $wp_query->query
            )
        );
    ?>

I have kind of solved this myself.

My custom query was returning more results than what wordpress was returning for the chosen category. So when I was going on page two there wasnt enough results so getting a page not found.

So instead of creating a new query I setout to filter the existing results with this instead:

<?php
        // Include Search - Search results returned in $_SESSION['lpoc_search_data'];
        include("functions/directorysearch.php");
        query_posts(
            array_merge(
                array( 'post__in' => $_SESSION['lpoc_search_data'], 'orderby' => 'post__in' ),
                $wp_query->query
            )
        );
    ?>
半山落雨半山空 2024-10-22 00:15:17

当您注册新的自定义分类法时,您需要再次保存永久链接设置,请务必重新保存。

When you register a new custom taxonomy you need to save the permalinks settings again, be sure to resave it.

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