Last active
December 31, 2015 16:29
-
-
Save marushu/8013747 to your computer and use it in GitHub Desktop.
get_postsでランダム取得で最初の2投稿とその他を分ける
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); ?> |
先頭2つをarray_sliceでforeachで出してみた。取りあえずこれで出てはきたけど…
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
archive.phpで使おうと思ってます (^^)