0

I am trying to make category tabs that show posts from selected category tab. I have tried it my way but m getting only one post for each category, whereas it should return all post in that category. I am pasting the code below, can anyone help me sort this issue ?

<ul id="download-tab" class="nav nav-tabs resp-tabs-list">
                        <?php
    $args = array(
      'orderby' => 'name',
      'parent' => 13
      );
    $categories = get_categories( $args );
$i=0;
foreach ( $categories as $category ) {
    $i++;
    echo '<li><a id="#'.strtolower(str_replace(' ', '', $category->name)).'" href="dtab'.$i.'">' . $category->name . '</a></li>';
}
?>
 </ul> 

           <div class="tab-content resp-tabs-container" id="download-content">

<?php
$pargs = array(
  'orderby' => 'name',
  'parent' => 13
  );
$categories_loop = get_categories( $pargs );
foreach ( $categories_loop as $category_loop ) {
query_posts('category_name='.$category_loop->slug.'&posts_per_page=-1&order=desc'); $j=0;
?>
<?php if ( have_posts() ) : ?>
  <?php while ( have_posts() ) : the_post(); 
  $j++ ?>
                        <div class="tab-pane" id="dtab<?php echo $j ?>">
                        <table class="table table-hover">
                        <tr>
                            <th>DATE</th>
                            <th>TITLE</th>
                            <th>SIZE</th>
                            <th></th>
                        </tr>
                    <?php 
    $file_size = rwmb_meta('file_size');
    $file_url = rwmb_meta( 'file_url');
  ?>
                        <tr>
                            <td><?php the_time('d/m/Y'); ?></td>
                            <td><?php the_title(); ?></td>
                            <td><?php echo $file_size; ?></td>
                            <td><a href="<?php echo $file_url ?>" class="button3 download_btn" target="_blank">DOWNLOAD</a></td>
                        </tr>
                        </table>
                        </div>

    <?php 
    endwhile;  ?>
<div style="width:45%; float:left;  font-weight:bold; margin-top:20px;"><?php //previous_posts_link('PREV'); ?></div>
<div style="width:45%; float:right; text-align:right; font-weight:bold; margin-top:20px;"><?php //next_posts_link('NEXT',$download_query->max_num_pages); ?></div>
<?php wp_reset_query();?>
<?php else:  ?>
    <p><?php _e( 'Sorry, no content found.' ); ?></p>
<?php endif; 
} ?>
  • You are sure that you have more than 1 post in each category? Also, query_posts() is frowned upon. Use new WP_Query() instead. And I would also suggest that once you have it all figured out, you use the Transient API to improve performance on all those queries. – helgatheviking Aug 21 '14 at 7:41
  • Thanks for quick reply, yes there are more posts in every category. And i had also tried using new WP_Query but got the same result – nnbhatt Aug 21 '14 at 8:21
  • Ok, just wanted to check the basics first. – helgatheviking Aug 21 '14 at 8:22
  • so what can be done ? any idea ? – nnbhatt Aug 21 '14 at 8:24
0

I'm not sure why you are only getting 1 post returned. Your query args look correct, even if as I mentioned in the comment you should be using WP_Query. Therefore, if I had to guess, I would say your looped html markup is off? And is maybe not printing the results correctly. I've modified your code to only have 1 get_categories() and categories loop. I don't know if it is more or less "readable" this way, but it seems to work for me.

$args = array(
    'orderby' => 'name',
    'parent' => 13
);
$categories = get_categories( $args );

$tabs = '';
$panes = '';

if( $categories ){

    $tabs .= '<ul id="download-tab" class="nav nav-tabs resp-tabs-list">';

    $panes .= '<div class="tab-content resp-tabs-container" id="download-content">';

    $i=0;

    foreach ( $categories as $category ) {
        $i++;
        $tabs .= '<li><a id="#'.$category->slug.'" href="dtab'.$i.'">' . $category->name . '</a></li>';

        $post_args = array( 'category_name' => $category->slug, 
                            'posts_per_page' => -1, 
                            'order' => 'desc' ); 

        $category_posts = new WP_Query( $post_args );

        $panes .= '<div class="tab-pane" id="dtab' .$i . '">';

        if ( $category_posts->have_posts() ) :

            $panes .= '<table class="table table-hover">
                        <tr>
                            <th>DATE</th>
                            <th>TITLE</th>
                            <th>SIZE</th>
                            <th></th>
                        </tr>';

            while ( $category_posts->have_posts() ) : $category_posts->the_post(); 

                $file_size = function_exists( 'rwmb_meta' ) ? rwmb_meta('file_size') : '';
                $file_url = function_exists( 'rwmb_meta' ) ? rwmb_meta( 'file_url') : '';

                $panes .= '<tr>
                            <td>'. get_the_time('d/m/Y') .'</td>
                            <td>'. get_the_title() .'</td>
                            <td>'. $file_size .'</td>
                            <td><a href="'. $file_url .'" class="button3 download_btn" target="_blank">DOWNLOAD</a></td>
                        </tr>';
            endwhile;

            $panes .= '</table>';

        else:
            $panes .= '<p>' . __( 'Sorry, no content found.' ) . '</p>';
        endif;

        $panes .= '</div><!--#dtab' .$i . '-->';

    }

    $panes .= '</div><!--#download-content-->';

    $tabs .= '</ul><!--#download-tab-->';

}

// print it all out
echo $tabs;
echo $panes;
| improve this answer | |
  • Thank you very much, this worked like a charm. Can you please tell me where was i making mistake ? – nnbhatt Aug 21 '14 at 8:35
  • Like I said, I am not 100% sure. It could be improperly closed mark up, though that looks ok to me. It could also be that wp_reset_query() is in the wrong place. – helgatheviking Aug 21 '14 at 8:37
  • May be wp_reset_query() is the culprit. I also want pagination/pager in the tab-pane, if you see my code i have put one. Can you help me place it, it would be wonderful. – nnbhatt Aug 21 '14 at 8:41
  • If you have "posts_per_page"=>-1 then you should be getting all the posts and thus have no need for pagination. Additionally, you are showing content in a tabbed format... where would you be linking to? – helgatheviking Aug 21 '14 at 8:43
  • ya i know it is a bit illogical, is there any way out if i have lot of content in the tab-pane – nnbhatt Aug 21 '14 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.