簡體   English   中英

從類別中獲取WordPress粘性帖子

[英]Fetch WordPress sticky posts from category

我已經為此工作了一段時間,基本上我正在嘗試從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; ?>

我假設您在$ 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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM