簡體   English   中英

WP自定義帖子類型查詢不返回自定義分類

[英]WP custom post type query does not return custom taxonomy

我有以下CPT:

        array(
            'slug'        => 'articles',
            'single_name' => 'Article',
            'plural_name' => 'Articles',
            'menu_name'   => 'Articles',
            'description' => '',
            'dashicon'    => 'dashicons-media-default'
        ),

以及根據CPT條款出現的以下自定義分類:

        array(
            'slug'        => 'author',
            'single_name' => 'Author',
            'plural_name' => 'Authors',
            'menu_name'   => '→ Authors',
            // This is where you sync the taxonomy to the post types above
            'post_type'   => ['articles', 'news']
        ),

現在,我正在嘗試創建一個單篇articles.php,我在其中查詢單篇文章,以包含所有自定義分類信息。

我嘗試了以下各種變化:

$args = array(
    'post_type' => 'articles',
    'tax_query' => array(
        array(
            'taxonomy' => 'author',
            'field' => 'slug'
        ),
    ),
);

我對PHP和WP相對較新,並不是100%確定如何檢索數據。 在Javascript中,您將獲得一個可以遍歷的對象。 添加print_r($my_query)我找不到我要找的東西。 在文檔的早些時候,我已經啟動了循環:

if (have_posts()) :
  while (have_posts()) : the_post()

但我無法弄清楚為什么php / wp不會顯示對象中包含的所有數據。 理想情況下, 我只想查詢單篇文章並獲取所有數據,以包含有關所有附加分類法的信息 不想查詢文章被分類所以我有兩個問題:

1)如何正確查詢以獲得結果? 2)如何在前端顯示此數據?

編輯: 根據這一 tax_query時,忽略is_singular()是正確的。 print_r( is_singular() )得到1 (又名真?!),這會有所不同嗎?

我還有附加到分類法的自定義帖子類型。 我也需要檢索這些信息。

您的分類標本是author而非authors

$args = array(
'post_type' => 'articles',
'posts_per_page' => -1,
'tax_query' => array(
    array(
        'taxonomy' => 'author',
        'field' => 'slug',
        'terms' => 'your-term-here',
    ),
  ),
);

您可以對單個模板使用single-{posttype} .php。

並且,如果您已將has_archive參數設置為true來注冊帖子類型,則可以使用archive- {posttype} .php作為歸檔模板。

您可以檢查模板層次結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM