0

我有以下问题。不幸的是,当我单击同一类别的上一个/下一个按钮时,我会按时间顺序收到上一个/下一个帖子,而不是同一类别的上一个/下一个帖子。

这是代码:

< div id="previous_post">
< ?php previous_post_link( '%link', __( '<span class="prev"><< Preivous Post</span>', 'esquire' ) ); ? > 
< /div>

< div id="next_post">   < ?php next_post_link( '%link', __( '<span class="next">Next Post >></span>',  'esquire' ) ); ? >

< /div>

我试图用这篇文章http://codex.wordpress.org/Function_Reference/next_post_link解决我的问题, 但我做错了。

请问有什么建议吗?

4

3 回答 3

1

例如

 previous_post_link( '%link', __( '<span class="prev"><< Preivous Post</span>', TRUE, 'esquire' ) );

我不想每次都指定每个类别。我希望这能自动完成。例如,当它显示来自类别机器的帖子并且您打开阅读第一个帖子时,当您单击下一个帖子时,您必须看到下一个相同类别的帖子,而不是一般的下一个帖子。如果类别是食物,则下一个相同类别的帖子。

我很清楚还是我让你更困惑?

于 2013-10-22T12:43:35.640 回答
1

从您的父主题复制 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

让我知道结果:)

于 2017-07-08T11:44:52.747 回答
0

这是最终/正确的语法

    previous_post_link( '%link', __( '<span class="prev"><< Previous Post</span>', 'esquire' ), TRUE );

谢谢!

于 2013-10-22T13:01:37.057 回答