【问题标题】:Can't view all posts in a category - Wordpress无法查看某个类别中的所有帖子 - Wordpress
【发布时间】:2016-04-14 02:24:22
【问题描述】:

我在查看某个类别中的所有帖子时遇到问题。

我在functions.php中创建了一个自定义帖子和类别,如下所示:

function create_posttype() {
    register_post_type( 'tours',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Tours' ),
                'singular_name' => __( 'Tour' )
            ),
            'public' => true,
            //'has_archive' => true,
            'rewrite' => array('slug' => 'tours'),
            //'taxonomies' => array('category'), //A added
            'hierarchical' => true //A added
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

function my_taxonomies_tours() {
  $labels = array(
    'name'              => _x( 'Tour Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Tour Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Tour Categories' ),
    'all_items'         => __( 'All Tour Categories' ),
    'parent_item'       => __( 'Parent Tour Category' ),
    'parent_item_colon' => __( 'Parent Tour Category:' ),
    'edit_item'         => __( 'Edit Tour Category' ), 
    'update_item'       => __( 'Update Tour Category' ),
    'add_new_item'      => __( 'Add New Tour Category' ),
    'new_item_name'     => __( 'New Tour Category' ),
    'menu_name'         => __( 'Tour Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'tour_category', 'tours', $args );
}

add_action( 'init', 'my_taxonomies_tours', 0 );

我创建了一个模板来显示所有类别,希望您可以单击一个类别并让它列出所有帖子。在我的模板中,我有以下内容:

$custom_terms = get_terms('tour_category');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'tours',
        'tax_query' => array(
            array(
                'taxonomy' => 'tour_category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

     }
}

我目前列出的类别,但我不知道如何以这种方式链接它,以便您可以查看该特定类别中的所有帖子。任何帮助将不胜感激。 使用 Wordpress 4.4

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试像这样调整您的 $args 变量(添加 posts_per_page):

    $args = array('post_type' => 'tours',
            'tax_query' => array(
                array(
                    'taxonomy' => 'tour_category',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
            'posts_per_page' => -1,
         );
    

    否则它会使用每页的默认帖子值。

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 2017-12-22
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多