从您的父主题复制 single.php 页面并将其粘贴到您的子主题目录。从子主题目录打开 single.php 并在文件末尾添加以下代码 [before get_footer(); ]
<?php
$post_id = $post->ID; // Get current post ID
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID; // Get current Category ID
$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC');
$posts = get_posts($args);
// Get IDs of posts retrieved by get_posts function
$ids = array();
foreach ($posts as $thepost) {
$ids[] = $thepost->ID;
}
// Get and Echo the Previous and Next post link within same Category
$index = array_search($post->ID, $ids);
$prev_post = $ids[$index-1];
$next_post = $ids[$index+1];
?>
<?php if (!empty($prev_post)){ ?> <a class="previous-post" rel="prev" href="<?php echo get_permalink($prev_post) ?>"> <span class="meta-icon"><i class="fa fa-angle-left fa-lg"></i></span> Previous</a> <?php } ?>
<?php if (!empty($next_post)){ ?> <a class="next-post" rel="next" href="<?php echo get_permalink($next_post) ?>">Next <span class="meta-icon"><i class="fa fa-angle-right fa-lg"></i></span> </a> <?php } ?>
添加此代码后,将以下代码粘贴到您的子主题的 Style.css 中以设置链接样式:
a.previous-post, a.next-post {
color: #fff;
background-color: #4498e7;
text-align: center;
height: 34px;
line-height: 34px;
font-size: 14px;
border: 1px solid;
padding: 0 20px;
margin-bottom: 30px;
text-transform: uppercase;
border-radius: 4px;
font-weight: bold;
}
a.previous-post:hover, a.next-post:hover {
color: #4498e7;
background-color: #fff;
}
a.previous-post {
float: left !important;
}
a.next-post {
float: right !important;
}
要查看这些链接的实际操作,请访问我的网站:www.techtutsonline.com
让我知道结果:)