简体   繁体   中英

Next and Previous nav links in same category with post titles

I have this working code in a WordPress widget :

<div class="portfolio-links" >
<span style="float:left"><?php next_post_link('%link'); ?></span>
<span style="float:right"><?php previous_post_link( '%link'); ?></span>
</div>

Now I am trying to limit the links only to the same portfolio category

< Prev.Cat.Post.Title1 __________ Next.Cat.Post.Title2 >

I tried using <?php next_post_link( '%link', 'Next post in category', TRUE ); ?> <?php next_post_link( '%link', 'Next post in category', TRUE ); ?> but I didn't figure out a way to add the post title.

I also tried <?php next_post_link( '%link', '%title', TRUE ); ?> <?php next_post_link( '%link', '%title', TRUE ); ?> but that leaves me with an empty string for some reason.

An alternateve way would be to use a code of the type :

if(portofilio_category = 'my-category'){
...
}

Only I don't know how to do it. Both solutions would work for me.

Useful reference: https://codex.wordpress.org/Function_Reference/next_post_link

Thank you very much

The Wordpress reference mentions:

link : Default '%title'.

So this should work for you:

<?php next_post_link( '%link', '%title', TRUE ); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM