【问题标题】:Wordpress Custom post type list categories & list post of active categoryWordpress 自定义帖子类型列表类别和活动类别列表帖子
【发布时间】:2014-07-10 03:58:35
【问题描述】:

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

<?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

-标题

-TITLE2

-TITLE3

蓝光

光盘

【问题讨论】:

    标签: php conditional-statements wordpress


    【解决方案1】:

    您在注册帖子类型时是否使用了'hierarchical' =&gt; 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
            )
          );
        }
    

    希望对你有帮助

    【讨论】:

    • 是的,我有,但我想要的是在活动列表下的列表中显示所有类别,并在其下显示帖子列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 2011-06-24
    • 1970-01-01
    • 2012-01-01
    • 2015-10-01
    相关资源
    最近更新 更多