Ottenere il Permalink all'interno del Loop

19 mag 2015, 06:00:09
Visualizzazioni: 16K
Voti: 2

Ho un tema (basato su Roots/Sage) con un override del template home.php e in quella pagina sto visualizzando estratti e immagini in evidenza per ogni post.

<?php while (have_posts()) : the_post(); ?>  
  <div class=".container-fluid">
    <?php if (get_post_type() == 'instruments') {?>
    <div class="col-md-3">
        <h1><?php the_ID(); ?></h1>
        <a href="<?php get_post_permalink(the_permalink()) ?>"><?php the_post_thumbnail( 'medium' ); ?></a>
        <?php get_template_part('templates/content', get_post_type() != 'post' ? get_post_type() : get_post_format()); ?>
    </div>
  </div>
<?php } ?>

<?php endwhile; ?>

Sembra relativamente semplice e funziona, ma è stato solo per tentativi ed errori che ho ottenuto il link tramite:

 get_post_permalink(the_permalink())

E visto che ci sono così tanti modi sbagliati di fare le cose in php (e wp), mi piacerebbe avere un feedback.

3
Commenti

Usa semplicemente <?php the_permalink(); ?>. Mostrerà il permalink.

Nilambar Sharma Nilambar Sharma
19 mag 2015 07:50:02

Puoi anche usare <?php echo get_post_permalink() ?>.

Robert hue Robert hue
19 mag 2015 08:26:09

Beh, allora almeno il mio codice stava (o meglio, stava facendo) due chiamate a funzione quando ne bastava una sola!

MikeiLL MikeiLL
19 mag 2015 08:28:41
Tutte le risposte alla domanda 2
0

Come suggerito, sono andato al riferimento delle funzioni e da lì al File Sorgente (situato in wp_includes/link-template.php) in cui ci sono quattro funzioni che restituiscono risultati simili.

    <?php echo get_post_permalink() ?>

http://newdep.localhost/instruments/jester/

    <?php echo post_permalink() ?>

http://newdep.localhost/instruments/jester/

    <?php the_permalink() ?>

/instruments/jester/

    <?php echo get_the_permalink() ?>

http://newdep.localhost/instruments/jester/

In questo caso, trattandosi di un custom post type, la funzione che la documentazione descrive come specifica per questo scopo è get_post_permalink(), che, come la maggior parte delle funzioni get_*(), restituisce piuttosto che mostrare un risultato, e quindi necessita di essere utilizzata con echo.

/**
 * Recupera il permalink per un post con un custom post type.
 *
 * @since 3.0.0
 *
 * @param int $id Opzionale. ID del post.
 * @param bool $leavename Opzionale, default false. Indica se mantenere il nome del post.
 * @param bool $sample Opzionale, default false. Indica se è un permalink di esempio.
 * @return string Il permalink del post.
 */
function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
    global $wp_rewrite;

    $post = get_post($id);

    if ( is_wp_error( $post ) )
        return $post;

    $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);

    $slug = $post->post_name;

    $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) );

    $post_type = get_post_type_object($post->post_type);

    if ( $post_type->hierarchical ) {
        $slug = get_page_uri( $id );
    }

    if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
        if ( ! $leavename ) {
            $post_link = str_replace("%$post->post_type%", $slug, $post_link);
        }
        $post_link = home_url( user_trailingslashit($post_link) );
    } else {
        if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
            $post_link = add_query_arg($post_type->query_var, $slug, '');
        else
            $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
        $post_link = home_url($post_link);
    }

    /**
     * Filtra il permalink per un post con un custom post type.
     *
     * @since 3.0.0
     *
     * @param string  $post_link Il permalink del post.
     * @param WP_Post $post      Il post in questione.
     * @param bool    $leavename Indica se mantenere il nome del post.
     * @param bool    $sample    Indica se è un permalink di esempio.
     */
    return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
}
19 mag 2015 09:06:26
0

Come ha menzionato Nilambar, la funzione corretta è <?php the_permalink(); ?>.

Tuttavia, se stai procedendo per tentativi - prova a consultare il Codex di WordPress, in particolare the_permalink per maggiori informazioni, esempi e funzioni correlate.

19 mag 2015 08:33:53