Come aggiungere collegamenti ai post precedenti e successivi in WordPress?
Ogni post in wordpress è associato a un ID, quindi potresti ottenere il suo post precedente e successivo che contiene i post adiacenti. La funzione wordpress get_previous_post recupera il post precedente e la funzione get_next_post ottiene il post successivo.
Quindi possiamo aggiungere la seguente funzione al file del modello del tema figlio functions.php per mostrare questi due link di post in una singola pagina di post. In una pagina (is_page), queste due funzioni di navigazione non funzioneranno ed è per questo che dobbiamo usare la funzione is_single() per escludere altre pagine singolari, ad esempio is_page() o is_attachment().
add_filter( 'the_content', 'next_prev_posts' );
function next_prev_posts($content) {
$pages = '';
if (is_single()) { // only shows on single post, excluding is_page, is_attachment()
$prev_post = get_previous_post();
if (!empty( $prev_post )) { // previous post available
$pages .= "Prev Post: <a href='/archives/".$prev_post->ID."'>$prev_post->post_title</a>";
}
$next_post = get_next_post();
if (!empty( $next_post )) { // next item available
$pages .= " | Next Post: <a href='/archives/".$next_post->ID."'>$next_post->post_title</a>";
}
}
return $content. $pages; // put the navigation at the end of the post content
}
Tieni presente che dovresti modificare gli URL dei tuoi post wordpress preferiti che fondamentalmente possono essere costruiti con i seguenti 3 metodi.
- guida
- ID
- titolo del post
Svuota la cache e dovresti aggiungere questi due collegamenti dopo il contenuto di ogni post. Puoi anche abilitare i tasti freccia della tastiera per navigare ai post vicini.