简体   繁体   English

WordPress自定义帖子类型列表类别和活动类别列表列表

[英]Wordpress Custom post type list categories & list post of active category

I can list all my custom post type categories and there posts however I only want to list the posts of the current category. 我可以列出所有自定义帖子类型类别,并且在那里列出帖子,但是我只想列出当前类别的帖子。

<?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();
} 
} 

?>

This will list all of the categories in my custom post type like I wanted however displays all posts and not just those related to the current category. 这将列出我想要的自定义帖子类型中的所有类别,但是显示所有帖子,而不仅仅是与当前类别相关的帖子。

Below is some code that I found of the web which will display the post of the active category. 以下是我在网上找到的一些代码,这些代码将显示活动类别的帖子。 I have tried to combine the two but my lack of php and conditional loops mean I can't seem to get it to work. 我试图将两者结合起来,但是由于缺少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(); ?>

Hope someone can help me and either combine the two pieces of code or tell me a new option. 希望有人可以帮助我,或者将这两段代码结合起来,或者告诉我一个新的选择。

For example if I was on a page showing DVD results I want a sidebar displaying other categories and below the DVD heading in the sidebar display the titles (posts) within the DVD category. 例如,如果我在显示DVD结果的页面上,我想要一个侧栏显示其他类别,并且在侧栏的DVD标题下方显示DVD类别内的标题(帖子)。

DVD DVD

-TITLE -标题

-TITLE2 -TITLE2

-TITLE3 -TITLE3

BLU-RAY 蓝光

CD 光盘

did you do 'hierarchical' => true , when registering your post type? 注册帖子类型时,您是否执行过'hierarchical' => true

It should look something similar to this on your functions.php 它应该在您的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
        )
      );
    }

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM