Adjacent Posts Navigation with Infinite Looping in WordPress

le 26/10/2021
JAG

This members-only tutorial provides the steps to set up links to previous and next posts on single posts whilst ensuring that
if there is no previous post, the first post is shown andif there is no next post, the last post is shown
essentially for an infinite looping of adjacent posts.
For example, given these posts:

when Post 3 is being viewed, the previous post would be Post 2 and the next would be Post 4.

when Post 4 is being viewed, the previous post would be Post 3 and the next should be Post 1 and not blank.

and when Post 1 is being viewed, the previous post should be Post 4 and not blank and the next would be Post 2.

While the tutorial has been written for Oxygen, the instructions can be used in any WordPress site.
Step 1
Edit the Template that applies to singular posts.
Add a Code Block and set its width to 100%.
Give it a class of adjacent-posts.
PHP & HTML:

ID ) ) {
$prev_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id( $prev_post->ID ), 'thumbnail' );
} else {
$prev_post_image_url = 'https://dummyimage.com/150';
} ?>


1,
'no_found_rows' => true,
'order' => 'DESC',
);

$first = new WP_Query( $args );

$first->the_post();

// first post's image URL
if ( has_post_thumbnail() ) {
$first_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id(), 'thumbnail' );
} else {
$first_post_image_url = 'https://dummyimage.com/150';
} ?>


ID ) ) {
$next_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id( $next_post->ID ), 'thumbnail' );
} else {
$next_post_image_url = 'https://dummyimage.com/150';
} ?>


1,
'no_found_rows' => true,
'order' => 'ASC',
);

$last = new WP_Query( $args );

$last->the_post();

// last post's image URL
if ( has_post_thumbnail() ) {
$last_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id(), 'thumbnail' );
} else {
$last_post_image_url = 'https://dummyimage.com/150';
} ?>



Replace all the four instances of https://dummyimage.com/150 with the URL of your fallback image for posts that do not have a featured image set.
Step 2
Add the following CSS at Manage > Stylesheets:
img {
vertical-align: top;
height: auto;
max-width: 100%;
}

.adjacent-posts {
display: flex;
justify-content: space-between;
}

.adjacent-post {
display: flex;
gap: 20px;
}

.adjacent-post img {
border-radius: 4px;
}

.adjacent-post > div {
display: flex;
flex-direction: column;
justify-content: center;
}

.adjacent-label {
text-transform: uppercase;
font-size: small;
line-height: 1;
margin-top: 0;
margin-bottom: 4px;
}

.next-post {
flex-direction: row-reverse;
}

.next-post > div {
text-align: right;
}

.adjacent-title {
font-size: 24px;
font-weight: 600;
}

@media only screen and (max-width: 767px) {
.adjacent-posts {
flex-direction: column;
align-items: flex-start;
gap: 40px;
}

.next-post {
flex-direction: row;
}

.next-post > div {
text-align: left;
}
}
References
https://gist.github.com/banago/5603826

Adjacent Posts Side Tabs in Oxygen

Nos experts se tiennent à votre disposition
pour répondre à vos questions et vous conseiller
dans votre projet digital.

Vous souhaitez développer une application ?

Envie d’échanger avec l’un de nos experts ? Besoin d’en savoir plus ?
En savoir plus

Recevez notre newsletter

La protection des données nous tient à cœur. JAG utilise les informations que vous fournissez afin de vous proposer des informations et du contenu pertinent sur nos produits et services. Vous pouvez vous désinscrire de ce type de communications à tout moment. Pour plus d'informations, consultez notre politique de confidentialité
© 2022 JAG Consulting, sas. Tous droits réservés.
© 2022 JAG Consulting, sas. Tous droits réservés.
4.8
Lire les 51 avis
google
Brieuc Pauly
08/09/2022

Créative, dynamique et très professionnelle, c'est à mon sens les piliers qui soutiennent cette équipe talentueuse ! Une agence à taille humaine qui privilégie la qualité et le résultat. Alors si vous souhaitez le meilleur pour votre communication adressez vous les yeux fermés à ces passionnés, plus que compétents ! (Translated by Google) Creative, dynamic and very professional, these are in my opinion the pillars that support this talented team! A human-sized agency that favors quality and results. So if you want the best for your communication, turn your eyes closed to these enthusiasts, who are more than competent!

google
Amaury Tardier
08/09/2022

Merci à Julien et son équipe, plus que satisfait par le travail fourni ! Je recommande vivement ! (Translated by Google) Thanks to Julien and his team, more than satisfied with the work provided! I highly recommend !

Ravi de travailler avec l'équipe JAG pour la gestion de mon site de e-Commerce. Equipe réactive et toujours pleine de bonnes idées pour améliorer les performances du site (Translated by Google) Glad to work with the JAG team for the management of my e-Commerce site. Responsive team always full of good ideas to improve the performance of the site

Merci à Julien pour son accompagnement dans mon projet, je recommande à 200% c est le Top (Translated by Google) Thanks to Julien for his support in my project, I recommend 200% it is the top

Excellente agence. Equipe est très pro, à l'écoute et de bon conseils. (Translated by Google) Excellent agency. The team is very professional, attentive and gives good advice.

Temps de réponse hors pair quand il y a eu un pb sur mon site, je vous recommande chaleureusement Julien :-) (Translated by Google) Unparalleled response time when there was a problem on my site, I warmly recommend Julien :-)

Charger plus
listarrow-right
Julien

Julien

Votre conseiller

I will be back soon

Une question ?

Julien
Bonjour ! Je suis Julien.
Comment puis-je vous aider?
Démarrer la conversation:
chat Live Chat