2013-07-07 224 views
0

我需要的地方按類別帖子的鏈接,例如:顯示類別帖子的列表?

類別:

分類描述:Lorem存有

  • 帖子鏈接1
  • 帖子鏈接2
  • 後鏈接3

這裏是我的代碼:

<?php 
$args=array(
    'type' => 'courses', 
    'taxonomy' => 'study_type', 
    'orderby' => 'name', 
    'order' => 'ASC' 
); 
$categories=get_categories($args); 
foreach($categories as $category) { 
    echo '<h3 class="study-tax-title">'. $category->name .'</h3>'; 
    echo '<p>'. $category->description . '</p>'; 
    /*List of links should be here*/ 
} 

我需要把另一個數組並做wpquery或者我可以附和通過get_categories陣列的東西嗎?

UPDATE:

這是我如何註冊我的中華映管和稅務的:

<?php 

    if (! function_exists('custom_course_post_type')) { 

    // Register Custom Post Type 
    function custom_course_post_type() { 
     $labels = array(
     'name'    => _x('Courses', 'Post Type General Name', 'text_domain'), 
     'singular_name'  => _x('Course', 'Post Type Singular Name', 'text_domain'), 
     'menu_name'   => __('Courses', 'text_domain'), 
     'parent_item_colon' => __('Parent Course', 'text_domain'), 
     'all_items'   => __('All Courses', 'text_domain'), 
     'view_item'   => __('View Course', 'text_domain'), 
     'add_new_item'  => __('Add New Course', 'text_domain'), 
     'add_new'    => __('New Course', 'text_domain'), 
     'edit_item'   => __('Edit Course', 'text_domain'), 
     'update_item'   => __('Update Course', 'text_domain'), 
     'search_items'  => __('Search Courses', 'text_domain'), 
     'not_found'   => __('No Courses found', 'text_domain'), 
     'not_found_in_trash' => __('No Courses found in Trash', 'text_domain'), 
    ); 

     $rewrite = array(
     'slug'    => 'courses', 
     'with_front'   => true, 
     'pages'    => true, 
     'feeds'    => true, 
    ); 

     $args = array(
     'label'    => __('courses', 'text_domain'), 
     'description'   => __('Course information pages', 'text_domain'), 
     'labels'    => $labels, 
     'supports'   => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions',), 
     'taxonomies'   => array('vendors', 'level', 'study_type', 'post_tag'), 
     'hierarchical'  => false, 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'show_in_nav_menus' => true, 
     'show_in_admin_bar' => true, 
     'menu_position'  => 5, 
     'can_export'   => true, 
     'has_archive'   => true, 
     'exclude_from_search' => false, 
     'publicly_queryable' => true, 
     'query_var'   => 'course', 
     'rewrite'    => $rewrite, 
     'capability_type'  => 'post', 
    ); 

     register_post_type('courses', $args); 
    } 

    // Hook into the 'init' action 
    add_action('init', 'custom_course_post_type', 0); 

    } 

    ?> 
    <?php register_taxonomy_for_object_type('study_type', 'courses'); ?> 
    <?php register_taxonomy_for_object_type('vendors', 'courses'); ?> 
     <?php register_taxonomy_for_object_type('level', 'courses'); ?> 
    <?php 

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

    function course_taxonomies(){ 
     register_taxonomy(
      'study_type', 
      'courses', 
      array(
      'hierarchical' => true, 
      'label' => 'Area of Study', 
      'query_var' => true, 
      'rewrite' => true 
      ) 
     ); 
     register_taxonomy(
      'vendors', 
      'courses', 
      array(
      'hierarchical' => true, 
      'label' => 'Vendors', 
      'query_var' => true, 
      'rewrite' => true 
      ) 
     ); 
     register_taxonomy(
      'level', 
      'courses', 
      array(
      'hierarchical' => true, 
      'label' => 'Level of Study', 
      'query_var' => true, 
      'rewrite' => true 
      ) 
     ); 
    } 
    ?> 

* *這是我更新的代碼:

<?php 
$args = array(
    'type' => 'courses', 
    'taxonomy' => 'study_type', 
    'orderby' => 'name', 
    'order' => 'ASC' 
); 

$categories = get_categories($args); 
foreach ($categories as $category) { 
    echo '<h3 class="study-tax-title">' . $category->name . '</h3>'; 
    echo '<p>' . $category->description . '</p>'; 
    /* List of links should be here */ 

    $filtered_posts = query_posts('cat=' . $category->cat_ID); 
    if ($filtered_posts) { 
     echo '<ul>'; 
     foreach ($filtered_posts as $post) { 
      echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>'; 
     } 
     echo '</ul>'; 

    } 
} 
?> 

回答

1

你可以如下做到這一點:

$args = array(
    /* 'type' => 'courses', */ 
    /* 'taxonomy' => 'study_type', */ // this only work if you have study_type as taxonomy, default taxonomy is category 
    'orderby' => 'name', 
    'order' => 'ASC' 
); 

$categories = get_categories($args); 
foreach ($categories as $category) { 
    echo '<h3 class="study-tax-title">' . $category->name . '</h3>'; 
    echo '<p>' . $category->description . '</p>'; 
    /* List of links should be here */ 

    $filtered_posts = get_posts(array('numberposts' => 3, 'category' => $category->cat_ID)); 
    if ($filtered_posts) { 
     echo '<ul>'; 
     foreach ($filtered_posts as $post) { 
      echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>'; 
     } 
     echo '</ul>'; 
    } 
} 

*更新的代碼與tax_query

$args = array(
    'type' => 'courses', 
    'taxonomy' => 'study_type', 
    'orderby' => 'name', 
    'order' => 'ASC' 
); 

$categories = get_categories($args); 
foreach ($categories as $category) { 
    echo '<h3 class="study-tax-title">' . $category->name . '</h3>'; 
    echo '<p>' . $category->description . '</p>'; 
    /* List of links should be here */ 

    $filtered_posts = get_posts(
     array(
      'post_type' => 'courses', 
      'tax_query' => array(
       array(
        'taxonomy' => $category->taxonomy, 
        'field' => 'id', 
        'terms' => $category->term_id 
       ) 
      ) 
     ) 
    ); 
    if ($filtered_posts) { 
     echo '<ul>'; 
     foreach ($filtered_posts as $post) { 
      echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>'; 
     } 
     echo '</ul>'; 
    } 
} 
+0

感謝您的幫助!我將它放在我的代碼中,它顯示了我的通用帖子類型類別。你有什麼想法如何使它顯示爲「課程」的CPT和「study_type」的分類?再次感謝您的一切! – DevUps

+0

用你的'的$ args =陣列( '型'=> '課程', '分類'=> 'study_type', '排序依據'=> '名', '秩序'=> 'ASC' ) ;'它應該工作,回答更新 –

+0

這顯示了類別的名稱和說明,但沒有鏈接:(我編輯我的原始問題與我的職能註冊cpt和稅務的我再次感謝您的幫助 – DevUps

1

如果你想顯示的帖子對於一個類別,您必須使用「get_posts」函數(這裏有很多示例:http://codex.wordpress.org/Template_Tags/get_posts

但你可以嘗試添加:

$args = array('posts_per_page' => 3, 'category' => id_number_of_your_category); 
$myposts = get_posts($args); 
echo "<ul>"; 
foreach($myposts as $post) : setup_postdata($post); ?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endforeach; 
echo "</ul>"; 
?> 

希望這就是你要找的人,將幫助您!

+0

感謝您的幫助。你會推薦我把它放在哪裏,我需要在foreach後回顯HTML嗎?另外,我是否可以使用CPT和分類標準,因此可以將其放置在該陣列的某個位置。對不起,具體但我真的迷路了。就在我認爲我有解決方案的時候,它似乎稍縱即逝。 – DevUps