• First i should say, i am very new to PHP. Just searching,learning and applying to modify theme.

    I have created child theme and made a page template which will show the post thumbnail according to category. I want to display 2 post per category, where a specific post will be shown at top and latest one will be shown below that post. Please check this layout image to get my idea – https://dl.dropboxusercontent.com/u/108458626/Category%20sample1%20for%20code.png

    My first idea was to query sticky post. But i learned, sticky post doesn’t work with category.

    Second idea was to query specific tag. The code is here – http://pastebin.com/jhZBBwPy
    By this way i get only specific post. I want to get another latest post below this but don’t know how. I searched around but didn’t get exact solution or i don’t know what to search for this problem.

    Is someone can help me with considering my bad English and short knowledge. Thanks in advance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • My first idea was to query sticky post. But i learned, sticky post doesn’t work with category.

    Actually, your first thought was correct. Whilst sticky posts are not treated differently by default on any page other than the main posts page, you can use the sticky option to selectively pull posts onto pages elsewhere using a custom query such as WP_Query().

    Thread Starter LittleBrother

    (@littlebrother)

    Thanks esmi
    But that idea didn’t work. I don’t get sticky post under category than latest post.
    Used this –

    $args = array(
    	'posts_per_page' => 1,
    	'post__in'  => get_option( 'sticky_posts' ),
    	'ignore_sticky_posts' => 1
    );
    $query = new WP_Query( $args );

    Where’s the rest of the Loop to display the returned post?

    Thread Starter LittleBrother

    (@littlebrother)

    <?php
    				if( is_category() ) :
    				$args = array(
    					'posts_per_page' => 1,
    					'post__in'  => get_option( 'sticky_posts' ),
    					'ignore_sticky_posts' => 1
    				);
    
    				$the_query = new WP_Query('$args&showposts=2&cat='.get_query_var('cat'));
    				else : $the_query = new WP_Query('showposts=2&orderby=post_date&order=desc');
    				endif;
    			?>
    
    			<?php
    				if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post()
    			?>

    *updated

    I still can’t see any code to actually display the returned post. You need something like:

    <?php
    if( is_category() ) :
    	$args = array(
    		'posts_per_page' => 1,
    		'post__in'  => get_option( 'sticky_posts' ),
    		'ignore_sticky_posts' => 1
    	);
    else :
    	$args = array(
    		'posts_per_page' => 2,
    		'orderby'  => 'post_date',
    		'order' => 'desc'
    	);
    endif;
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content();?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    Thread Starter LittleBrother

    (@littlebrother)

    oh,yes. I used this.

    <?php
    	if( is_category() ) :
    		$args = array(
    			'posts_per_page' => 1,
    			'post__in'  => get_option( 'sticky_posts' ),
    			'ignore_sticky_posts' => 1
    		);
    	$the_query = new WP_Query('$args&showposts=2&cat='.get_query_var('cat'));
    	else : $the_query = new WP_Query('showposts=1&orderby=post_date&order=desc');
    	endif;
    ?>
    
    <?php
    	if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post()
    ?>
    <div class="film singleBox first tallBox" style="width:100%">
    	<div class="filmInside">
    		<a href="<?php the_permalink(); ?>">
    
    			<?php
    				if( has_post_thumbnail() )
    					{
    					the_post_thumbnail('small-thumb', array( 'class' => 'layout-2-thumb' ) );
    					echo '<div class="struggleTitle midGrey">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
    					echo '<div class="struggleDescription whiteColor">' .'<p>'. get_post(get_post_thumbnail_id())->post_content . '</p>'. '</div>';
    
    					}
    				else{
    					echo '<img src="'.get_template_directory_uri().'/images/no-image.png" class="layout-2-thumb">';
    					}
    			?>
    		</a>
    
    	</div>
    </div>
    
    <?php endwhile; endif; ?>
    <?php wp_reset_postdata(); ?>

    Try comparing your code to mine. Can you see that you are not actually using the &args that you set up in your custom query?

    Thread Starter LittleBrother

    (@littlebrother)

    Thanks. I modified my code according to your suggestion.

    <?php
    	if( is_category() ) :
    		$args = array(
    			'posts_per_page' => 1,
    			'post__in'  => get_option( 'sticky_posts' ),
    			'ignore_sticky_posts' => 1
    		);
    	else :
    		$args = array(
    			'posts_per_page' => 2,
    			'orderby'  => 'post_date',
    			'order' => 'desc'
    		);
    	endif;
    $the_query = new WP_Query($args);
    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <div class="film singleBox first tallBox" style="width:100%">
    	<div class="filmInside">
    		<a href="<?php the_permalink(); ?>">
    
    			<?php
    				if( has_post_thumbnail() )
    					{
    					the_post_thumbnail('small-thumb', array( 'class' => 'layout-2-thumb' ) );
    					echo '<div class="struggleTitle midGrey">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
    					echo '<div class="struggleDescription whiteColor">' .'<p>'. get_post(get_post_thumbnail_id())->post_content . '</p>'. '</div>';
    
    					}
    				else{
    					echo '<img src="'.get_template_directory_uri().'/images/no-image.png" class="layout-2-thumb">';
    					}
    			?>
    		</a>
    
    	</div>
    </div>
    
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    With this i get only latest one sticky post. Same sticky post in every category.

    I wish to get sticky post at top according to category and another post under that sticky video. Just like as my image.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display specific post at top with other post belongs to a category’ is closed to new replies.