Php Wordpress下一个帖子链接/上一个帖子链接不在同一类别中
这是我的设置 single.phpPhp Wordpress下一个帖子链接/上一个帖子链接不在同一类别中,php,wordpress,Php,Wordpress,这是我的设置 single.php <?php if ( in_category( 'my-category' ) ) { include( TEMPLATEPATH.'/single-my-category.php' ); } else { include( TEMPLATEPATH.'/single-generic.php' ); } ?> <?php $the_query = new WP_Que
<?php
if ( in_category( 'my-category' ) ) {
include( TEMPLATEPATH.'/single-my-category.php' );
}
else {
include( TEMPLATEPATH.'/single-generic.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
<?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; ?>
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; ?>
这就是我所遵循的-
我不太确定我在这里做错了什么,因为出于某种原因,前一个帖子链接将我带到了不同类别的帖子,即使函数的in_same_term参数设置为true
有什么想法吗
谢谢。您可以将此代码添加到您的模板文件中吗
<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( '←' , '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( '→' , 'Next post link' , 'customizr' ) );
next_post_link( '%link' , '%title <span class="meta-nav">' . $singular_nav_next_text . '</span>' );
?>
</span>
</li>
<?php endif; ?>
</ul>
如果需要下一个/上一个链接的更多选项,请检查编辑代码块,如下所示。您没有将“.php”放在单个.php文件的第5行。上一篇/下一篇文章将仅显示在single.php的
if
语句中指定的类别的文章(此处为“php”类别)。对于下面的示例,我创建了一个目录“template parts”,并在该目录中创建了两个php文件(“single my category.php”和“single generic.php”)
single.php
<?php
if ( in_category( 'my-category' ) ) {
include( TEMPLATEPATH.'/single-my-category.php' );
}
else {
include( TEMPLATEPATH.'/single-generic.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
<?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; ?>
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; ?>
if(have_posts()):while(have_posts()):the_post();?>
还可以在单个页面中指定类别名称。。由于single-categoryname.php不正确,您应该尝试使用taxonomy-taxonomy\u 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>
如果同一术语中的不适用于您,则可能是您的查询对象没有保留post数据 试一试-
global $the_query;
$the_query = new WP_Query( $post );
global$the_查询;
如果($the_query->have_posts()):而($the_query->have_posts()):the_post();?>
之所以会出现这种情况,是因为您的帖子上有多个类别,而wp只会转到帖子中的第一个类别(猜测)。检查。也可能有帮助。该帖子仅分配给1个类别。您可以尝试Loop下面的分页链接。您确定该帖子仅分配给1个类别吗?是否仍有可能分配给未分类的?它也是一个类别,它的ID在数据库中是最低的。另外,我希望你没有使用缓存插件。这些都是特定于自定义程序的挂钩。不是wordpress,这不起作用。Stackoverflows过期赏金选择过程将赏金授予此答案。这个“解决方案”不起作用。谢谢。嗨,谢谢你的回答,但这实际上是我的一个错误。我删减了这个问题,让大家更容易看到我做了什么,我不小心漏掉了结尾处的“.php”。在代码中,它确实在模板名称的末尾有“.php”。我现在将在我的问题中编辑此内容。@AndrewMatthew我认为您遗漏了非常重要的信息。您的代码运行得非常好,因此其他未发布的内容正在破坏您的代码next\u post\u link()
&previous\u post\u link()
获取五个参数。第四个是$excluded_terms,第五个是$taxonomy(默认值为'category')。您可以尝试向这两个函数添加两个参数吗?第四名为“类别”,第五名为“类别”。