【问题标题】:Display posts from all custom post type taxanomies not regular categories显示来自所有自定义帖子类型分类法而非常规类别的帖子
【发布时间】:2015-04-09 16:52:51
【问题描述】:

我设置了自定义帖子类型(培训),它具有分类法(公司)。我已将一篇帖子分配给我设置的名为 highwaysmedia 的示例分类。

以下代码可在我的 archive-training.php 页面上找到,该页面用于此 CPT。它显示的是常规帖子类型的类别,每个类别都有 1 个帖子。我需要它来显示自定义帖子类型“training”的每个类别/分类中的一篇帖子。

<?php
//for each category, show all posts
$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
  'post_type' => 'training'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' => 1,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

这是我设置的页面:http://www.highwaysindustry.com/training/

我正在使用 TYPES 插件来创建我的自定义帖子类型。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以像这样在分类法上查询自定义帖子类型:

    $args = array(
        'post_type' => 'training',
        'posts_per_page' => 1,
        'orderby' => 'name',
        'order' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'companies',
                'field' => 'slug',
                'terms' => 'highwaysmedia'
            )
        )
    );
    $query = new WP_Query( $args );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      相关资源
      最近更新 更多