Ask your WordPress questions! Pay money and get answers fast! (more info)

How to show 12 recent posts out of loop WordPress

  • SOLVED

Hi,

I have been trying to figure this out and have had some previous help, but it did not resolve it. So either I am stupid or my problem is lol.. So I decided to try a simpler direction with my code.

RESULT I AM AFTER:
I want to show 12 recent posts on my homepage, but if they are from my video category, I want to add a watermark to the THUMB. So I am just going to have it pull a diffenent DIV/CSS on that so I can CSS the overlay of the PNG.

CODE
http://pastebin.com/N0iW14dZ

Any Help would be great!

Answers (2)

2013-02-18

Kiet Luong answers:

please try this:

<?php

wp_reset_query();

query_posts('posts_per_page=12');

if( have_posts() ) : while( have_posts() ) :the_post();

if ( in_category('videos') ) { ?>

<div class="story-video">

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } else { ?>

<div class="story">

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } ?>



<?php endif;endwhile;endif; wp_reset_query();?>


Kiet Luong comments:

sorry this:
<?php



wp_reset_query();



query_posts('posts_per_page=12');



if( have_posts() ) : while( have_posts() ) :the_post();



if ( in_category('videos') ) { ?>



<div class="story-video">



<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>



<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>



<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>



</div>



<?php } else { ?>



<div class="story">



<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>



<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>



<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>



</div>



<?php } ?>







<?php endwhile;endif; wp_reset_query();?>


Jay comments:

How do I make it RECENT POSTS - sorry forgot about that


Kiet Luong comments:

You mean, the title ?

insert <h3 class="title">RECENT POSTS</h3>

above codes.


Jay comments:

It actually was showing 13 posts with that code? - I was wrong it was showing recent posts, just a few to many


Jay comments:

It is only marking on with a PNG even thought there are 4 posts from the category. Hope that helps.


Kiet Luong comments:

can I see the link ?


Jay comments:

Here is what I have in there and the CSS is putting the PNG ABSOLUTE in the middle of the thumb. I cannot do a link at this time, sorry. But it should be in this code, the VIDEO OPTION will only mark 1 from VIDEOS even though there are 4 or the 12 that are.

<div id="video-grid">
<?php
wp_reset_query();
query_posts('posts_per_page=12');
if( have_posts() ) : while( have_posts() ) :the_post();
if ( in_category('videos') ) { ?>

<div class="story-video">
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } else { ?>

<div class="story">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } ?>
<?php endwhile;endif; wp_reset_query();?>
<div class="clearer"></div>
</div>


Jay comments:

Ohh and it outs 13 not 12 - so I think the code is telling it 1 from VIDEOS with this DIV and then 12 with the other DIV


Jay comments:

there our 13 and not 12 - I meant to say.. Okay I am in CST and must go to bed, I will check this in the AM. I hope what I said helps :)


Kiet Luong comments:

try this :
<div id="video-grid">

<?php

query_posts( $query_string . '&posts_per_page=12' );

if( have_posts() ) : while( have_posts() ) :the_post();

if ( in_category('videos') ) { ?>



<div class="story-video">

<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } else { ?>



<div class="story">

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } ?>

<?php endwhile;endif; wp_reset_query();?>

<div class="clearer"></div>

</div>


Kiet Luong comments:

Try this code, I've checked it. thanks.
<div id="video-grid">



<?php



query_posts('posts_per_page=12' );



if( have_posts() ) : while( have_posts() ) :the_post();



if ( in_category('videos') ) { ?>







<div class="story-video">



<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>



<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>



<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>



<p><a href="<?php the_permalink(); ?>"><?php echo the_content(); ?> Read More</a></p>



</div>



<?php } else { ?>







<div class="story">



<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>



<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>



<p><a href="<?php the_permalink(); ?>"><?php the_content(); ?> Read More</a></p>



</div>



<?php } ?>



<?php endwhile;endif; wp_reset_query();?>



<div class="clearer"></div>


Kiet Luong comments:

have another loop in this homepage ?
if this code not work, please write me here or skype: kioluong.
thanks !


Jay comments:

Here is the code for the homepage - that last peice of code put like 50 on the same page on top of each other, but the one you pasted before works, but adds 1 extra story, and only puts a icon one the 1st one.


<?php get_header(); ?>
<div class="box-home-video">
<?php if ( function_exists( 'riva_slider_pro' ) ) { riva_slider_pro( 2 ); } ?>

<div class="box-home-ad2">
</div>

<div class="box-home-ad1">
</div>

<div class="clearer"></div>
</div>

<div id="video-grid">
<?php
query_posts( $query_string . '&posts_per_page=12' );
if( have_posts() ) : while( have_posts() ) :the_post();
if ( in_category('videos') ) { ?>

<div class="story-video">
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } else { ?>

<div class="story">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } ?>
<?php endwhile;endif; wp_reset_query();?>
<div class="clearer"></div>
</div>

<div class="main-home" style="margin:0;">
<div class="column">
<div class="full">
<div class="content">
<?php $my_query = new WP_Query("showposts=1&post_type=page&page_id=5");
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="post-home" id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<?php get_footer(); ?>




Kiet Luong comments:

please try this:

<?php get_header(); ?>

<div class="box-home-video">

<?php if ( function_exists( 'riva_slider_pro' ) ) { riva_slider_pro( 2 ); } ?>



<div class="box-home-ad2">

</div>



<div class="box-home-ad1">

</div>



<div class="clearer"></div>

</div>



