我的粘贴帖有问题。我使用的代码有效,但粘贴的帖子显示在每个查询中,我需要进行修改,以便在查询它们所在的类别时显示它们。例如:
我过滤了类别CARS,它向我展示了10个有车的帖子,但我总是把阿斯顿马丁视为第一个帖子。
如果我过滤摩托车类别,它不应该向我展示阿斯顿马丁作为第一个,但杜卡迪就是这样。
第二个问题是,如果Aston Martin Vanish显示为粘性,那么它不应该再次成为该查询中的正常帖子。
经过大量的研究,我发现的唯一一件事就是如何只在其类别上解决粘性帖子,但它没有用。
http://wordpress.org/support/topic/sticky-post-from-only-current-category
<?php
/*Template for displaying archives*/
?>
<?php get_header(); ?>
<div id="middle-full">
<div id="middle">
<div id="slider">
<h1 class="rezultatipretrage"><?php $tit = wp_title('', false); $newtit = array("Kursevi stranih jezika", ";"); $newtit2 = array("", " |"); echo str_replace($newtit, $newtit2, $tit);?></h1>
</div>
<div id="pretraga">
<?php if(is_active_sidebar('sidebar-1')) : ?>
<div id="sidebar">
<ul>
<?php dynamic_sidebar('sidebar-1'); ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
<div id="main">
<div id="content-wrap">
<div id="content">
<h3 class="pronadjeno"> </h3>
<div class="ispodpronadjeno"> <div class="prvideotrake"><span>Pronađeno je <?php global $wp_query; echo $wp_query->found_posts; ?> kurseva:</span></div>
<div class="cenatxt"> <span></span> </div> <div class="cenasapopustom"><span></span></div></div>
<?php if ( have_posts() ) : ?>
<?php
//is sticky
$cat_IDs = array(); // array to hold cat IDs
$categories = get_the_category(); // current categories as array
foreach ( $categories as $category ){
$cat_IDs[] = $category->cat_ID; // assign to argument array
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => -1, //uzmi sve postove
'category__in' => $cat_IDs,
'post__in' => $sticky, //pogledaj koji je sticky
);
// The Query
$do_not_duplicate = array();
$the_query = new WP_Query( $args );
}
while ( $the_query->have_posts() ) {
$the_query->the_post();
$duplikat[] = $post->ID;
echo '<li>' . get_the_title() . '</li>';
//OVDE IDE STICKY POST DEO
}
wp_reset_query(); // resetuje se query
$args2 =array("post__not_in" =>get_option("sticky_posts"),);
// The Query
$the_query2 = new WP_Query( $args2 );
// The Loop.... ?>
<?php while ( have_posts($the_query2) ) : the_post($the_query2);
if ( !in_array( $post->ID, $duplikat ) ){
?>
<div id="postnapretrazi">
<div id="cena"><span class="cena">Cena:</span></div>
<div id="cenapopustt"><span class="cenapopustt">Cena uz popust:</span></div>
<div class="excerpt-thumb">
<?php the_post_thumbnail('excerpt-thumbnail', 'class=alignleft'); ?>
</div>
<div id="standardnicontent">
<?php get_template_part( 'content', get_post_format() )?>
<div id="imeskole"><span class="ime_skole"><?php the_field('ime_skole_stranih_jezika') ?></span></div>
<div id="drugideo">
<div id="trajanje"><span class="trajanje"><?php the_field('trajanje_kursa') ?></span></div>
<div id="broj_casova_nedeljno"><span class="broj_casova_nedeljno"><?php the_field('broj_casova_nedeljno') ?></span></div>
<div id="raspon_godina"><span class="raspon_godina"><?php the_field('raspon_godina') ?></span></div>
</div>
</div>
<a class="dugmeupostu" title="<?php echo get_the_title($ID); ?>" href=" <?php the_permalink(); ?>">Kliknite ovde za detalje</a>
<div id="cenakursa"><span class="spancenakursa"><span class="cenaunutar"></span><?php the_field('cena') ?></span></div>
<div id="cenakursasapopustom"><span class="spancenakursasapopustom"><span class="cenaunutar"></span><?php the_field('cena_sa_popustom') ?></span></div>
</div>
<?php } ?>
<?php $postnum++;
if ($postnum == 4) {
echo '<div class="loopbanner"></div>';
}
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_the_content(); ?>
<?php endif; ?>
<div id="pagcon">
<?php wp_pagination(); ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
答案 0 :(得分:0)
您需要按$args
数组中的当前类别进行过滤。您可以使用get_the_category()
获取一系列类别,然后从中创建一个ID数组。
$cat_IDs = array(); // array to hold cat IDs
$categories = get_the_category(); // current categories as array
foreach ( $categories as $category ){
$cat_IDs[] = $category->cat_ID; // assign to argument array
}
$args = array(
'posts_per_page' => -1, // return as many sticky posts as we can
'category__in' => $cat_IDs, // get posts only from the current categories
'post__in' => $sticky, // get only sticky post IDs
);