Вывод ссылок постов под категорией в сайдбаре

Доброго времени суток…

Интересует как можно реализовать такое:

>Категория 1
>пост 1 в категории 1
>пост 2 в категории 1
>пост 3 в категории 1
>Категория 2
>Категория 3
>Категория 4

И как то чтобы при выборе другой категории разварачивались посты активной.

Все уже реализовал сам, созданием модифицированной функции wp_list_categories
Кому надо вот код:

<?php
class Walker_Category_Posts extends Walker {
    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this

    function start_lvl(&$output, $depth, $args) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent<ul class='children'>\n";
    }

    function end_lvl(&$output, $depth, $args) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }

    function start_el(&$output, $category, $depth, $args) {
        extract($args);

        $cat_name = attribute_escape( $category->name);
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $link = '<a class="cat-link" href="' . get_category_link( $category->term_id ) . '" ';
        if ( $use_desc_for_title == 0 || empty($category->description) )
            $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
        else
            $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->description, $category )) . '"';
        $link .= '>';
        $link .= $cat_name . '</a>';

        if ( (! empty($feed_image)) || (! empty($feed)) ) {
            $link .= ' ';

            if ( empty($feed_image) )
                $link .= '(';

            $link .= '<a href="' . get_category_feed_link($category->term_id, $feed_type) . '"';

            if ( empty($feed) )
                $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
            else {
                $title = ' title="' . $feed . '"';
                $alt = ' alt="' . $feed . '"';
                $name = $feed;
                $link .= $title;
            }

            $link .= '>';

            if ( empty($feed_image) )
                $link .= $name;
            else
                $link .= "<img src='$feed_image'$alt$title" . ' />';
            $link .= '</a>';
            if ( empty($feed_image) )
                $link .= ')';
        }

        if ( isset($show_count) && $show_count )
            $link .= ' (' . intval($category->count) . ')';

        if ( isset($show_date) && $show_date ) {
            $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
        }

        if ( isset($current_category) && $current_category )
            $_current_category = get_category( $current_category );

        if ( 'list' == $args['style'] ) {
            $output .= "\t<li";
            $class = 'cat-item cat-item-'.$category->term_id;
            if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
                $class .=  ' current-cat';
            elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
                $class .=  ' current-cat-parent';
            $output .=  ' class="'.$class.'"';
            $output .= ">$link\n";
        } else {
            $output .= "\t$link<br />\n";
        }
        $posts = get_posts('numberposts=10&order=DESC&orderby=post_date&category='.$category->term_id);
        if(count($posts)){
            $output .= '<ul class="post-list">';
         foreach ($posts as $post) {
                 $output .= '<li><a id="post-item-'.$post->ID.'" href="'.get_permalink($post->ID).'" rel="bookmark">'.get_the_title($post->ID).'</a></li>';
         }
             $output .= '</ul>';
         }
        //$output .= $category->term_id;    //list_posts();

    }

    function end_el(&$output, $page, $depth, $args) {
        if ( 'list' != $args['style'] )
            return;

        $output .= "</li>\n";
    }

}
function walk_category_posts_tree() {
    $walker = new Walker_Category_Posts;
    $args = func_get_args();
    return call_user_func_array(array(&$walker, 'walk'), $args);
}

function wp_list_categories_posts($args = '') {
    $defaults = array(
        'show_option_all' => '', 'orderby' => 'name',
        'order' => 'ASC', 'show_last_update' => 0,
        'style' => 'list', 'show_count' => 0,
        'hide_empty' => 1, 'use_desc_for_title' => 1,
        'child_of' => 0, 'feed' => '', 'feed_type' => '',
        'feed_image' => '', 'exclude' => '', 'current_category' => 0,
        'hierarchical' => true, 'title_li' => __('Categories'),
        'echo' => 1, 'depth' => 0
    );

    $r = wp_parse_args( $args, $defaults );

    if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
        $r['pad_counts'] = true;
    }

    if ( isset( $r['show_date'] ) ) {
        $r['include_last_update_time'] = $r['show_date'];
    }

    extract( $r );

    $categories = get_categories($r);

    $output = '';
    if ( $title_li && 'list' == $style )
            $output = '<li class="categories">' . $r['title_li'] . '<ul>';

    if ( empty($categories) ) {
        if ( 'list' == $style )
            $output .= '<li>' . __("No categories") . '</li>';
        else
            $output .= __("No categories");
    } else {
        global $wp_query;

        if( !empty($show_option_all) )
            if ('list' == $style )
                $output .= '<li><a href="' .  get_bloginfo('url')  . '">' . $show_option_all . '</a></li>';
            else
                $output .= '<a href="' .  get_bloginfo('url')  . '">' . $show_option_all . '</a>';

        if ( empty( $r['current_category'] ) && is_category() )
            $r['current_category'] = $wp_query->get_queried_object_id();

        if ( $hierarchical )
            $depth = $r['depth'];
        else
            $depth = -1; // Flat.

        $output .= walk_category_posts_tree($categories, $depth, $r);
    }

    if ( $title_li && 'list' == $style )
        $output .= '</ul></li>';

    $output = apply_filters('wp_list_categories', $output);

    if ( $echo )
        echo $output;
    else
        return $output;
}?>

Юзать wp_list_categories_posts аналогично wp_list_categories

Упс, а я вспомнила плагин: http://www.raproject.com/wordpress/categories-and-posts/

Самое забавное что там тоже самое что сделал я 😀

Юзаю этот плагин, подскажите, как исключить одну из рубрик для отображения? (или, что вообще шикарно, возможность задать свой порядок отображения рубрик)

Юзать wp_list_categories_posts аналогично wp_list_categories

http://codex.wordpress.org/Template_Tags/wp_list_categories
параметр смотрите exclude

Юзать wp_list_categories_posts аналогично wp_list_categories

Подскажите плиззз куда вставить этот код???? как плагин он у меня почему то не видится (((((

[quote=fear86]Юзать wp_list_categories_posts аналогично wp_list_categories

Подскажите плиззз куда вставить этот код???? как плагин он у меня почему то не видится ((((([/quote]
куда вставить разобрался, только почему то выводит теперь просто все рубрики и все записи

то есть записи выводит и в активных рубриках и не в активных
так же кстати как и плагин который посоветовала sonika

может я что-то не так делаю ??? (((((((((

Anonymous
Отправить
Ответ на: