税收-分类查询不起作用

时间:2020-01-31 作者:Mats Gustavsson

我正在尝试获取属于特定分类法的自定义posttypes。我没有在分类法上对它进行分类,它需要每个自定义帖子。我使用此代码

    global $post;
$terms = get_terms(\'resmal\');
foreach ($terms as $term) :
    echo get_term_link($term);
    //Define the query
    $args = array(
        \'post_type\' => \'resor\',
        \'tax-query\' => array(
            array(
                \'taxonomy\' => \'resmal\',
                \'field\' => \'slug\',
                \'terms\' => \'Italien\'
            )
        )
    );
    $query = new WP_Query( $args );
        // Start the Loop
    if( $query->have_posts() ):
        while ( $query->have_posts() ) : 
            $query->the_post();
            echo get_the_title();
        endwhile;
    endif;
    wp_reset_query();
    wp_reset_postdata();
endforeach;
自定义posttype

function base_register_resor_post_type() {
  $labels = array(
        \'name\' => __( \'Våra Resor\'),
        \'singular_name\' => __( \'Resa\' ),
        \'add_new\' => __(\'Skapa ny\'),
        \'add_new_item\' => __(\'Skapa ny Resa\'),
        \'edit_item\' => __(\'Redigera Resa\'),
        \'new_item\' => __(\'Ny Resa\'),
        \'view_item\' => __(\'Visa Resa\'),
        \'search_items\' => __(\'Sök Resa\'),
        \'not_found\' =>  __(\'Hittade inget\'),
        \'not_found_in_trash\' => __(\'Papperskorgen är tom!\')
  );
  $args = array(
        \'labels\'                            => $labels,
        \'public\'                            => true,
        \'has_archive\'               => \'resor\',
        \'publicly_queryable\'    => true,
        \'show_ui\'                       => true,
        \'query_var\'                     => true,
        \'rewrite\'                       => array(\'slug\' => \'resa\'),
        \'taxonomies\' => array(\'resmal\'),
        \'capability_type\'       => \'post\',
        \'supports\'                      => array(\'title\',\'editor\',\'thumbnail\',\'excerpt\',\'revisions\'),
        \'show_in_nav_menus\'     => true,
        \'hierarchical\'              => true
  );
  register_post_type(\'resor\', $args);
}
add_action(\'init\', \'base_register_resor_post_type\');
分类学

function base_register_destination_taxonomy() {
  $labels = array(
    \'name\'              => \'Resmål\',
    \'singular_name\'     => \'Resmål\',
    \'search_items\'      => \'Sök Resmål\',
    \'all_items\'         => \'Alla Resmål\',
    \'parent_item\'       => \'Parent Location\',
    \'parent_item_colon\' => \'Parent Location:\',
    \'edit_item\'         => \'Ändra Resmål\',
    \'update_item\'       => \'Updatera Resmål\',
    \'add_new_item\'      => \'Lägg till nytt Resmål\',
    \'new_item_name\'     => \'Nytt Resmål namn\',
    \'menu_name\'         => \'Resmål\'
  );

  $args = array(
    \'hierarchical\' => true,
    \'labels\'       => $labels,
    \'has_archive\'  => true,
    \'rewrite\'      => array( \'slug\' => \'resmal\' ),
    \'public\'       => true
  );
  register_taxonomy(\'resmal\', array(\'resor\'), $args);
}
add_action(\'init\', \'base_register_destination_taxonomy\');

1 个回复
最合适的回答,由SO网友:Greg Winiarski 整理而成

您似乎在第一段中有一个拼写错误

\'tax-query\' => array(
而不是

\'tax_query\' => array(

相关推荐