【问题标题】:Fetch WordPress sticky posts from category从类别中获取 WordPress 粘性帖子
【发布时间】:2013-07-01 08:03:19
【问题描述】:

我已经为此工作了一段时间,基本上我试图从 WordPress 中的特定类别中获取前三个棒帖子并仅显示它们。我在下面有一些代码,但是这会获取所有帖子,而不仅仅是在该类别中标记为粘性的特定帖子。

<?php $sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
 );
$the_query = new WP_Query($query_args); ?>
<?php $count = 0; ?>  
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate =      $post->ID; ?>
<?php $count++; ?>  

<?php if ($count == 1) : ?>  
    <div class="featurethumb"><?php the_post_thumbnail(array(306,306), array ('class' => 'featurethumb')); ?>
<div class="featuretitle-bg"><div class="featuretitle"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></div>
<div class="featured-desc"><?php the_excerpt(__('(more…)')); ?></div></div>
</div>
<?php elseif ($count == 2) : ?>  
    <div class="index-thumb"><?php the_post_thumbnail(array(100,100), array     ('class' => 'alignleft1')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<?php the_excerpt(__('(more…)')); ?>
<?php else : ?>  
    <div class="index-thumb"><?php the_post_thumbnail(array(100,100), array   ('class' => 'alignleft2')); ?></div>
 <div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<?php the_excerpt(__('(more…)')); ?>
<?php endif; ?>

 <?php endwhile; ?>

【问题讨论】:

  • $categorydefined 在哪里?你混淆了$the_querymy_query

标签: php wordpress post sticky


【解决方案1】:

我假设您在 RST 提到的某处定义了 $category?

<?php

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
$query_args = array( 
                       'post__in' => $sticky, 
                       'category__in'=>array($category)
                    );
?>

$my_query = new WP_Query($query_args); ?>
 <?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate =      $post->ID; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多