Wsparcie » Zaawansowane » Walker_Category_post

  • Użyłem kodu do wyświetlania w formie listy kategorii i postów, niestety ukazuje mi się coś takiego:
    – KATEGORIA 1
    — POD KAT 1
    — POD KAT 2
    —- POST 1
    —- POST 2
    — POST 1
    — POST 2
    – KATEGORIA 2
    — POD KAT 21
    — POD KAT 22
    —- POST 21
    —- POST 22
    — POST 21
    — POST 22

    Dubluje mi zawsze posty z ostatniej na liście podkategorii w nadrzędnej kategorii.
    Powinno być tak:
    – KATEGORIA 1
    — POD KAT 1
    — POD KAT 2
    —- POST 1
    —- POST 2
    – KATEGORIA 2
    — POD KAT 21
    — POD KAT 22
    —- POST 21
    —- POST 22

    Czy ktoś wie co może być przyczyną?

    kod jakiego użyłem:

    <?php
        $args2 = array(
    	'show_option_all'    => '',
    	'orderby'            => 'name',
    	'order'              => 'ASC',
    	'style'              => 'list',
    	'show_count'         => 0,
    	'hide_empty'         => 0,
    	'use_desc_for_title' => 1,
    	'child_of'           => 13,
    	'feed'               => '',
    	'feed_type'          => '',
    	'feed_image'         => '',
    	'exclude'            => '',
    	'exclude_tree'       => '',
    	'include'            => '',
    	'hierarchical'       => 1,
    	'title_li'           => __( 'Categories' ),
    	'show_option_none'   => __( '' ),
    	'number'             => null,
    	'echo'               => 1,
    	'depth'              => 0,
    	'current_category'   => 0,
    	'pad_counts'         => 0,
    	'taxonomy'           => 'category',
    	'walker' => new Walker_Category_Posts()
        );
    
    class Walker_Category_Posts extends Walker_Category {
    
        function start_el(&$output, $category, $depth, $args) {
    
            $this->category = $category;
    
            parent::start_el($output, $category, $depth, $args);
        }
    
        function end_el(&$output, $page, $depth, $args) {
            if ( 'list' != $args['style'] )
                return;
    
            $posts = get_posts( array(
                'cat' => $this->category->term_id,
                'numberposts' => 9,
            ) );
    
            if( !empty( $posts ) ) {
    
                $posts_list = '<ul style="height: 100%;overflow: hidden;">';
    
                foreach( $posts as $post )
                    $posts_list .= '<li><a style="width: 200px; height: 200px; display: block; float: left; text-align: center; position: relative;" href="'. get_permalink( $post->ID ) . '">'.get_the_post_thumbnail( $post->ID, array( 200, 200) ).''.get_the_title( $post->ID ).'</a></li>';
                $posts_list .= '</ul>';
            }
            else {
                $posts_list = '';
            }
    
            $output .= "{$posts_list}";
        }
    
    }
    wp_list_categories( $args2 );
    ?>
Viewing 1 replies (of 1 total)
  • Błąd może polegać na tym, że wpis będąc przypisany do podkategorii będzie również wyświetlany w kategorii nadrzędnej.

Viewing 1 replies (of 1 total)
  • Temat ‘Walker_Category_post’ jest zamknięty na nowe odpowiedzi.