<div id="video-grid">

<?php

query_posts( 'posts_per_page=12' );

if( have_posts() ) : while( have_posts() ) :the_post();

if ( in_category('videos') ) { ?>



<div class="story-video">

<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } else { ?>



<div class="story">

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } ?>

<?php endwhile;endif; wp_reset_query();?>

<div class="clearer"></div>

</div>



<div class="main-home" style="margin:0;">

<div class="column">

<div class="full">

<div class="content">

<?php $my_query = new WP_Query("showposts=1&post_type=page&page_id=5");

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

$do_not_duplicate = $post->ID; ?>

<div class="post-home" id="post-<?php the_ID(); ?>">

<div class="entry">

<?php the_content(); ?>

</div>

</div>

<?php endwhile; ?>

</div>

</div>

</div>

<?php get_footer(); ?>


Kiet Luong comments:

Ahh try this :

<?php get_header(); ?>

<div class="box-home-video">

<?php if ( function_exists( 'riva_slider_pro' ) ) { riva_slider_pro( 2 ); } ?>



<div class="box-home-ad2">

</div>



<div class="box-home-ad1">

</div>



<div class="clearer"></div>

</div>



<div id="video-grid">

<?php

query_posts( 'posts_per_page=12' );

if( have_posts() ) : while( have_posts() ) :the_post();

if ( in_category('videos') ) { ?>



<div class="story-video">

<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } else { ?>



<div class="story">

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>

</div>

<?php } ?>

<?php endwhile;endif; wp_reset_query();?>

<div class="clearer"></div>

</div>

<?php get_footer(); ?>


the extra is page content.


Jay comments:

Yes it is homepage content for the page pulled from a ID


Kiet Luong comments:

is latest post work ?


Jay comments:

Surprisingly no -

I think the fact it is showing 13 (not 12) and the 1st story is the only one with a ICON on it. - is the answer - not sure what though :)


Kiet Luong comments:

Did you try this code below and checked published posts in wp dashboard also title, content and categories of each post ?

<?php get_header(); ?>



<div class="box-home-video">

<?php if ( function_exists( 'riva_slider_pro' ) ) { riva_slider_pro( 2 ); } ?>

<div class="box-home-ad2">

</div>

<div class="box-home-ad1">

</div>


<div class="clearer"></div>


</div>


<div id="video-grid">



<?php



query_posts( 'posts_per_page=12' );



if( have_posts() ) : while( have_posts() ) :the_post();



if ( in_category('videos') ) { ?>


<div class="story-video">



<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/img/video.png" class="v-icon" title="<?php the_title(); ?>"></a>



<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>



<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>



<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>



</div>



<?php } else { ?>



<div class="story">



<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>



<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>



<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>



</div>



<?php } ?>



<?php endwhile;endif; wp_reset_query();?>


<div class="clearer"></div>


</div>


<?php get_footer(); ?>


Kiet Luong comments:

Did you check riva slider ? I think the extra is a slide of riva slider.


Jay comments:

Yes I did - it is not that. The grid shows 13 and only one has the icon - very strange but I think it is

if ( in_category('videos') ) { ?>



The reason is because this is saying for some reason show only one and then if not show 12 of the other ones. This is why I got stuck too



Kiet Luong comments:

how many posts under "videos" category, and how many post under other ? and how it shown ?


Kiet Luong comments:

I tested in locahost it work fine.


Jay comments:

hmmm - that is weird. Ah ha I think I know, some of the posts are selected for a regular category and then also videos category - that must be it. How can it still be limted to 12 but if video and a nother category is selected still adds the ICON.


Jay comments:

I am essentially tagging the videos with a CATEGORY to organize the videos to all be accessed if a person wants, but they would still show up in general categories.


Kiet Luong comments:

send me screenshots !


Kiet Luong comments:

[email protected]


Jay comments:

EVERYTHING WORK after I adjusted added in that CSS - thanks for you help!

2013-02-18

Arnav Joy answers:

try this

<?php if ( in_category('videos') ) { ?>
<div class="story-video">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } else { ?>
<div class="story">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } ?>


Arnav Joy comments:

Here is the full code

<?php
wp_reset_query();
query_posts('posts_per_page=12');
if( have_posts() ) : while( have_posts() ) :the_post();
if ( in_category('videos') ) { ?>
<div class="story-video">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } else { ?>
<div class="story">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } ?>

<?php endif;endwhile; wp_reset_query();?>



Arnav Joy comments:


<?php
wp_reset_query();
query_posts('posts_per_page=12');
if( have_posts() ) : while( have_posts() ) :the_post();
if ( in_category('videos' , get_the_ID() ) ) { ?>
<div class="story-video">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } else { ?>
<div class="story">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php echo excerpt(12); ?> Read More</a></p>
</div>
<?php } ?>

<?php endif;endwhile; wp_reset_query();?>



also check this refernce

http://codex.wordpress.org/Function_Reference/in_category


Jay comments:

Trying now brb


Jay comments:

got a Parse error: syntax error, unexpected T_ENDIF


Arnav Joy comments:

try this
<?php
wp_reset_query();
query_posts('posts_per_page=12');
if( have_posts() ) : while( have_posts() ) :the_post();
if ( in_category('videos' , get_the_ID() ) ) { ?>
<div class="story-video">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?> Read More</a></p>
</div>
<?php } else { ?>
<div class="story">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?> Read More</a></p>
</div>
<?php } ?>

<?php endwhile;endif; wp_reset_query();?>