从特定分类术语获取帖子

时间:2015-07-06 作者:LinekereDe

我有nhl新闻网站,我有每个季节的分类法(例如2014-15)

功能分类:

function wptp_register_taxonomy() {
    register_taxonomy( \'season\', \'post\',
        array(
            \'labels\' => array(
                \'name\'              => \'Seasons\',
                \'singular_name\'     => \'Season\',
                \'search_items\'      => \'Search Season\',
                \'all_items\'         => \'All Season\',
                \'edit_item\'         => \'Edit Season\',
                \'update_item\'       => \'Update\',
                \'add_new_item\'      => \'Add New Season\',
                \'new_item_name\'     => \'New Season\',
                \'menu_name\'         => \'Seasons\',
            ),
            \'hierarchical\' => true,
            \'sort\' => true,
            \'args\' => array( \'orderby\' => \'term_order\' ),
            \'rewrite\' => array( \'slug\' => \'season\' ),
            \'show_admin_column\' => true
        )
    );
}
add_action( \'init\', \'wptp_register_taxonomy\' );
我将“新闻”类别中的所有帖子分配到每个季度的不同术语中,例如:20132014 20142015

我创建分类术语查询,因此当我转到此url时localhost/nhl/news/?season=20132014 向我显示此特定季节的帖子,而不是转到此处localhost/nfl/season/20132014

我想做什么?

获取本学期ex 20142015(当前真实赛季)的帖子,并在分类新闻中显示localhost/nhl/news 不显示所有术语中的所有帖子,也不使用url查询?season=(term)

本地主机/nhl<--folder name in wamp/news<--categoryname/

非常感谢。

1 个回复
SO网友:Anand

此代码将显示wordpress中所有分类法的帖子:

$args = array(
    \'type\'                     => \'post\',
    \'child_of\'                 => 0,
    \'parent\'                   => \'\',
    \'orderby\'                  => \'name\',
    \'order\'                    => \'ASC\',
    \'hide_empty\'               => 1,
    \'hierarchical\'             => 1,
    \'exclude\'                  => \'\',
    \'include\'                  => \'\',
    \'number\'                   => \'\',
    \'taxonomy\'                 => \'season\',
    \'pad_counts\'               => false 

    ); 
$taxonomy = get_categories( $args );
foreach ( $taxonomy as $tax ) {

$posts_array = get_posts(
    array(
        \'posts_per_page\' => -1,
        \'post_type\' => \'post\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'season\',
                \'field\' => \'term_id\',
                \'terms\' => $tax->term_id,
            )
        )
    )
);

 print_r( $posts_array ); 
}
如果要显示特定的post,请传递以下代码:

$posts_array = get_posts(
    array(
        \'posts_per_page\' => -1,
        \'post_type\' => \'post\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'season\',
                \'field\' => \'term_id\',
                \'terms\' => 2, // taxonomy id
            )
        )
    )
);

 print_r( $posts_array );

结束

相关推荐

WP_LIST_CATEGORIES()-将div添加到每个li?

我正在使用wp_list_categories() 从特定的作者ID打印出每个类别的链接,这很好。然而,我还想添加一个div 致各li 将保留类别的图标。我怎样才能做到这一点?这是我当前的代码:<?php // Display a list of all categories associated with author $cat_array = array(); $args = array( \'author\' =>