【问题标题】:Wordpress next_post_link/previous_post_link not staying in same categoryWordpress next_post_link/previous_post_link 不在同一类别中
【发布时间】:2016-03-16 11:52:42
【问题描述】:

这是我的设置。

single.php

<?php

    if ( in_category( 'my-category' ) ) {
        include( TEMPLATEPATH.'/single-my-category.php' );
    }
    else {
        include( TEMPLATEPATH.'/single-generic.php' );
    }
?>

single-my-category.php

<?php

if ( have_posts() ) : while (have_posts()) : the_post(); ?>

<?php echo the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
            </div>

            <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; endif; ?>

这就是我所关注的 - http://codex.wordpress.org/Function_Reference/next_post_link

我不太确定我在这里做错了什么,因为由于某种原因,previous_post_link 将我带到不同类别的帖子,即使函数的 in_same_term 参数设置为 true。

有什么想法吗?

谢谢。

【问题讨论】:

  • 发生这种情况是因为您的帖子中有多个类别,而 wp 只是转到帖子中的第一个类别(猜测)。检查this link herethis answer 也可能会有所帮助。
  • 帖子只分配到1个类别
  • 你可以试试loop下面的分页链接
  • 你确定这篇文章只分配到 1 个类别吗?是否可能仍将其分配给未分类?它也是一个类别,它在数据库中的 ID 最低。
  • 另外,我希望你没有使用缓存插件。

标签: php wordpress


【解决方案1】:

如下编辑您的代码块。您还没有将“.php”放在 single.php 文件的第 5 行。上一篇/下一篇文章将仅针对您在 single.php 的 if 语句中指定的类别的帖子显示(此处为类别 'php' )。对于以下示例,我创建了一个目录“template-parts”并在该目录中创建了两个 php 文件(“single-my-category.php”和“single-generic.php”)。

single.php

<?php
    $the_query = new WP_Query( $post );

    if (in_category('php')) {
        include( 'template-parts/single-my-category.php' );
    } else {
        include( 'template-parts/single-generic.php' );
    }
?>

single-my-category.php

if ( have_posts() ) : while (have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
            </div>

            <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; endif; ?>

【讨论】:

  • 您好,感谢您的回答,但这实际上是我的一个错误。我已经精简了这个问题,让每个人都更容易看到我做了什么,我不小心从最后遗漏了“.php”。在代码中,模板名称的末尾确实有“.php”。我现在将在我的问题中对此进行编辑。
  • @AndrewMatthew 我想你在这里遗漏了非常重要的信息。您的代码运行良好,因此未发布的其他内容正在破坏您的代码
  • next_post_link() & previous_post_link() 带五个参数。第四个是$excluded_terms,第五个是$taxonomy(默认值为'category')。您可以尝试向这两个函数添加另外两个参数吗?第 4 个将是“”,第 5 个将是“类别”。 Reference
【解决方案2】:

还要在单页中指定类别名称.. 由于 single-categoryname.php 不正确,您应该尝试使用 taxonomy-taxonomy_name.php 或

<?php query_posts('category_name=CATEGORYNAME&showposts=5');
while (have_posts()) : the_post();
  // do whatever you want
?>
<?php endwhile;?>
<div class="pagination">
<div class="container">
    <div class="row">
        <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
            <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
        </div>

        <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
            <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
        </div>
    </div>
</div>

【讨论】:

    【解决方案3】:

    如果 in_same_term 对您不起作用,则可能是您的查询对象没有保留发布数据。

    试一试——

    global $the_query;
    $the_query = new WP_Query( $post );
    

    global $the_query;
    if ( $the_query->have_posts() ) : while ($the_query->have_posts() ) : the_post(); ?>
    
    <?php the_title(); ?>
    
    <div class="pagination">
        <div class="container">
            <div class="row">
                <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                    <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
                </div>
    
                <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                    <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
                </div>
            </div>
        </div>
    </div>
    
    <?php endwhile; endif; ?>
    

    【讨论】:

      【解决方案4】:

      您能否将此代码添加到您的模板文件中。

      <ul class="pager">
        <?php if ( get_previous_post() != null ) : ?>
          <li class="previous">
            <span class="nav-previous">
              <?php 
                $singular_nav_previous_text = apply_filters( 'tc_singular_nav_previous_text', _x( '&larr;' , 'Previous post link' , 'customizr' ) );
                previous_post_link( '%link' , '<span class="meta-nav">' . $singular_nav_previous_text . '</span> %title' ); 
              ?>
            </span>
          </li>
        <?php endif; ?>
        <?php if ( get_next_post() != null ) : ?>
          <li class="next">
            <span class="nav-next">
              <?php
                $singular_nav_next_text = apply_filters( 'tc_singular_nav_next_text', _x( '&rarr;' , 'Next post link' , 'customizr' ) );
                next_post_link( '%link' , '%title <span class="meta-nav">' . $singular_nav_next_text . '</span>' ); 
                ?>
            </span>
          </li>
        <?php endif; ?>
      </ul>
      

      现在在 function.php

      中添加以下代码
      add_filter('tc_previous_single_post_link_args', 'navigate_in_same_taxonomy');
      add_filter('tc_next_single_post_link_args', 'navigate_in_same_taxonomy');
      function navigate_in_same_taxonomy( $args ){
        $args['in_same_term'] = true;
        return $args;
      }
      

      如果您需要更多关于下一个/上一个链接的选项,请检查this link

      【讨论】:

      • 这些是 Customizr 特定的钩子。不是 wordpress,这是行不通的。
      • Stackoverflows 过期的赏金选择过程为这个答案提供了赏金。这个“解决方案”不起作用。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2011-10-24
      • 1970-01-01
      • 2020-06-12
      相关资源
      最近更新 更多