WordPress问题与棒帖


WordPress issue with stick posts

我正在使用粘性帖子来允许将某些帖子固定在精选帖子区域中。我让它在开发服务器上工作,但是当我将其移动到实时服务器时,它只能部分工作。如果有粘性帖子,它会显示它。但是,如果没有粘性帖子,则不会显示任何内容,并且应该显示最新的帖子。有没有可能有效的替代方法来处理这个问题?

$options = array(
    'post_type' => post,
    'posts_per_page' => 1,
    'post__in'  => get_option( 'sticky_posts' ),
    'ignore_sticky_posts' => 1,
    'status' => 'publish'
);

您可以先检查get_option( 'sticky_posts' )的结果:

$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
   // query options for sticky posts
   $options = array(
       'post_type' => post,
       'posts_per_page' => 1,
       'post__in'  => $sticky,
       'ignore_sticky_posts' => 1,
       'status' => 'publish'
   );
} else {
    // query options for the most recent post
    $options = array(
        'posts_per_page' => 1,
        'paged' => 1,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_status' => 'publish'
    );
}