php - Sticky posts managment in Wordpress -


i have problem sticky post. code use works, sticky posts showed on every query, , need modification in order show them when categories in queried. example:

i filter category cars , shows me 10 posts cars, see aston martin first post.

if filter category motorcycles, should not show me aston martin first one, ducati example.

the second problem is, if aston martin vanish showed sticky, should not come again normal post in query.

after doing lot of research , thing found how solve sticky post on category didn't work.

http://wordpress.org/support/topic/sticky-post-from-only-current-category

<?php /*template 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 hold cat ids $categories = get_the_category();  // current categories array foreach ( $categories $category ){    $cat_ids[] = $category->cat_id; // assign 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 );    // 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"),);   // query  $the_query2 = new wp_query( $args2 );  // 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 filter current category or categories in $args array. can array of categories using get_the_category(), , creating array of ids it.

$cat_ids = array();                // array hold cat ids $categories = get_the_category();  // current categories array foreach ( $categories $category ){    $cat_ids[] = $category->cat_id; // assign argument array } $args = array(     'posts_per_page' => -1,        // return many sticky posts can     'category__in' => $cat_ids,    // posts current categories     'post__in'  => $sticky,        // sticky post ids ); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -