Danke an @the_dramatist für den folgenden Code als Antwort auf diese vorherige Frage .
Ich habe ein Follow-up, das ist, dass ich das Folgende verwenden möchte, aber zerlegen / filtern Sie es, so dass ich einzelne Kategorien aus der benutzerdefinierten Taxonomie ziehen kann.
Zum Beispiel folgen die folgenden Ausgaben:
Allgemeines
Regeln
etc…
'questions', // Post type 'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'slug', 'terms' => $term->slug ) ), 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ) ); ?> name; ?>
- post_title; ?> post_content; ?>
Meine Frage ist jetzt:
Wenn ich nur eine Kategorie anzeigen möchte, zum Beispiel General, wie würde ich das filtern? Ich habe versucht, include
und post_in
gefolgt von der ID der Kategorie Allgemein, aber das funktioniert nicht.
Entschuldigung, wenn das eine einfache Frage ist, aber etwas über cpt und Taxonomien verblüfft mich einfach!
AKTUALISIEREN – KANN DIESES ZU ARBEITEN?
Wenn ich @ the_dramatists Antwort unten benutze, bekomme ich immer noch alle Sektionen, ich kann es nicht aussortieren. Ich habe versucht mit IDs wie unten beantwortet und sogar die Slug.
'questions', // Post type 'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'slug', 'terms' => 'general' ) ), 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ) ); ?> name; ?>
- post_title; ?> post_content; ?>
oder benutze diese tax_query
:
'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'term_id', 'terms' => '1005' ) ),
Beide Male werden alle Taxonomien angezeigt und doch sieht alles gut aus.
UPDATE 2
Das sollte alles richtig funktionieren, aber es ist nicht. Könnte es etwas mit der Art und Weise zu tun haben, wie ich meinen benutzerdefinierten Post-Typ und meine Taxonomien eingerichtet habe? So habe ich sie:
add_action( 'init', 'questions_post_type'); function questions_post_type() { $labels = array( 'name' => 'Questions', // This is the Title of the Group 'singular_name' => 'Question', // This is the individual type 'add_new' => 'Add New', // The add new menu item 'add_new_item' => 'Add New Question', // Add New Display Title 'edit' => 'Edit', // Edit Dialog 'edit_item' => 'Edit Questions', // Edit Display Title 'new_item' => 'New Question', // New Display Title 'view_item' => 'View Question', // View Display Title 'search_items' => 'Search Questions', // Search Custom Type Title 'not_found' => 'No questions yet, why not create some?', // This displays if there are no entries yet 'not_found_in_trash' => 'No questions found in Trash', // This displays if there is nothing in the trash 'parent_item_colon' => '' ); register_post_type( 'questions', array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'menu_position' => 20, // this is what order you want it to appear in on the left hand side menu 'menu_icon' => 'dashicons-editor-help', // the icon for the custom post type menu. uses built-in dashicons (CSS class name) 'query_var' => true, 'rewrite' => array( 'with_front' => false, 'slug' => 'questions' // you can specify its url slug ), 'capability_type' => 'page', 'has_archive' => true, 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'revisions') )); } register_taxonomy( 'sections', array('questions'), // if you change the name of register_post_type( 'directions', then you have to change this array( 'hierarchical' => true, 'labels' => array( 'name' => __( 'Sections', 'jointswp' ), // name of the custom taxonomy 'singular_name' => __( 'Section', 'jointswp' ), // single taxonomy name 'menu_name' => __( 'Sections', 'jointswp'), // name as it appears in Dashboard menu 'search_items' => __( 'Search Sections', 'jointswp' ), // search title for taxomony 'all_items' => __( 'All Sections', 'jointswp' ), // all title for taxonomies 'edit_item' => __( 'Edit Section', 'jointswp' ), // edit custom taxonomy title 'update_item' => __( 'Update Section', 'jointswp' ), // update title for taxonomy 'add_new_item' => __( 'Add New Section', 'jointswp' ), // add new title for taxonomy 'new_item_name' => __( 'New Section', 'jointswp' ) // name title for taxonomy ), 'show_admin_column' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'with_front' => false, 'slug' => 'sections' ), 'hierarchical' => true, 'update_count_callback' => '_update_post_term_count' ) );
Ja, das kannst du tun. Entfernen Sie einfach die erste foreach
und geben Sie die allgemeine Kategorie ID und ändern Sie den Code wie unten-
// Initiating shortcode. Place this code to any of your page to get your desired output. add_shortcode( 'faq_page_content', 'the_dramatist_faq_page_content'); /** * Rendering function */ function the_dramatist_faq_page_content() { echo ''; $posts = get_posts( array( 'post_type' => 'questions', // Post type 'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'term_id', // For taxonomy ID 'terms' => 'the-ID-of-the-taxonomy' // Here you need to pass the term ID, in you case General taxonomy term ID ) ), 'posts_per_page' => -1 ) ); echo '- ' . $term->name; echo '
'; foreach($posts as $post) { echo '- ' . $post->post_title . '
'; } echo '
'; echo '
'; }
Oder für Ihren aktuellen Seitenvorlagencode
< ?php $posts = get_posts( array( 'post_type' => 'questions', // Post type 'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'term_id', // For ID 'terms' => 'the-ID-of-the-taxonomy' // Here you need to pass the term ID, in you case General taxonomy term ID ) ), 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ) ); ?> < ?php echo $term->name; ?>
< ?php foreach($posts as $post) { ?> - < ?php echo $post->post_title; ?> < ?php echo $post->post_content; ?>
< ?php } ?>
Schauen Sie genau hin, hier verwenden wir die Taxonomie-ID, um Post für einen Taxonomiebegriff zu bekommen, OK? –
'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'term_id', // For ID 'terms' => 'the-ID-of-the-taxonomy' // Here you need to pass the term ID, in you case General taxonomy term ID ) )
Sie können einen Taxonomiebegriff auch nach dem Begriff slug
. Und dafür müssen Sie den vorherigen Block wie unten ändern
'tax_query' => array( array( 'taxonomy' => 'sections', // Taxonomy name 'field' => 'slug', // For ID 'terms' => 'the-slug-of-the-taxonomy' // Here you need to pass the term slug, in you case General taxonomy term slug ) )
Ich hoffe, das hilft !!!