Skip to content

Instantly share code, notes, and snippets.

@marushu
Last active December 31, 2015 16:29
Show Gist options
  • Save marushu/8013747 to your computer and use it in GitHub Desktop.
Save marushu/8013747 to your computer and use it in GitHub Desktop.
get_postsでランダム取得で最初の2投稿とその他を分ける
<?php // https://gist.github.com/marushu/8013747 ← my gists
$args = array(
'post_type' => get_post_type(),
'numberposts' => -1,
'orderby' => 'rand'
);
$no_overlap_posts = get_posts( $args );
setup_postdata( $post );
//$test_array[] = $no_overlap_posts;
$top_two_posts = array_slice( $no_overlap_posts, 0, 2 );
foreach( $top_two_posts as $post ) :
?>
<p><?php echo get_the_ID(); ?></p>
<?php endforeach; ?>
<?php
$exclude_array_top_two = array_splice($no_overlap_posts, 2);
foreach( $exclude_array_top_two as $post ) :
?>
<p><?php echo get_the_ID(); ?></p>
<?php endforeach; wp_reset_postdata(); ?>
@marushu
Copy link
Author

marushu commented Dec 17, 2013

archive.phpで使おうと思ってます (^^)

@marushu
Copy link
Author

marushu commented Dec 17, 2013

先頭2つをarray_sliceでforeachで出してみた。取りあえずこれで出てはきたけど…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment