2013-07-07 9 views
0

мне нужно размещать размещать ссылки по категориям, например:Отображение списка сообщений категории?

Категория:

Категория Описание: Lorem Ipsum

  • сообщение ссылка 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:

Вот как я зарегистрировал мой CPT и налог-х:

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

Используйте '$ арг = массив ( 'типа' => 'курсы', 'таксономии' => 'study_type', 'OrderBy' => 'имя', 'порядка' => 'ASC' ) ; он должен работать, отвечать обновлено –

+0

Это отображало название и описание категории, но не ссылки :(Я отредактировал свой оригинальный вопрос с моими функциями для регистрации cpt и tax. Опять же, я очень ценю вашу помощь. – 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

Спасибо за вашу помощь. Где бы вы порекомендовали, что я помещаю это, и мне нужно откликнуться на HTML после foreach? Кроме того, могу ли я использовать CPT и таксономию, поэтому могу ли я разместить это в этом массиве. Извините, но я действительно потерян. Просто, когда я думаю, что у меня есть решение этого, похоже, мимолетно. – DevUps

Смежные вопросы