douwen1901 2012-05-08 21:04
浏览 48
已采纳

WordPress - 按自定义帖子类型和类别获取帖子,每个类别自动包含在DIV中

Sorry for the long question title. I was attempting to be accurate.

I need to devise a WordPress query that will automatically get posts from a certain custom post type, detect how each post is categorized, and then output the posts, by category onto a page, with each category encased in its own DIV.

For example, I have a custom post type called "Map Data". Within this custom post type, I have a heirarchial taxonomy that I have named "Categories", and within that taxonomy, a number of categories, "Category #1", "Category #2", so on and so forth. Each Category has a number of posts.

So, the query should get a list of all of the categories within the custom post type, and then output something like this:

<div id="category-1">
     <div class="post">This is a post in Category 1</div>
     <div class="post">This is another post in Category 1</div>
</div>
<div id="category-2">
     <div class="post">This is a post in Category 1</div>
     <div class="post">This is another post in Category 1</div>
</div>

I have the following code which works with the default WordPress Category system, however, I either need to re-write it, or update it so that it can work with custom post types and their taxonomies.

<?php 
    $cat_args=array();
    $categories=get_categories($cat_args);
    foreach($categories as $category) {
        $args=array(
            'category__in' => array($category->term_id),
        );
    $posts=get_posts($args);
        if ($posts) {
            echo '<div class="cat" id="' . $category->slug.'" name="' . $category->name.'">';
            foreach($posts as $post) {
          setup_postdata($post); 
?>

<?php the_title();?>
<?php the_content();?>

<?php
        } // foreach($posts
        echo '</div>';
      } // if ($posts
    } // foreach($categories
?>

If anyone could provide updated code for me to try, or a working example, it would be very much appreciated.

  • 写回答

1条回答 默认 最新

  • dpw5865 2012-05-09 13:41
    关注

    i did this it gets all taxonomies but it could easily be modified to active what your trying

    // for a given post type, return all
    $post_type = 'shows';
    $tax = 'show-topic';
    $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '135, 49, 25, 24, 54'));
    if ($tax_terms) {
        foreach ($tax_terms as $tax_term) {
            $args = array(
                'post_type' => $post_type,
                "$tax" => $tax_term->slug,
                'post_status' => 'publish',
                'posts_per_page' => - 1,
                'orderby' => 'title',
                'order' => 'ASC',
                'caller_get_posts' => 1
                ); // END $args
            $my_query = null;
            $my_query = new WP_Query($args);
            if ($my_query->have_posts()) {
                echo '<h3>' . $tax_term->name . '</h3>';
                while ($my_query->have_posts()) : $my_query->the_post();
                ?>
              <div class="post row" id="post-<?php the_ID(); ?>">
                        <div class="thumb-box three column">
                            <?php
                $src = wp_get_attachment_image_src(get_post_thumbnail_id());
                if (has_post_thumbnail()) {
                    the_post_thumbnail();
                } else {
                    if (get_post_meta($post->ID, "thumbnail", true)):
                        ?>
                                    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
                                <?php else: ?>
                                    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/insp-tv-small.png" alt="<?php the_title(); ?>" /></a>
                                <?php endif;
                }
                ?>
                        </div>
                        <div class="post-content nine columns">
                            <h4 class="posttitle archiveposttitle">
                                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'buddypress') ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                            </h4>
                            <div class="entry">
                                <?php the_excerpt(); ?>
                            </div>
                        </div>
                    </div>
              <?php
                endwhile;
            } // END if have_posts loop
            wp_reset_query();
        } // END foreach $tax_terms
    } // END if $tax_terms
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?