Query tassonomia personalizzato termine id?
-
16-10-2019 - |
Domanda
Ho fatto un tipo di messaggio personalizzato e una tassonomia personalizzato, ma ho un problema.
Quando accedo al http://www.ithemes.co.kr/?shopcat=galaxy-s
URL, funziona bene. Ma quando accedo al http://www.ithemes.co.kr/?shopcat=5
URL, restituisce una pagina 404. galaxy-s
è il termine tassonomia lumaca, ma voglio accedervi term_id
, come si può con una categoria: cat=1
. Io preferisco cat=1
.
Che cosa posso fare?
Soluzione
Questa non è supportata di default, ma è possibile convertire i termini numerici indietro per lumache con il gancio pre_get_posts
. Ho provato questo con WP 3.0.1, ma in 3.1 query tassonomia manipolazione cambiato, quindi non so se questo funziona per difetto o se c'è un modo migliore per farlo.
add_action( 'pre_get_posts', 'wpse6066_pre_get_posts' );
function wpse6066_pre_get_posts( &$wp_query )
{
if ( $wp_query->is_tax ) {
if ( is_numeric( $wp_query->get( 'term' ) ) ) {
// Convert numberic terms to term slugs
$term = get_term_by( 'term_id', $wp_query->get( 'term' ), $wp_query->get( 'taxonomy' ) );
if ( $term ) {
$wp_query->set( 'term', $term->slug );
}
}
}
}
Bizzarro che si preferisce la versione numerica, molti avrebbero scelto la lumaca termine per motivi di SEO.
Altri suggerimenti
Queste poche righe sono molto utili per wp_dropdown_categories
utilizzato in tassonomie personalizzate per backend (edit.php) Lista sottoselezione. Poiché i valori in elenco sono term id
e non utilizzabile come per la selezione sub nella query. Le parti di codice qui sotto sono stati utilizzati in una classe di gestione di posta personalizzati tipo e personalizzato tassonomia.
add_action( 'restrict_manage_posts', array(&$this,'restrict_manage_writer_posts') );
add_action( 'pre_get_posts', array(&$this,'wpse6066_pre_get_posts' ) );
function restrict_manage_writer_posts () {
$selected = "";
if ( isset ( $_GET['writer_name'] ) ) {
$selected = $_GET['writer_name'];
}
$dropdown_options = array(
'taxonomy' => 'writer',
'show_option_all' => __( 'View all writers' ),
'hide_empty' => 0,
'hierarchical' => 1,
'show_count' => 0,
'orderby' => 'name',
'name' => 'writer_name',
'selected' => $selected
);
wp_dropdown_categories( $dropdown_options );
}
/**
* to fixes wp_dropdown_categories id value in option
* thanks to http://wordpress.stackexchange.com/questions/6066/query-custom-taxonomy-by-term-id
*/
function wpse6066_pre_get_posts( &$wp_query )
{
if ( $wp_query->is_tax ) { ;
if ( is_numeric( $wp_query->get( 'writer_name' ) ) ) {
// Convert numberic terms to term slugs for dropdown
$term = get_term_by( 'term_id', $wp_query->get( 'writer_name' ), 'writer' );
if ( $term ) {
$wp_query->set( 'writer_name', $term->slug );
}
}
}
}