WordPress.org

Ready to get started?Download WordPress

Forums

Sampression Lite
index.php - show posts of specific cats and only most recent (4 posts)

  1. karenshotz
    Member
    Posted 1 year ago #

    Hello,
    Thank you, your theme is great. I would like to show posts of certain cats and the most recent on the index page. I have tried various codes, the one I am working with is :

    <?php	$recent = new WP_Query();  $recent->query('cat= 13,237,1,190,455'); ?>
    <?php while($recent->have_posts()) : $recent->the_post(); ?><a href="<?php the_permalink(); ?>">

    Not sure where to put this on loop.php. What happens is I keep getting the most recent posts from all cats.
    Thank you
    K

  2. ceh
    Member
    Posted 1 year ago #

    I'm not sure if this it what you need, but instead of the loop.php try putting your code in index.php at this code

    // Exclude Sticky Posts and show remaining normal posts
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts( array(
    		'post__not_in' => get_option( 'sticky_posts' ),
    		'paged' => $paged
    		) );
    
    		while (have_posts()) : the_post();
    		get_template_part( 'loop', 'index' );
    
    		endwhile;
    
    	?>

    I changed that code to this, and it loads the page only showing posts with tag_id of 202 and 82...

    // Exclude Sticky Posts and show remaining normal posts
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts( array( 'tag__in' => array( 202, 82 ),
    						'posts_per_page' => -1
    						) );
    
    		while (have_posts()) : the_post();
    		get_template_part( 'loop', 'index' );
    
    		endwhile;
    
    	?>
  3. karenshotz
    Member
    Posted 1 year ago #

    Hi ceh,
    Thank you for sending me that. I tried it, what happens is I am getting the five latest posts, so if one cat has 2 posts that are new, the page shows those and the next 3 recent regardless of category...I think the loop is working by recent posts of all the cats I choose in the loop. What my goal is : to show posts from 5 different cats the most recent post from each cat.
    here is the code, basically, just like yours but using cat instead of tags

    // Exclude Sticky Posts and show remaining normal posts
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts( array( 'cat__in' => array( 13,237,1,190,455),
    						'posts_per_page' => 5
    						) );
    
    		while (have_posts()) : the_post();
    		get_template_part( 'loop', 'index' );
    
    		endwhile;
    
    	?>

    if cat 13 has 2 posts that are more recent than cat 237, it will exclude 237 and show the two posts from cat 13.
    I appreciate your response to this.
    Thank you,
    K

  4. MiKapa
    Member
    Posted 1 year ago #

    It's been a month from the last post here so i hope you've already found what you wanted, but in any case, here's what works from me (live demo @ http://www.blogmag.gr)
    Code:

    <div id="main-content" class="group">
                <?php
    			$cat_args=array(
                				'include' => '3,4,5,6,7,8', //for these categories, show posts--> , 'orderby' => 'name','order' => 'ASC'
                				'orderby' => 'id'
    							);
                $categories=get_categories($cat_args);
                foreach($categories as $category) {
                	$args=array(
    							'showposts' => 4,
    							'category__in' => array($category->term_id)
    							);
                    echo '<div class="front_category_box group">';
                    $posts=get_posts($args);
    					if ($posts) {
    						$count = 0;
    						while (have_posts()) : the_post();
    							$count++;
    							if ($count <= 1) :
    							?>
                                    <h2><?php the_category(', ') ?></h2>
                                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                                    <p class="front_exerpt"><?php $excerpt = strip_tags(get_the_excerpt()); echo $excerpt; ?> </p>
                                    <p class="front_read_more"><a href="<?php the_permalink(); ?>">Διαβάστε Περισσότερα</a></p>
                                    <p class="front_view_comments"><?php comments_popup_link('0 σχόλια »', '1 Σχόλιο »', '% Σχόλια »'); ?></p>
                                    <h4>Τελευταία - <?php the_category(', ') ?></h4>
                                    <ul class="front_latest_news">
    							<?php
                                else :
    							?>
                                        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> <?php the_title(); ?></a></li>
                                <?php
    							endif;
                            endwhile;
    					} // if ($posts)
    					else {
    
    				} //else // if ($posts)
    				echo "</ul></div>";
    			} // foreach($categories
                ?>
    		</div><!-- END of main-content -->

    The count check is for showing the first post of each category including the thumbnail and the rest three without one. Hope it helps somebody ;)

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic