Tassonomia, Termini e File Template in WordPress

10 apr 2011, 22:57:22
Visualizzazioni: 4.02K
Voti: 7

Ho un post type 'product' e una tassonomia gerarchica 'types' associata ad esso. In questa tassonomia, ho termini come 'dry-clean', 'washer', ecc. con diversi sottotermini per ogni termine.

Nella mia situazione, ho bisogno di visualizzare, ad esempio:

  1. http://example.com/types/washer -> visualizzare tutti i sottotermini
  2. http://example.com/types/washer/{subterm} -> visualizzare tutti i post

Le mie domande sono:

  1. Come ottengo il permalink URL per i termini genitori? Ho provato l'URL sopra, ma risulta in un 404.
  2. Come funzionano i file template per questo tipo di problema?
    1. È sufficiente taxonomy-types-washer.php per il caso #1? O devo creare taxonomy-types.php e implementare la logica lì?
    2. Dopo aver esaminato la gerarchia dei template, penso di aver bisogno di taxonomy-types-{term_parent}.php per elencare tutti i sottotermini e taxonomy-types.php per elencare tutti i product nei sottotermini.
    3. Infine, per visualizzare ogni product, dovrò creare single-product.php
  3. Su un problema leggermente diverso. Ho notato che archive-{posttype}.php non funziona quando non ho alcun post. C'è una soluzione per questo? (dovrei creare una domanda separata o mantenerla qui?)

AGGIORNAMENTO

Ho controllato le opzioni rewrite_rules e non elenca [types]. Non so perché. Per testare, ho cambiato lo slug in product-types, ho resettato i permalink e ora elenca questo:

[product-types/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/([^/]+)/page/?([0-9]{1,})/?$] => index.php?types=$matches[1]&paged=$matches[2]
[product-types/([^/]+)/?$] => index.php?types=$matches[1]

Quindi credo sia registrato ora. Provo a caricare l'URL product-types/washer ma va in 404. Provo anche index.php?types=washer, ma va comunque in 404. Attualmente ho questi file:

  • taxonomy-types-washer.php
  • taxonomy-types.php

Non capisco cosa non funzioni :(

AGGIORNAMENTO #2

Ho trovato il problema. Mancava 'rewrite'=>array('hierarchical'=>true)

Ecco le nuove rewrite_rules:

[product-types/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?types=$matches[1]&feed=$matches[2]
[product-types/(.+?)/page/?([0-9]{1,})/?$] => index.php?types=$matches[1]&paged=$matches[2]
[product-types/(.+?)/?$] => index.php?types=$matches[1]
3
Commenti

So che questo può essere fatto http://wordpress.stackexchange.com/questions/5308/custom-post-types-taxonomies-and-permalinks/5313#5313 ma per tassonomie "non gerarchiche".

Bainternet Bainternet
11 apr 2011 02:09:29

Dovresti creare una nuova domanda per il punto 3.

scribu scribu
11 apr 2011 03:09:45

@scribu, creerò quella domanda stasera, quando tornerò a casa.

ariefbayu ariefbayu
11 apr 2011 12:14:13
Tutte le risposte alla domanda 1
0

Questi tipi di URL sono supportati da WP 3.1:

register_taxonomy( 'types', 'post', array(
  'hierarchical' => true,
  'rewrite' => array( 'hierarchical' => true ),
  ...
);

Ricorda di svuotare le regole di riscrittura dopo aver apportato la modifica.

Il template che useresti sia per i termini genitore che figlio è taxonomy-types.php:

$current_term = get_queried_object();

if ( 0 == $current_term->parent ) {
  // è un termine genitore; mostra i termini figli usando wp_list_categories() ecc.
} else {
  // è un termine figlio; mostra i post usando The Loop ecc.
}
11 apr 2011 03:09:10