Skip to content

Instantly share code, notes, and snippets.

@anegoda1995
Last active January 26, 2017 07:49
Show Gist options
  • Save anegoda1995/6f12bb21bcc69f24377017f608a3998d to your computer and use it in GitHub Desktop.
Save anegoda1995/6f12bb21bcc69f24377017f608a3998d to your computer and use it in GitHub Desktop.
Code for getting posts by WP_Query (can use pagination for like this posts)
<ul>
<div id="container">
<div id="content" role="main">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category_name' => 'news',
'paged' => $paged, 'post_type' => 'post');
$postslist = new WP_Query($args);
if ($postslist->have_posts()) :
//print_r('<pre>');
//var_dump($postslist->posts);
foreach ($postslist->posts as $pa)
{
// print_r($pa);
?>
<li>
<?php
if (get_the_post_thumbnail_url((int) $pa->ID))
{
?>
<div class="photo">
<img src="<?php echo get_the_post_thumbnail_url((int) $pa->ID); ?>" alt="">
</div>
<?php
}
?>
<div class="desc">
<span class="name"><?php echo $pa->post_title; ?></span>
<div class="stat">
<div class="date">
<img src="<?php bloginfo('template_url'); ?>/img/date_green.png" alt="">
<p><?php echo date("d.m.Y", strtotime($pa->post_date)); ?></p>
</div>
<div class="views">
<img src="<?php bloginfo('template_url'); ?>/img/views_green.png" alt="">
<p><?php echo do_shortcode('[post_view id="' . $pa->ID . '"]'); ?></p>
</div>
<div class="comments">
<img src="<?php bloginfo('template_url'); ?>/img/comments_green.png" alt="">
<p><?php echo $pa->comment_count; ?></p>
</div>
</div>
<p class="txt"><?php echo wp_trim_words($pa->post_content, 30); ?></p>
<a href="<?php echo $pa->guid; ?>" class="more">читать далее ></a>
</div>
</li>
<?php
}
wp_reset_postdata();
endif;
/*
* 57 string for pagination, but before you need to add posts_per_page in $args for @P_Query
*/
//wp_pagenavi( array( 'query' => $postslist ) );
?>
</div><!-- #content -->
</div><!-- #container -->
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment