简体   繁体   中英

Sticky posts managment in Wordpress

I have a problem with sticky post. The code that I use works, but the sticky posts are showed on every query, and I need a modification in order to show them just when the categories that they are in are queried. Example:

I filter category CARS and it shows me 10 posts with cars, but I always see Aston Martin as a first post.

If I filter category Motorcycles, it should not show me Aston Martin as the first one, but Ducati for example.

The second problem is, if Aston Martin Vanish is showed as sticky, it should not come again as normal post in that query.

After doing a lot of research , the only thing that I found is how to solve Sticky post on its category only but it didn't work.

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(); ?>

You need to filter by the current category or categories in your $args array. You can get an array of categories using get_the_category() , and then creating an array of IDs from it.

$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
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM