WordPress: creare la navigazione tra i post di una serie

In WordPress è relativamente semplice creare la navigazione per i post che fanno parte di una serie.

La soluzione è la seguente:


function my_post_nav( $atts, $content = null ) {
  global $post;
  
  extract( shortcode_atts( array(
    'previous' => '',
    'next' => ''
  ), $atts ) );

  $prev = get_previous_post();
  $nxt = get_next_post();
  $previous_link = ( empty( $previous ) ) ? get_permalink( $prev->ID ) : get_permalink( intval( $previous ) );
  $next_link = ( empty( $next ) ) ? get_permalink( $nxt->ID ) : get_permalink( intval( $next ) );
  $previous_title = ( empty( $previous ) ) ? get_the_title( $prev->ID ) : get_the_title( intval( $previous ) );
  $next_title = ( empty( $next ) ) ? get_the_title( $nxt->ID ) : get_the_title( intval( $next ) );

  return '<nav><a href="' . $previous_link . '">' . $previous_title . '</a><a href="' . $next_link . '">' . $next_title . '</a></nav>';
  
}
add_shortcode( 'my-post-nav', 'my_post_nav' );

Torna su