WordPress.org

Ready to get started?Download WordPress

Forums

Adding a carousel to the category template (4 posts)

  1. mty42
    Member
    Posted 1 year ago #

    HI

    I would like a carousel to show the first 6 posts from each category depending on what category page you are on. Then the rest of the posts (7 -> infininity) displayed in a more standard format underneath and paginated after around 20.

    Does this make sense? I have tried to ask this before (and got no answers) and tirelessly searched for answers but I cannot find out how to do this...

    Any takers? I'd be very grateful!

    Matt

  2. ubaidullahbutt
    Member
    Posted 1 year ago #

    Yes it is possible,

    But category page will need 2 post queries for that according to my stupid mind :).

    First query will grab first 6 posts via posts_per_page = 6.

    Second one will grab posts greater than 7.

    For more information look at this http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination.

  3. mty42
    Member
    Posted 1 year ago #

    Is this not possible then? It must be. Please help WP boffins!

  4. mty42
    Member
    Posted 1 year ago #

    Hi,

    Thank you for replying, yes that is what I want, but even with that link I'm not sure how to achieve it. I am not much good with PHP, copy, paste and hope for the best is not working this time...

    I need a query that asks:

    If a user is on category A then the page grabs the first 6 posts from A then lists the rest (7 - Nth) underneath, stoping after say 20 and paging the rest.

    If they land on category B I want the same to happen, so one loop for all categories.

    This is my very very poor code:

    <h2 class="page-title"><?php single_cat_title(); ?></h2>
    
    // If less than page 2
    <?php if ( $paged < 2 ) : ?>
    
    // Show this Carousel
    	<?php
    		if (is_category()) {
    		global $wp_query;
    		$cat_obj = $wp_query->get_queried_object();
    		echo $cat_obj->name . " has id ". $cat_obj->term_id;
    		}
    	?>
    
    	<div class="flexslider">
    	<ul class="slides">
    	<?php
        $custom_query = new WP_Query(array(
    // Somehow get the current category ID... this does not work...
        	'category' => $cat_obj,
            'post_status' => 'publish',
            'post_type' => 'post',
            'order' => 'ASC',
            'orderby' => 'none',
            'posts_per_page' => '6',
            'offset' => '1',
            'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
        ));
        while ($custom_query->have_posts()) : $my_query->the_post(); $do_not_duplicate[] = $post->ID;
        ?>
    	<li class="carousel-article">
    			<a href="<?php the_permalink() ?>" rel="bookmark">
    			<article>
    			<?php the_post_thumbnail(); ?>
    			<div>
    				<h3><?php the_title(); ?></h3>
    					<?php the_excerpt(); ?>
    					<?php the_category(); ?>
    			</div>
    			</article>
    			</a>
    	</li>
    	<?php endwhile;?>
    	</ul>
    	</div>
    
    // Then display the rest of the results, this does not work well either
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<?php the_post_thumbnail(); ?>
    		<div>
    			<h3><?php the_title(); ?></h3>
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-content -->
    	</article><!-- #post -->
    
    	<?php else : ?>
    // on page two and beyond show even more of the results
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<?php the_post_thumbnail(); ?>
    		<div>
    			<h3><?php the_title(); ?></h3>
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-content -->
    	</article><!-- #post -->
    
    	<?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic