[英]Wordpress next_post_link/previous_post_link not staying in same category
Here's my setup. 这是我的设置。
single.php single.php中
<?php
if ( in_category( 'my-category' ) ) {
include( TEMPLATEPATH.'/single-my-category.php' );
}
else {
include( TEMPLATEPATH.'/single-generic.php' );
}
?>
single-my-category.php 单我-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; ?>
This is what i have followed - http://codex.wordpress.org/Function_Reference/next_post_link 这就是我所遵循的 - http://codex.wordpress.org/Function_Reference/next_post_link
I'm not quite sure what I'm doing wrong here as for some reason the previous_post_link is taking me to a post in a different category even though in_same_term parameter of the function is set to true. 由于某种原因,即使函数的in_same_term参数设置为true,previous_post_link也会将我带到不同类别的帖子,我不太确定我在这里做错了什么。
Any ideas? 有任何想法吗?
Thanks. 谢谢。
Edit your code blocks as following. 编辑代码块如下。 You haven't put '.php' on the 5th line of your single.php file.
你没有在你的single.php文件的第5行放上'.php'。 Previous/Next post will be shown only for the posts of category which you specify inside
if
statement of single.php ( here category 'php' ). 上一页/下一页后会显示仅适用于你的内心指定类别的职位
if
single.php中(这里类别“PHP”)的声明。 For the following example, I've created a directory 'template-parts' and created two php file ("single-my-category.php" and "single-generic.php")inside that directory. 对于以下示例,我创建了一个目录'template-parts',并在该目录中创建了两个php文件(“single-my-category.php”和“single-generic.php”)。
single.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 单我-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; ?>
Also specify the category name in single page .. since single-categoryname.php is not correct you should try using taxonomy-taxonomy_name.php OR 同时在单页中指定类别名称..由于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>
Can you plz add this code in your template file . 你可以在模板文件中添加此代码吗?
<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>
Now add below code in function.php 现在在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;
}
if you need more option on Next/Prev link check this link 如果您在Next / Prev链接上需要更多选项,请选中此链接
If in_same_term is not working for you, It might your query object is not holding up the post data. 如果in_same_term不适合您,那么您的查询对象可能不会阻止发布数据。
Give it a try - 试试看 -
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; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.