简体   繁体   English

WordPress中的即时贴管理

[英]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. 我过滤了“汽车”类别,它向我显示了10条关于汽车的帖子,但我始终将阿斯顿·马丁视为第一篇帖子。

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. 第二个问题是,如果阿斯顿·马丁·范尼什(Aston Martin Vanish)显示为发粘,则该查询不应再作为普通帖子再次出现。

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. 经过大量研究,我发现的唯一发现是如何仅解决其类别上的Sticky post,但它没有用。

http://wordpress.org/support/topic/sticky-post-from-only-current-category 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. 您需要按$args数组中的当前类别或类别进行过滤。 You can get an array of categories using get_the_category() , and then creating an array of IDs from it. 您可以使用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
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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