我已经为我的Wordpress特色文章编写了一个简洁的响应滑块(请随意使用):
<?php
$responsive = \'on\' != get_option(\'henrik_responsive_layout\') ? false : true;
$featured_auto_class = \'\';
if ( \'on\' == get_option(\'henrik_slider_auto\') ) $featured_auto_class .= \' et_slider_auto et_slider_speed_\' . get_option(\'henrik_slider_autospeed\');
?>
<div id="featured" class="<?php if ( $responsive ) echo \'flexslider\' . $featured_auto_class; else echo \'et_cycle\'; ?>">
<a id="left-arrow" href="#"><?php esc_html_e(\'Previous\',\'henrik\'); ?></a>
<a id="right-arrow" href="#"><?php esc_html_e(\'Next\',\'henrik\'); ?></a>
<?php if ( $responsive ) { ?>
<ul class="slides">
<?php } else { ?>
<div id="slides">
<?php } ?>
<?php global $ids;
$ids = array();
$arr = array();
$i=0;
$featured_cat = get_option(\'henrik_feat_cat\');
$featured_num = (int) get_option(\'henrik_featured_num\');
if (get_option(\'henrik_use_pages\') == \'false\') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
else {
global $pages_number;
if (get_option(\'henrik_feat_pages\') <> \'\') $featured_num = count(get_option(\'henrik_feat_pages\'));
else $featured_num = $pages_number;
query_posts(array
(\'post_type\' => \'page\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'post__in\' => (array) get_option(\'henrik_feat_pages\'),
\'showposts\' => (int) $featured_num
));
} ?>
<?php if (have_posts()) : while (have_posts()) : the_post();
global $post; ?>
<?php if ( $responsive ) { ?>
<li class="slide">
<?php } else { ?>
<div class="slide">
<?php } ?>
<?php
$width = $responsive ? 960 : 958;
$height = 340;
$small_width = 95;
$small_height = 54;
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,\'\',$titletext,$titletext,false,\'Featured\');
$arr[$i][\'thumbnail\'] = get_thumbnail($small_width,$small_height,\'\',$titletext,$titletext,false,\'Small\');
$arr[$i][\'titletext\'] = $titletext;
$thumb = $thumbnail["thumb"];
print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, \'\'); ?>
<div class="featured-top-shadow"></div>
<div class="featured-bottom-shadow"></div>
<div class="featured-description">
<div class="feat_desc">
<p class="meta-info"><?php esc_html_e(\'Posted\',\'henrik\'); ?> <?php esc_html_e(\'by\',\'henrik\'); ?> <?php the_author_posts_link(); ?> <?php esc_html_e(\'on\',\'henrik\'); ?> <?php the_time(esc_attr(get_option(\'henrik_date_format\'))) ?></p>
<h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php truncate_post(410); ?></p>
</div>
<a href="<?php the_permalink(); ?>" class="readmore"><?php esc_html_e(\'Read More\', \'henrik\'); ?></a>
</div> <!-- end .description -->
<?php if ( $responsive ) { ?>
</li> <!-- end .slide -->
<?php } else { ?>
</div> <!-- end .slide -->
<?php } ?>
<?php $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
</ul> <!-- end .slides -->
<?php } else { ?>
</div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->
<div id="controllers" class="clearfix">
<ul>
<?php for ($i = 0; $i < $featured_num; $i++) { ?>
<li>
<div class="controller">
<a href="#"<?php if ( $i == 0 ) echo \' class="active"\'; ?>>
<?php print_thumbnail( $arr[$i][\'thumbnail\'][\'thumb\'], $arr[$i][\'thumbnail\']["use_timthumb"], $arr[$i][\'titletext\'], $small_width, $small_height ); ?>
<span class="overlay"></span>
</a>
</div>
</li>
<?php } ?>
</ul>
<div id="active_item"></div>
</div> <!-- end #controllers -->
它选择一个自定义类别并返回自定义数量的帖子。问题是,它将按创建帖子的顺序显示帖子。
I want to choose my own order, 这样我就可以选择先显示哪些帖子等等。
So, is it possible to select which order the returned posts are displayed? 可能通过针对特定ID?
理想情况下,我希望为第一篇文章设置一个自定义ID(直接在代码中),然后按照返回的顺序显示其余部分。这可能吗?
如果您选择回复,请用代码示例详细说明,谢谢阅读。
SO网友:Bendoh
为了实现你的愿望,你必须提前拿到帖子。在给定ID列表的情况下,无法在SQL中指定任意顺序。
query\\u posts()创建另一个主查询,抛出主查询。由于您正在执行此操作,因此可以使用get\\u posts()返回帖子列表。
$posts = get_posts( array(
\'post_type\' => \'page\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'post__in\' => (array) get_option(\'henrik_feat_pages\'),
\'showposts\' => (int) $featured_num
) );
然后,
$posts_by_id = array();
foreach( $posts as $post ) $posts_by_id[$post->ID] = $post;
现在,您有了一个按ID索引的post对象数组,您可以遍历有序列表并从该列表中选择post:
global $post;
foreach( (array) get_option( \'henrik_feat_pages\' ) as $post_id ) {
$post = $posts_by_id[$post_id];
setup_postdata( $post )
// ... Template code as if you were in the loop.
}
Update
上面的循环替换了主循环。所以与其写作
<?php if (have_posts()) : while (have_posts()) : the_post();
<?php /** Code in loop **/ ?>
<?php endwhile; endif; ?>
要启动循环,我们使用:
global $post;
foreach( (array) get_option( \'henrik_feat_pages\' ) as $post_id ) {
$post = $posts_by_id[$post_id];
setup_postdata( $post );
/** Code in loop **/
}
这是使用稍微不同语法的等效代码$post必须全球化,并在每次迭代时设置为正确的post对象,以及
setup_postdata()
用$post中的信息填充所有相关post变量。
此代码假定选项henrik_feat_pages
是帖子ID的有序列表。
Update 2
如何显示具有已知帖子ID的特定帖子,
$first_post_id
首先:
// Create ordered array of posts to show, with $first_post_id first
$posts_to_show = array_merge( (array) $first_post_id, (array) get_option( \'henrik_feat_pages\' ) );
// Get the pages whose IDs are in $posts_to_show
$posts = get_posts( array(
\'post_type\' => \'page\',
\'post__in\' => $posts_to_show
) );
// Create ordered post array:
$posts_by_id = array();
foreach( $posts as $post ) $posts_by_id[$post->ID] = $post;
// Output loop:
global $post;
foreach( $posts_to_show as $post_id ) {
$post = $posts_by_id[$post_id];
setup_postdata( $post )
// ... Template code as if you were in the loop.
}
注意,我在中简化了查询
get_posts()
所以只有
post_type
和
post__in
提供了参数。没有必要指定排序,因为这些帖子是按代码排序的。