如何显示分类?我创建了一个插件,然后创建了一个自定义类型。还有一个分类法,我也已经登记了

时间:2021-03-07 作者:tasos Nz

所以我的问题是,我可以把代码放在哪里来显示分类法?我找到了这个代码

`function get_taxonomies( $args = array(), $output = \'names\', $operator = \'and\' ) {
    global $wp_taxonomies;
 
    $field = ( \'names\' === $output ) ? \'name\' : false;
 
    return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field );
}`
但它不起作用,我会出错。我的意思是我必须做一个新的文件把它放在那里?

分类法是

   function create_post_type()
{
    $labels = array(
        \'name\' => __(\'Offer\'),
        \'singular_name\' => __(\'offer\'),
        \'add_new\' => __(\'New offer\'),
        \'add_new_item\' => __(\'Add New offer\'),
        \'edit_item\' => __(\'Edit offer\'),
        \'new_item\' => __(\'New offer\'),
        \'view_item\' => __(\'View offer\'),
        \'search_items\' => __(\'Search offer\'),
        \'not_found\' =>  __(\'No offer Found\'),
        \'not_found_in_trash\' => __(\'No offer found in Trash\'),
    );
    $args = array(
        \'labels\' => $labels,
        \'has_archive\' => true,
        \'public\' => true,
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'excerpt\',
            \'custom-fields\',
            \'thumbnail\',
            \'page-atributes\'
        ),
        \'taxonomies\' => array(\'post_tag\', \'category\'),
    );
    register_post_type(\'offer\', $args);
}

add_action(\'init\', \'create_post_type\');


function htl_register_taxonomy()
{
    $args = array(
        \'labels\' => array(
            \'name\' => __(\'hotel\'),
            \'singular_name\' => __(\'hotel\'),

        ),
        \'public\' => true,
    );

    register_taxonomy(\'hotel\', array(\'offer\'), $args);
}
add_action(\'init\', \'htl_register_taxonomy\');

1 个回复
SO网友:rahmat hidawe

在主题自定义页面或调试页面中尝试此选项:

<?php

$args = array(
    \'post_type\' => \'offer\',
    \'posts_per_page\' => -1 //show all posts
);

$loop = new WP_Query($args);


if($loop->have_posts()){
    while($loop->have_posts()){
        $loop->the_post();

    $id = get_the_ID();

    $tax = get_the_terms($id, \'hotel\');

    print_r($tax);

    }//endwhile
    wp_reset_postdata();
}//endif

相关推荐

Filter Custom Taxonomy Posts

我有一个自定义的分类页面,它也有一个向上的滑块。问题是滑块显示来自所有分类法而非当前分类法的随机帖子。是否有办法过滤滑块中的图像,使其仅使用当前分类法?<?php global $taxonomy_location_url, $taxonomy_profile_url; $args = array( \'post_type\' => $taxonomy_profile_url, \'orderby\' => \'rand\', \'meta_que