Modificare il permalink del custom post type

16 set 2014, 10:35:11
Visualizzazioni: 26K
Voti: 1

Ho una categoria progetti nel mio tema (custom post type).

Il mio link è così strutturato

http://shaowtriger.com/project/naim/

Il link che vorrei ottenere è

http://shaowtriger.com/brands/naim/

Ho anche una pagina come

http://shaowtriger.com/brands

Sono confuso su come ottenere questo risultato.

Avete suggerimenti sulle regole di riscrittura URL?

1
Commenti

Non chiedere raccomandazioni sui plugin. Questo è fuori tema

Pieter Goosen Pieter Goosen
16 set 2014 11:58:20
Tutte le risposte alla domanda 1
7

Puoi utilizzare l'argomento rewrite in register_post_type. Puoi avere un nome per il custom post type diverso dallo slug di rewrite. Ad esempio, ecco un codice di esempio con un custom post type "project" che mostra le pagine con http://shaowtriger.com/brands/naim/

Nota il rewrite in $args

add_action( 'init', 'codex_brand_init' );
function codex_brand_init() {
  $labels = array(
    'name'               => _x( 'Brands', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Brand', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Brands', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Brand', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'brand', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Brand', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Brand', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit Brand', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View Brand', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All Brands', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search Brands', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent Brands:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No brands found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No brands found in Trash.', 'your-plugin-textdomain' )
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'brands' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );

  register_post_type( 'project', $args );
  flush_rewrite_rules();

}

Anche se avere solo la pagina base http://shaowtriger.com/brands/ non dovrebbe interferire con lo slug e la funzionalità del CPT, avere sottopagine alla pagina brands non funzionerà perché WordPress cercherà un elemento del CPT invece della sottopagina.

16 set 2014 10:51:16
Commenti

ho già questo nel mio functions.php con Projects al posto di Brands... ho cambiato quello slug ma non funziona

user1145009 user1145009
16 set 2014 11:12:19

Devi anche eseguire flush_rewrite_rules(); e poi visitare nuovamente la pagina delle impostazioni dei permalink.

Robert hue Robert hue
16 set 2014 11:25:39

non ho capito... dove dovrei usare flush_rewrite_rules();

user1145009 user1145009
16 set 2014 12:02:17

Puoi usarlo quando registri il CPT. Ho aggiornato il mio codice sopra e aggiunto flush_rewrite_rules();. Puoi aggiungerlo alla tua funzione in modo simile.

Robert hue Robert hue
16 set 2014 12:17:28

Non mantenere mai flush_rewrite_rules(); È una funzione molto costosa da usare. Usala una sola volta e poi cancellala, oppure agganciala a un'azione che viene eseguita una sola volta, come quando un tema o un plugin viene attivato.

Pieter Goosen Pieter Goosen
16 set 2014 12:48:14

Se hai familiarità con l'interfaccia a riga di comando, puoi installare wp-cli e usare il comando wp rewrite flush

Slam Slam
10 ott 2017 00:07:17

Cosa succede se volessi modificare ulteriormente il permalink in http://shaowtriger.com/brands/2018/naim/ Ovvero: /brands/%year%/%postname% ? Ho trovato questo: https://wordpress.stackexchange.com/questions/216337/change-permalink-structure-for-custom-post-type/216470#216470 Ma in qualche modo questo non ha mai effetto. Qualcuno conosce un esempio con un po' più di codice, magari includendo la registrazione del post come sopra? Questo potrebbe aiutarmi un po'...

Merc Merc
2 nov 2018 12:44:36
Mostra i restanti 2 commenti