$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Staggering featured post using 'sticky'|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Staggering featured post using 'sticky'

matteradmin9PV0评论

So I have Custom post type that occasionally will have posts marked as 'featured' Client would like to use the 'sticky' feature of WordPress to pick which posts from the CPT are classed as featured and would like the layout of the page to show featured (a) and normal posts (b) in a different fashion.

I would like to work out the best way of creating a loop that can stagger the two sets of posts in a uniform way, that can behave with pagination if possible.

I have boiled it down to (perhaps rightly or wrongly):

  1. Created two arrays of posts - one for just stickies, one for just normal posts
  2. Somehow merge the two arrays into one in a set pattern:

a1, a2, b1, b2, a3, b3, b4, a4, b5, b6 etc etc.

<?php
$sticky = get_option( 'sticky_posts' );

$args_ordinary = array(
    'post__not_in' => $sticky,
    'post_type' => 'research-articles',
    'posts_per_page' => 10,
    'order' => 'ASC',
);

$args_sticky = array(
    'post__in'  => $sticky,
    'posts_per_page' => 3,
    'order' => 'ASC',
    'post_type' => 'research-articles',
);
$ordinaryQuery = new WP_Query($args_ordinary);
$ordinaryPosts = $ordinaryQuery->posts;
wp_reset_postdata();

$stickyQuery = new WP_Query($args_sticky);
$stickyPosts = $stickyQuery->posts;
wp_reset_postdata();

$result = $stickyPosts + $ordinaryPosts;
$result = array_replace(array_fill_keys(array(0,1,3,4,2,5,6,7), null), $result);
foreach ($result as $post){ ?>
    <p><?php echo(is_sticky() ? '(sticky) ' : '') ?><?php echo get_the_title() ?></p>
<?php }
?>

My question is really, is the sticky functionality the best way to go about this? and what suggestions for achieving this kind of loop would you guys have (either by using a merged array or perhaps something better)?

I would prefer not to use any plugins to achieve this, as the client has requested not to.

Any feedback is greatly appreciated!

Post a comment

comment list (0)

  1. No comments so far