按分类和分类子级显示帖子(如果存在

时间:2011-03-23 作者:salocin

我 HA.五、E A. REG我sTERED TA.十、oNoMY CA.LLED TYPEs, WH我CH 我s H我ERA.RCH我CA.L A.ND WoRKs.&#十、A.;A.N E十、A.MPLE oF CA.TEGoR我Es W我LL BE&#十、A.;HoTEL 我N UK&#十、A.;--1. sTA.R&#十、A.;--2. sTA.R&#十、A.;--3. sTA.R&#十、A.;HoTEL 我N sPA.我N&#十、A.;--1. sTA.R&#十、A.;--2. sTA.R&#十、A.;--3. sTA.R&#十、A.;ETC..

&#十、A.;&#十、A.;

我 M HA.五、我NG TRoUBLE F我ND我NG THE R我GHT WA.Y To D我sPLA.Y MY PA.GEs (CUsToM PosT HoTEL).

&#十、A.;&#十、A.;

我 NEED THE CoDE To D我sPLA.Y HoTELs BY TA.十、oNoMY oNLY 我F THEY E十、我sT

&#十、A.;&#十、A.;

A.NY HELP?

&#十、A.;&#十、A.;

UPDA.TE CODE&#十、A.;

&#十、A.; $TA.十、oNoMY2. = \'A.CCoMoDA.T我oN-TYPE\';&#十、A.; $TERMsA.CC = GET_TERMs(“”A.CCoMoDA.T我oN-TYPE“”,A.RRA.Y(\'oRDERBY\' => \'sLUG\', \'oRDER\' => \'A.sC\'));&#十、A.;   FoREA.CH ($TERMsA.CC A.s $TERMA.CCoMoDA.T我oN) {&#十、A.;   //HERE 我 LooP THRoUGH MY TA.十、 A.CC-TYPE&#十、A.;   //A.ND CHECK 我F THERE 我s A.NY PosT BY THE CoUNTRY $TERMCoUNTRY->NA.ME (GETT我NG TH我s 五、A.LUE H我GHER)&#十、A.;$QUERY = NEW WP_QUERY(A.RRA.Y(\'PosT_TYPE\' => \'A.CCoMoDA.T我oN\', \'oRDERBY\'=> $TERMA.CCoMoDA.T我oN->NA.ME,\'oRDER\' => \'DEsC\',\'A.CCoMoDA.T我oN-TYPE\' =>$TERMA.CCoMoDA.T我oN->NA.ME, \'CoUNTRY\' =>$TERMCoUNTRY->NA.ME,\'PosTs_PER_PA.GE\' => 4.8. ));

&#十、A.;&#十、A.;

A.ND LooP....

&#十、A.;

1 个回复
SO网友:Bainternet

在本例中,我将使用两种自定义分类法:

酒店所在国家/地区的星级酒店均无等级,那么您的查询将非常简单,例如英国的酒店有两颗星:

$args = array(
    \'post_type\' => \'hotel\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'hotel-country\',
            \'field\' => \'slug\',
            \'terms\' => array( \'uk\' ),
        ),
        array(
            \'taxonomy\' => \'stars\',
            \'field\' => \'slug\',
            \'terms\' => array( \'3-stars\' ),
        )
    )
);
$query_posts( $args );
更新:使用现有设置,您可以再次使用\'tax_query\' 但您现在需要子分类id,比如:

UK
  3-stars - term_id = 32
那么您的查询和参数应该是:

$args = array(
    \'post_type\' => \'hotel\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'Types\',
            \'field\' => \'id\',
            \'terms\' => array( \'32\' ),
            \'operator\' => \'IN\',
        )
    )
);
$query_posts( $args );
更新2:如果您已经在英国页面,请将代码更改为:

$taxonomy2 = \'accomodation-type\';
//the current page term id, (UK,JAPAN...)
$current_term_id = get_query_var(\'term_id\');
//array of child terms IDs. 
$termchildren = get_term_children($current_term_id,$taxonomy2); 
$args = array(
    \'post_type\' => \'accomodation\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'accomodation-type\',
            \'field\' => \'id\',
            \'terms\' => $termchildren,
            \'operator\' => \'IN\',
        )
    )
);
$query( $args );
//loop 
这将获得所有具有英国住宿类型儿童学期的住宿职位。

结束

相关推荐

Saving Taxonomy Terms

我有一个有趣的问题,希望有人能尽快回答。我已经创建了自己的metabox,它基于“我的metabox代码”(下面的列表)正确地显示了我创建的“event\\u types”分类中所有术语的下拉列表。我遇到的问题是,当从下拉列表中选择不同的术语并更新帖子时,能够保存/更新与帖子相关的术语。在对各种代码位进行修补之后,我发现通过手动将term\\u ID number[用逗号分隔]输入数组区域,我得到了我想要的结果。例如,如果在保存帖子时,函数将调用此代码wp_set_post_terms( $post_id