WordPress - 通过自定义帖子类型和类别获取帖子,每个类别自动包含在一个 DIV 中 [英] WordPress - Getting Posts by Custom Post Type and Category, with each category automatically encased in a DIV

查看:17
本文介绍了WordPress - 通过自定义帖子类型和类别获取帖子,每个类别自动包含在一个 DIV 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉问题标题太长.我试图做到准确.

我需要设计一个 WordPress 查询,它会自动从某个自定义帖子类型获取帖子,检测每个帖子的分类方式,然后按类别将帖子输出到页面上,每个类别都包含在自己的 DIV 中.

例如,我有一个名为地图数据"的自定义帖子类型.在这个自定义帖子类型中,我有一个层次分类法,我命名为类别",在该分类法中,有许多类别,类别#1"、类别#2"等等.每个类别都有许多帖子.

因此,查询应该获取自定义帖子类型中所有类别的列表,然后输出如下内容:

<div class="post">这是第 1 类帖子</div><div class="post">这是第 1 类中的另一篇博文</div>

<div id="category-2"><div class="post">这是第 1 类帖子</div><div class="post">这是第 1 类中的另一篇博文</div>

我有以下代码适用于默认的 WordPress 类别系统,但是,我要么需要重新编写它,要么更新它,以便它可以与自定义帖子类型及其分类法一起使用.

数组($category->term_id),);$posts=get_posts($args);如果($帖子){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回声'</div>';}//如果 ($posts}//foreach($categories?>

如果有人可以提供更新的代码供我尝试,或者提供一个工作示例,我将不胜感激.

解决方案

我这样做了,它获得了所有分类法,但可以轻松修改以激活您的尝试

//对于给定的帖子类型,返回所有$post_type = '显示';$tax = 'show-topic';$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '135, 49, 25, 24, 54'));如果($tax_terms){foreach ($tax_terms 作为 $tax_term) {$args = 数组('post_type' =>$post_type,"$tax" =>$tax_term->slug,'post_status' =>'发布','posts_per_page' =>- 1,'orderby' =>'标题','订单' =>'ASC','caller_get_posts' =>1);//结束 $args$my_query = null;$my_query = new WP_Query($args);如果 ($my_query->have_posts()) {回声'<h3>'.$tax_term->name .'</h3>';而 ($my_query->have_posts()) : $my_query->the_post();?><div class="post row" id="post-<?php the_ID(); ?>"><div class="thumb-box 三列"><?php$src = wp_get_attachment_image_src(get_post_thumbnail_id());如果(has_post_thumbnail()){the_post_thumbnail();} 别的 {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 其他:?><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 class="post-content 九列"><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><div class="entry"><?php the_excerpt();?>

<?php终了;}//END if have_posts 循环wp_reset_query();}//结束 foreach $tax_terms}//如果 $tax_terms 结束?>

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.

解决方案

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

?>

这篇关于WordPress - 通过自定义帖子类型和类别获取帖子,每个类别自动包含在一个 DIV 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