Riscrittura di un permalink custom-post-type con termine di tassonomia?

16 apr 2012, 09:23:57
Visualizzazioni: 35.3K
Voti: 17

Sto cercando di riscrivere l'URL per un custom_post_type chiamato wr_events con uno dei suoi termini custom_taxonomy da event_type

add_action('init', 'wr_events');

function wr_events() {

     register_taxonomy(
        'event_type',
        'wr_event',
        array(
            'label' => 'Tipi',
            'singular_label' => 'Tipo',
            'hierarchical' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'events'),
        )
    );

    $labels = array(
        'name' => _x('Eventi', 'nome generale del tipo di post'),
        'singular_name' => _x('Evento', 'nome singolare del tipo di post')
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail', 'excerpt'),
        'rewrite' => array(
            //'slug' => 'event',
            'slug' => 'events/%event%',
            'with_front' => false
        ),
        'has_archive' => 'events'
    ); 

    register_post_type( 'wr_event' , $args );
    flush_rewrite_rules();
}

add_action('save_post', 'save_details');

add_filter('post_type_link', 'events_permalink_structure', 10, 4);
function events_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%event%' ) ) {
        $event_type_term = get_the_terms( $post->ID, 'event_type' );
        $post_link = str_replace( '%event%', array_pop( $event_type_term )->slug, $post_link );
    }
    return $post_link;
}

Nel mio caso i termini della tassonomia sarebbero "workshop" o "lectures" ecc. url/events/lectures o url/events/workshops elenca tutti i miei post relativi a questa "categoria", url/events mostra un archivio personalizzato per tutti i miei eventi. -> questo è esattamente quello che voglio, tuttavia l'unica cosa che non funziona è l'URL completo del custom-post stesso...

url/events/lectures/post-name - restituisce un 404!

Qualche idea sul perché sta succedendo questo? La mia funzione events_permalink_structure() sembra funzionare correttamente poiché sostituisce i miei permalink esattamente nel modo in cui voglio.

Ho installato il plugin "Rewrite Analyzer" e mi mostra "Regex is empty" per wr_event.

Ho anche provato a fare il flush delle Rewrite Rules visitando le impostazioni dei permalink. Tuttavia nessun effetto.

0
Tutte le risposte alla domanda 1
3
11

Cambia tutti i tuoi %event% in %event_type%. Spero che funzioni per te.

16 apr 2012 14:33:00
Commenti

Grazie mille davvero! Fantastico! Non capisco come funziona ma funziona :)

mathiregister mathiregister
16 apr 2012 17:57:18

@mathiregister - è perché WP non riconosce automaticamente il tag %event%, ma capisce che %event_type% corrisponde alla tua tassonomia del tipo di evento.

Stephen Harris Stephen Harris
9 mag 2012 18:55:51

Ora ho un custom post type con slug /catalog/ che mostra tutti i post, /katalog/whatever/ mostra i post nella tassonomia personalizzata con slug whatever, e /katalog/whatever/mypost mostra il singolo post che appartiene alla tassonomia whatever. Basta sostituire %event% nel codice sopra con il nome della tua tassonomia personalizzata tra %%

User User
16 ago 2012 22:51:51