查询某些帖子的自定义分类

时间:2013-08-21 作者:RussC

我试图根据自定义分类法在模板上显示一些帖子。

这是我的模板代码:

<?php $field = get_field(\'newsletter_date\'); ?>
                <?php 
                $args = array (
                \'orderby\'                => \'rand\',
                \'taxonomy\' => \'newsdates\',
                \'field\' => \'id\',
                \'terms\' => "$field",
                \'post_type\' => \'post\',
                );

                $queryarticles = new WP_Query( $args ); ?>

                <?php while ($queryarticles -> have_posts()) : $queryarticles -> the_post(); ?>

                <h4 class="homerssfeed"><a href="<?php the_permalink() ?>" class="nound"><?php the_title(); ?></a></h4>

                <span class="dcecright">
                    <?php the_content(); ?>

                </span>
                <?php endwhile;?>
                <?php wp_reset_postdata(); ?>
这就是我的功能。php:

// Register Custom Taxonomy
function newsdates_taxonomy()  {

    $labels = array(
        \'name\'                       => _x( \'Newsletter Dates\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Newsletter Date\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Newsletter Date\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Newsletter Dates\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Date\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Date:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Date\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add Newsletter Date\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Date\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Date\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate dates with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search dates\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove dates\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used dates\', \'text_domain\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
    );
    register_taxonomy( \'newsdates\', \'post\', $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'newsdates_taxonomy\', 0 );

}

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

您在WP\\U查询中设置的分类参数不正确。您需要设置tax_query, 它接受一个数组。看见the codex.

$args = array(
    \'post_type\' => \'post\',
        \'orderby\' => \'rand\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'newsdates\',
            \'field\' => \'id\',
            \'terms\' => $field
        )
    )
);

结束

相关推荐