如何对自定义帖子列表进行排序,以获得类别及其子类别下的帖子树一样的视图?

时间:2013-01-11 作者:combi

我是新来的,我的英语不太好,所以我很抱歉:)我来这里是因为我在网站上工作,按类别排序的帖子列表,我在分类和子类别下排序我的帖子时有问题。现在一切都搞砸了。我想对我的自定义帖子进行如下排序:

类别1

儿童类别1

子类别2

第2类

子类别3第3类

发布

子类别4

是否可以使用此代码:

<?php
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON       tax.term_id = terms.term_id WHERE tax.taxonomy = \'MY_CUSTOM_TAXONOMY\'";

$categories = $wpdb->get_results($querystr, OBJECT);

foreach( $categories as $category ): 
echo \'<div class="category-header"><h3>\'.$category->name.\'</h3>\';  
echo \'<p class="category-description">\'.strip_tags(term_description($category->term_id,\'MY_CUSTOM_TAXONOMY\')).\'</p></div>\';  

$posts = get_posts( array( \'MY_CUSTOM_TAXONOMY\' => $category->name, \'post_type\' => \'MY_CUSTOM_POST\' ) );  
foreach($posts as $post) :  
setup_postdata($post);  

 the_title();   

endforeach;

endforeach;
?>
我将非常感谢您的帮助,谢谢!!

4 个回复
SO网友:combi

好的,这是我的工作解决方案:

<?php

$args=array(
\'post_type\'                => \'biblioteka\',
\'child_of\'                 => 0,
\'parent\'                   => \'\',
\'orderby\'                  => \'name\',
\'order\'                    => \'ASC\',
\'hide_empty\'               => 1,
\'hierarchical\'             => 1,
\'exclude\'                  => \'\',
\'include\'                  => \'\',
\'number\'                   => \'\',
\'taxonomy\'                 => \'kategoria-pozycji\',
\'pad_counts\'               => false
);

$categories=get_categories($args);

foreach ( $categories as $category ) {

if ( $category->parent > 0 ) {
continue;   
}

echo \'<h1 style="font-weight:bold">\' . $category->name . \'</h1>\';

$querystr = "SELECT $wpdb->posts.*
          FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
          WHERE term_id = (" . $category->cat_ID . ")
          AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
          AND ID = object_id
          AND post_type = \'biblioteka\'
          AND post_status = \'publish\'
          ORDER BY post_date DESC";
$posts = $wpdb->get_results($querystr, OBJECT);

echo \'<ul>\';
foreach ( $posts as $post ) {
    setup_postdata($post);  

        echo \'<li>\'; the_title();   echo \'</li>\';

        }
echo \'</ul>\';

$categories2 = get_terms(\'kategoria-pozycji\',array(\'parent\' => $category->term_id , \'hide_empty\'=> \'0\' ));

foreach ( $categories2 as $category ) {

echo \'<h2>\' . $category->name . \'</h2>\';

$posts = get_posts( array( \'kategoria-pozycji\' => $category->name, \'post_type\' => \'biblioteka\' ) );  

echo \'<ul>\';
foreach($posts as $post) { 
    setup_postdata($post);  

        echo \'<li>\'; the_title();   echo \'</li>\';

        }
echo \'</ul>\';

}
}

?>

SO网友:T.Todua

My best solution! (适用于任何分类法,包括“类别”)

$your_taxonomy=\'category\';

function my_Categ_tree($TermName=\'\', $termID, $separator=\'\', $parent_shown=true ){
    $args = \'hierarchical=1&taxonomy=\'.$TermName.\'&hide_empty=0&orderby=id&parent=\';
            if ($parent_shown) {$term=get_term($termID , $TermName); $output=$separator.$term->name.\'(\'.$term->term_id.\')<br/>\'; $parent_shown=false;}
    $separator .= \'-\';  
    $terms = get_terms($TermName, $args . $termID);
    if(count($terms)>0){
        foreach ($terms as $term) {
            //$selected = ($cat->term_id=="22") ? " selected": "";
            //$output .=  \'<option value="\'.$category->term_id.\'" \'.$selected .\'>\'.$separator.$category->cat_name.\'</option>\';
            $output .=  $separator.$term->name.\'(\'.$term->term_id.\')<br/>\';
            $output .=  my_Categ_tree($TermName, $term->term_id, $separator, $parent_shown);
        }
    }
    return $output;
}
Then you can output:

1)目标类别(分类)树,使用特定ID

echo my_Categ_tree($your_taxonomy, 0 );
2)所有类别/分类

foreach (get_terms($your_taxonomy, array(\'hide_empty\'=>0, \'parent\'=>0)) as $each) {
    echo my_Categ_tree($each->taxonomy,$each->term_id);
}

SO网友:OriginalEXE

我玩了一点,这就是我想到的,我进行了测试,它的工作原理与您给出的示例一样:

<ul>
<?php

// get initial categories
$categories = get_categories();

foreach ( $categories as $category ) {

// we don\'t want child categories now, and since get_categories does not support \'depth\' parameter, we use ->parent check
if ( $category->parent > 0 ) {
    continue;   
}

$i = 0;
echo \'<li>\' . $category->cat_name . \'</li>\';

$posts = get_posts(
    array(
        \'category\' => $category->term_id
    )
);

foreach ( $posts as $post ) {

    // let\'s make sure that the post is not also in any of child categories, if it is, skip it ( we don\'t want to display it twice )
    $child_categories = get_term_children( $category->term_id, \'category\' );

    if ( $child_categories && in_category( $child_categories, $post->ID ) ) {
        continue;
    }

    echo 0 === $i ? \'<ul>\' : \'\';
    echo \'<li>\' . $post->post_title . \'</li>\';
    $i++;

}

// now, after we listed all the posts, we query for child categories
$categories2 = get_categories(
    array(
        \'parent\' => $category->term_id
    )
);

foreach ( $categories2 as $category ) {

    $j = 0;
    echo \'<li>\' . $category->cat_name . \'</li>\';
    $posts2 = get_posts(
        array(
            \'category\' => $category->term_id
        )
    );

    foreach ( $posts2 as $post ) {

        echo 0 === $j ? \'<ul>\' : \'\';
        echo \'<li>\' . $post->post_title . \'</li>\';
        $j++;
    }

    echo null === $posts2 ? \'\' : \'</ul>\';

}

echo null === $posts ? \'\' : \'</ul>\';

}

?>
</ul>

SO网友:Por

以下是我解决您遇到的问题的解决方案

            //Define Some require
            define(TAX_TYPE, \'category\'); //you can also replace your custom taxonomy in category
            $terms = get_terms(TAX_TYPE); 
            define(WP_HOME, \'http://yourdomain.com\');


        ?>
        <div class="menu-tours-container">
            <ul id="menu-tours" class="menu">
                <?php 
                    foreach( $terms as $tax_term ) : 

                    $permalink = WP_HOME . \'/\' . TAX_TYPE . \'/\' . $tax_term->slug;

                    // getting posts from each tour type
                    $args=array(
                        \'post_type\'     => \'tour\',
                        \'post_status\'   => \'publish\',
                        \'order_by\'      => \'post_title\',
                        \'order\'         => \'ASC\',
                        \'tour-type\'    => $tax_term->slug
                    );
                    $posts = get_posts($args);

                ?>                        
                    <li>
                        <div class="block-title">
                            <h4><?php echo $tax_term->name; ?></h4>
                        </div>
                        <ul class="check">
                            <?php foreach ($posts as $post) : ?>
                            <li><a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    </li>                 
                <?php endforeach; ?>
            </ul> <!--#menu-tours-->

        </div>

结束

相关推荐

Custom Taxonomy Query

我为我创建的自定义帖子类型设置了两个自定义分类法。post类型称为beer,两种分类法是country和brewers。我想这样列出他们。 Country -->Brewers ----->Beers 我可以使用此代码提取国家。 $terms_country = get_terms(\'country\'); foreach ($terms_country as $term_country) { echo \"<h3 c