2013-10-21 22 views
0

我有以下问题。当我点击相同类别的上一个/下一个按钮时,不幸的是,我按照时间顺序接收了上一个/下一个帖子,而不是同一类别的上一个/下一个帖子。当你点击上一个/下一个按钮wordpress时,显示同一类别的帖子

下面是代码:

< 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

你可能想继续阅读进一步下来,直到你看到这样 <?PHP next_post_link(“%链接”,“下一步POS t','TRUE','13'); ?> – xlordt

+0

嘿@xlordt。我已经尝试了参数TRUE,但它没有帮助。当然,我不确定我是否把它放在正确的地方。 –

+0

你究竟如何尝试这个?你可以用你尝试过的解决方案更新你的文章吗? – xlordt

回答

1

例如

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

我不想指定每次每个类别。我希望这是自动完成的。例如,当它显示类别机器中的帖子并打开以阅读第一篇文章时,当您点击下一篇文章时,则必须看到相同类别的下一篇文章,而不是下一篇文章。如果该类别是食品,则是同一类别的下一个帖子。

我很清楚,或者我更困惑吗?

+0

OOOOOOHHH神,多么失败!你是对的xlordt。我试图把真实的地方放在一个错误的地方。我把它放在最后的两个括号之间,它起作用。 –

0

这里是最后的/正确的语法

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

谢谢!

+0

但这不适用于next_post_link? –

1

从您的父主题中复制single.php页面并将其粘贴到您的Child-theme的目录中。从child-theme目录打开single.php,并在文件末尾添加以下代码[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

让我知道结果:)

相关问题