Wordpress自定义帖子类型列表类别&列出活动类别的帖子

时间:2014-05-21 13:18:12

标签: php conditional-statements wordpress

我可以列出我的所有自定义帖子类型类别和帖子,但我只想列出当前类别的帖子。

<?php

$post_type = 'product';
$tax = 'product_category';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
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
        ); 
    $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();
        ?>
      <p><i class="fa fa-angle-right" style="margin-right:5px;"></i><?php the_title(); ?>       </p>

      <?php
        endwhile;
    } 
    wp_reset_query();
} 
} 

?>

这将列出我想要的自定义帖子类型中的所有类别,但是会显示所有帖子,而不仅仅是与当前类别相关的帖子。

下面是我在网上找到的一些代码,它会显示活动类别的帖子。我试图结合这两个,但我缺乏PHP和条件循环意味着我似乎无法让它工作。

<?php

$tax = get_query_var('tax'); // get current category
$yourcat = get_category($cat);

// now get all 'business' posts that are in the current custom taxonomy 
query_posts( array( 'post_type' => 'product', 'product_category' => $yourcat->slug ) ); 

if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<h3><?php the_title(); ?></h3>
<?php endwhile; endif; wp_reset_query(); ?>

希望有人可以帮助我,或者将两段代码结合起来或者告诉我一个新选项。

例如,如果我在显示DVD结果的页面上,我想要一个显示其他类别的侧边栏,并且侧栏中的DVD标题下方显示DVD类别中的标题(帖子)。

DVD

-title

-TITLE2

-TITLE3

BLU-RAY

CD

1 个答案:

答案 0 :(得分:0)

在注册帖子类型时你做过'hierarchical' => true吗?

它应该在你的functions.php上看起来与此类似。

// Custom Post_type Products
    add_action( 'init', 'create_products' );
    function create_products() {
         $labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Product', 'post type singular name'),
        'add_new' => _x('Add New', 'Product'),
        'add_new_item' => __('Add new product'),
        'edit_item' => __('Edit product'),
        'new_item' => __('New product'),
        'view_item' => __('View product'),
        'search_items' => __('Search product'),
        'not_found' =>  __('No products found'),
        'not_found_in_trash' => __('No products found in Trash'),
        'parent_item_colon' => ''
      );

      $supports = array('title', 'editor', 'revisions', 'thumbnail', 'page-attributes' );

      register_post_type( 'Products',
        array(
          'labels' => $labels,
          'public' => true,
          'supports' => $supports,
          'hierarchical' => true,
          'menu_position' => 6, // places menu item directly below Posts
          'menu_icon' => 'dashicons-cart',
          'has_archive' => true
        )
      );
    }

我希望这会有所帮助

相关问题