Link Precedente/Successivo per Custom Post Type?

19 ott 2011, 22:25:20
Visualizzazioni: 64K
Voti: 14

Ho un custom post type chiamato portfolio. Ho bisogno di un link precedente/successivo SENZA utilizzare plugin. Qualcuno ha una soluzione?

Post di esempio: http://themeforward.com/demo2/archives/portfolio/boat

<?php get_header(); ?>

<!-- Inizio wrap -->
<div class="clear">
<div id="full_container">
<div id="content2">
<div id="content">

<!-- Recupera i post -->
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>

<!-- Titolo del post -->
<h1>
    <?php the_title(); ?>
</h1>

<!-- Il post -->
<?php the_content(); ?>

<!-- Tag -->
<h3 class="tags">
    <?php the_tags('Tags ',' / ','<br />'); ?>
</h3>

<!-- Fine wrap -->
</div>

<!-- Post Precedente/Successivo -->
<div class="mp_archive2">
<div id="more_posts">
    <div class="oe">
        <?php previous_post_link('%link', '« Post precedente', TRUE); ?>
    </div>

    <div class="re">
        <?php next_post_link('%link', 'Post successivo »', TRUE); ?>
    </div>
</div>
</div>

<?php endwhile; else: ?>
<p>Nessuna voce corrispondente trovata.</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
4
Commenti

Perché l'avversione ai plugin?

chrisguitarguy chrisguitarguy
19 ott 2011 23:06:07

Perché se è un plugin, non è integrato nel tema.

AndrettiMilas AndrettiMilas
19 ott 2011 23:26:16

@Lucas Wynne Se vuoi che sia integrato, copia/incolla il codice di qualche plugin nel tuo file functions.php del tema.

kaiser kaiser
19 ott 2011 23:50:10

@kaiser assumendo ovviamente che tu rispetti le condizioni della licenza e della proprietà intellettuale, il che non è così semplice se stai producendo un tema in vendita.

Phill Healey Phill Healey
24 lug 2016 14:20:46
Tutte le risposte alla domanda 3
2
21
<?php
$prev_post = get_previous_post();
if($prev_post) {
   $prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
   echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" ">&laquo; Post precedente<br /><strong>&quot;'. $prev_title . '&quot;</strong></a>' . "\n";
}

$next_post = get_next_post();
if($next_post) {
   $next_title = strip_tags(str_replace('"', '', $next_post->post_title));
   echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" ">Post successivo &raquo;<br /><strong>&quot;'. $next_title . '&quot;</strong></a>' . "\n";
}
?>
20 nov 2013 12:05:11
Commenti

Formatta i tuoi codici/risposte e aggiungi una spiegazione.

Maruti Mohanty Maruti Mohanty
20 nov 2013 12:35:55

Posso limitarlo alla tassonomia in cui si trova il CPT?

gil hamer gil hamer
8 apr 2014 12:28:20
8
15

Se hai bisogno di link successivo/precedente per i singoli articoli, WordPress offre le funzioni integrate next_post_link e la corrispondente previous_post_link, entrambe dovrebbero essere utilizzate all'interno del loop.

Per gli archivi, usa next_posts_link e previous_posts_link.

Tutte queste funzioni funzionano correttamente anche con i custom post type.

19 ott 2011 23:12:33
Commenti

Non funzionano nel mio tema.

AndrettiMilas AndrettiMilas
19 ott 2011 23:26:05

Okay. Senza vedere il tuo codice, è difficile capire perché. Ci sono errori o warning PHP? Hai inserito più post per cui le funzioni possano recuperare i link?

chrisguitarguy chrisguitarguy
19 ott 2011 23:46:52

Ho aggiornato la mia domanda sopra.

AndrettiMilas AndrettiMilas
19 ott 2011 23:57:03

Provalo con il terzo argomento TRUE e facci sapere.

chrisguitarguy chrisguitarguy
20 ott 2011 00:11:28

Terzo argomento true?

AndrettiMilas AndrettiMilas
20 ott 2011 00:13:18

Usa questo <?php next_post_link('%link', 'Next post »'); ?> e <?php next_post_link('%link', 'Next post »'); ?>

chrisguitarguy chrisguitarguy
20 ott 2011 00:16:29

Questo funziona sui custom post type, ma per quanto riguarda la stessa tassonomia del post corrente?

jepser jepser
4 ott 2012 23:08:49

@jepser (Mi presento in ritardo alla festa) Dovresti specificare la tassonomia da utilizzare come quinto argomento. Se passi solo i primi 3 argomenti e abiliti il terzo per mantenere la stessa tassonomia, molto probabilmente fallirà perché la tassonomia predefinita è category (che il tuo CPT probabilmente non ha). Vedi gli articoli del codex collegati nella risposta.

Evan Mattson Evan Mattson
7 feb 2014 21:39:25
Mostra i restanti 3 commenti
1

Puoi utilizzare get_adjacent_post per ottenere l'oggetto del post precedente o successivo nel loop.

Puoi modificare il terzo parametro su true o false per ottenere rispettivamente il post successivo o precedente. get_adjacent_post( false, '', true);

Con questa conoscenza possiamo usare get_the_permalink per ottenere l'URL dei post e creare i tuoi link personalizzati, senza dover rimuovere elementi indesiderati che WordPress aggiunge con altri metodi.

Questo metodo è particolarmente utile se vuoi stilizzare i link ipertestuali autonomamente e avere pieno controllo sulla formattazione.

<?php
    $next_post = get_adjacent_post( false, '', false);
    $next_post_url = get_the_permalink($next_post);

    $previous_post = get_adjacent_post( false, '', true);
    $previous_post_url = get_the_permalink($previous_post);
?>

<a href="<?php echo $next_post_url;?>">Post successivo</a>
<a href="<?php echo $previous_post_url;?>">Post precedente</a>
3 feb 2022 13:40:09
Commenti

Inoltre, se vuoi creare un ciclo continuo di articoli puoi usare questo: https://wplancer.com/infinite-next-and-previous-post-looping-in-wordpress/

SunnyRed SunnyRed
29 ago 2022 19:55:52