[英]Loop through sticky posts in wordpress?
我是Wordpress的新手,並且嘗試使用以下代碼遍歷粘性帖子:
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Query sticky posts */
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
<?php
foreach ($stickies as $sticky) {
the_title();
comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );
}
?>
但是在輸出中,如果我有2個即時貼帖子,則第一個顯示兩次。
任何想法 ?
非常感謝你的幫助 !
編輯:
看起來
foreach ($stickies['WP_Post Object'] as $sticky) {
得到了很好的兩篇文章,但我仍然有此錯誤消息:警告:為foreach()提供了無效的參數...
完整代碼:
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
<?php
foreach ($stickies['WP_Post Object'] as $sticky) {
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
}
the_title();
comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );
}
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="media">
<a class="pull-left" href="<?php the_permalink() ?>">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
}
?>
</a>
<div class="media-body">
<h3 class="media-heading"><a class="permalink" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h3>
Par <?php the_author(); ?>, le <?php the_time('j F Y'); ?> - <?php comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' ); ?>
<?php the_excerpt(); ?>
</div>
</div> <!-- /media -->
<?php endwhile; ?>
<?php endif; ?>
嘗試這個:
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1, 'ignore_sticky_posts' => true) );
編輯:
我認為您應該使用標准的wordpress帖子循環:
while (have_posts()) : the_post();
the_title();
endwhile;
編輯:
您可以使用rewind_posts()
開始新的循環:
// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
// rewind
<?php rewind_posts(); ?>
// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.