在自定义帖子中循环多个分类

时间:2014-12-09 作者:user3766356

我有一个名为“摇滚”的自定义帖子和分类法“流派”,我需要用标签或类似的东西来过滤帖子

post\\u type>taxonomy>taxonomy\\u标记(tagsss)

我找不到解决方案:(

我已经在功能中注册了一个新的标签分类法(比如标签)(这不是一个好的选择)

    add_action( \'init\', \'create_tag_taxonomiess\', 0 );
    function create_tag_taxonomiess() 
    {
      // Add new taxonomy, NOT hierarchical (like tags)
      $labels = array(
        \'name\' => _x( \'Tags\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Tag\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Tags\' ),
        \'popular_items\' => __( \'Popular Tags\' ),
        \'all_items\' => __( \'All Tags\' ),
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edit Tag\' ), 
        \'update_item\' => __( \'Update Tag\' ),
        \'add_new_item\' => __( \'Add New Tag\' ),
        \'new_item_name\' => __( \'New Tag Name\' ),
        \'separate_items_with_commas\' => __( \'Separate tags with commas\' ),
        \'add_or_remove_items\' => __( \'Add or remove tags\' ),
        \'choose_from_most_used\' => __( \'Choose from the most used tags\' ),
        \'menu_name\' => __( \'Tags\' ),
      ); 

      register_taxonomy(\'tagsss\',\'rock\',array(
        \'hierarchical\' => false,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'update_count_callback\' => \'_update_post_term_count\',
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'tag\' ),
      ));

}
分类法中的我的循环类型事件。php

<?php $args = array( \'post_type\' => \'rock\',\'taxonomy\' => \'genres\',\'tagsss\' =>\'concerts\', \'orderby\' => \'menu-order\' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $rock; ?>

 <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile;?>

EDIT:

我自己找到了解决办法

        <?php
$args = array(
    \'tax_query\' => array(
        array(

            \'posts_per_page\' => 5,
            \'order\' => \'DESC\',
            \'taxonomy\' => \'tagsss\',
            \'field\' => \'slug\',
            \'terms\' => \'concerts\',

        )
    )
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();?> 

1 个回复
SO网友:Rarst

您无法通过分类法(仅通过术语)进行广泛的本机查询

如果你学习到Taxonomy Parameters 在Codex中,现在很容易为所需的多组术语构造条件。

尽管如此,它并不是完全灵活的,我不知道如何简单地在我的头脑中做“在某些分类法中有任何术语”之类的事情。

结束

相关推荐

Get multiple tags by slug

以下代码对我来说很好:$term = get_term_by(\'slug\', ‘foo’, \'post_tag\'); $args = array ( \'posts_per_page\' => 5, \'paged\' => $paged, \'post__not_in\' => get_option( \'sticky_posts\' ) , \'tag__not_in\' => $term