show post from the same category when you click pr

2019-08-31 07:11发布

I have the below issue. When I click to the previous/next button of the same category unfortunately i receive the previous/next post hronologically instead of the previous/next post of the same category.

Here is the code:

< 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>

I've tried to fix my problem with this article http://codex.wordpress.org/Function_Reference/next_post_link but something i am doing wrong.

Any advice please?

3条回答
Deceive 欺骗
2楼-- · 2019-08-31 07:25

Here is the final/correct syntax

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

Thank you!

查看更多
贼婆χ
3楼-- · 2019-08-31 07:31

for example

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

I don't want to specify every time each category. I want this to be done automatically. For example, when it shows the posts from category machine and you open to read the first post, when you click to next post, then you must see next post of the same category not the next post in general. If the category is food, then the next post of the same category.

I was clear or I confused you more?

查看更多
可以哭但决不认输i
4楼-- · 2019-08-31 07:35

Copy the single.php page from your Parent theme and Paste it to your Child-theme's directory. Open the single.php from child-theme directory and add the following code at the end of file [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 } ?>

After adding this code, paste the following code into your child-theme's Style.css to style the links:

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;
}

To see these links in actions, visit my website: www.techtutsonline.com

Let me know the results :)

查看更多
登录 后发表回答