Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active February 25, 2017 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save s-hiroshi/3088098 to your computer and use it in GitHub Desktop.
Save s-hiroshi/3088098 to your computer and use it in GitHub Desktop.
Wordpress - スニペット - foreach文を使用した基本ループのサンプルです。
/**
* foreachを使ったサンプル
*
* reffer
* get_posts
* http://codex.wordpress.org/Template_Tags/get_posts
*/
<?php
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '', // category_name is category slug
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$lastposts = get_posts($args);
?>
<?php foreach($lastposts as $post) : setup_postdata($post); ?>
<p><?php the_time("Y/m/d"); ?>&emsp;<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment