Come ottengo il permalink di un custom post type?

25 ott 2011, 05:26:22
Visualizzazioni: 62.8K
Voti: 20

Posso ottenere il permalink di uno specifico tag o categoria di un post, ma come posso ottenere il permalink di un custom post type? Non riesco a trovare nulla nel Codex o in altre parti su come fare questo.

0
Tutte le risposte alla domanda 5
1
32

Che ne dici di href="<?php echo get_post_type_archive_link( $post_type ); ?>", dove $post_type è il tuo tipo di post?

Per approfondire: Codex

1 dic 2011 20:34:45
Commenti

Piccolo miglioramento; <?php echo get_post_type_archive_link( $post->post_type ); ?>

Usare $post->post_type invece di $post_type restituisce direttamente lo slug.

rwzdoorn rwzdoorn
19 apr 2022 16:40:36
1

All'interno del loop, puoi semplicemente utilizzare the_permalink(). Al di fuori del loop, puoi usare get_permalink( $id ).

25 ott 2011 05:44:38
Commenti

Questo restituisce il permalink di un post o pagina specifico. Voglio restituire il permalink di un tipo di post. Quindi, ad esempio, se c'è un tipo di post chiamato "recensioni di film", e il permalink di quel tipo di post è "www.website.com/recensioni-film", come faccio ad ottenere quel permalink? Forse tecnicamente non è un vero permalink, voglio semplicemente l'URL di quel tipo di post.

Industrial Themes Industrial Themes
25 ott 2011 15:48:59
0

Oppure, per quel che vale, get_term_link($term, $taxonomy); - Codex.

1 dic 2011 22:56:11
0

So che questo post potrebbe essere vecchio, ma nel caso qualcuno stia cercando la funzione che fa questo, ecco quella che ho scritto. $post_type deve essere passato come variabile :)

if( !function_exists( 'wp_get_post_type_link' )  ){
    function wp_get_post_type_link( &$post_type ){

        global $wp_rewrite; 

        // Verifica se esiste l'oggetto post type
        if ( ! $post_type_obj = get_post_type_object( $post_type ) )
            return false;

        // Se i permalink sono attivi e c'è una struttura rewrite
        if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {

            $struct = $post_type_obj->rewrite['slug'] ;
            // Aggiunge il front alla struttura se necessario
            if ( $post_type_obj->rewrite['with_front'] )
                $struct = $wp_rewrite->front . $struct;
            else
                $struct = $wp_rewrite->root . $struct;

            // Genera il link completo
            $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );       

        } else {
            // Link semplice senza permalink
            $link = home_url( '?post_type=' . $post_type );
        }

        // Applica i filtri e restituisce il link
        return apply_filters( 'the_permalink', $link );
    }
}

Spero sia utile! :)

30 lug 2012 13:29:26
0

@Stefan KRUGER

curioso del perché il valore venga passato per riferimento: &$post_type

nota a margine: non capisco perché posso fornire una risposta, che non è ciò che questo è, ma NON un commento, che è ciò che questo è. Sembra controintuitivo.

25 ago 2023 03:02:43