从部分中排除主页滑块中的帖子

时间:2019-01-31 作者:Zawaya Vision

我有一个主滑块和新闻部分的主页。

主滑块通过tag=\'slider\'获取帖子,并显示10个帖子限制。

各部分按类别获取帖子(每个部分按特定类别)

我想做的是:从部分中排除在滑块中显示的帖子,直到帖子退出滑块。

以下是滑块的代码和部分示例:

Slider Code :

<?php
// Slider
$tag = ap_option(\'slider_tag\');
$count = ap_option(\'slider_count\');
$slid = new WP_Query(
array(
\'tag\'            => $tag ,
\'posts_per_page\' => $count ,
\'post__not_in\'   => get_option( \'sticky_posts\' ),
)
);
?>
<div class="thumbslider">
<div class="postlist">
<?php while ( $slid->have_posts() ) : $slid->the_post(); ?>
<div class="item">
<div class="det">
<h2><a href="<?php the_permalink() ?>" title="<?php the_title();?>"><?php 
the_title(); ?></a></h2>
        </div>
        <div class="thumbnail">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( \'slider\' );
            } else {
              echo \'<img alt="Assahifa.com" title="Assahifa.com" src="https://placeholdit.imgix.net/~text?txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">\';
            } 
            ?>
          </a>
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
    </div>
    </div>
Section News Sample :

   <?php
   // cat eco
   $n=0;
   $cat_eco = ap_option(\'sec_eco_cat\');
   $count_eco = ap_option(\'sec_eco_count\');
   $eco = new WP_Query(
   array(
   \'cat\'            => $cat_eco ,
   \'posts_per_page\' => $count_eco ,
   \'post__not_in\'   => get_option( \'sticky_posts\' ),
   \'offset\'         => 0,
     )
   );
   ?>
   <section class="main_eco clearfix">
   <div class="box">
   <div class="title" style="color: <?php echo ap_option(\'eco-color\'); ?>;"> 
   <h3 style="color: <?php echo ap_option(\'eco-color\'); ?>;"><a href="<?php  
   echo get_category_link( $cat_eco ); ?>" title="<?php echo 
   ap_option(\'sec_eco_title\'); ?>" style="color: <?php echo ap_option(\'eco- 
   color\'); ?>;"><?php echo ap_option(\'sec_eco_title\'); ?></a></h3></div>
   <div class="postlist">
      <?php while ( $eco->have_posts() ) : $eco->the_post();$n++; ?>
      <div class="item clearfix">
        <div class="thumbnail imgtrans">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( \'md\' );
            } else {
              echo \'<img alt="AisPanel" title="AisPanel" 
      src="https://placeholdit.imgix.net/~text? 
      txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">\';
            } 
            ?>
          </a>
        </div>
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();? 
       >"><?php the_title(); ?></a></h2>
          <!--<?php if($n == 1) the_excerpt(); ?>-->
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
      </div>
    </div>
    </section>

2 个回复
SO网友:Chinmoy Kumar Paul

我假设您正在将滑块代码保存在slider.php 文件,新闻节代码section-news.php 并在首页/主页/索引中跟踪此类代码段。php文件

get_template_part(\'slider\');
get_templates_part(\'section-news\');
然后可以编辑滑块。php文件并添加新的数组变量$do_not_duplicate. 因此,它保留了滑块柱ID以备将来使用。下面是slider的新代码

<?php
// Slider
$tag = ap_option(\'slider_tag\');
$count = ap_option(\'slider_count\');
$do_not_duplicate = array();
$slid = new WP_Query(
  array(
  \'tag\'            => $tag ,
  \'posts_per_page\' => $count ,
  \'post__not_in\'   => get_option( \'sticky_posts\' ),
  )
);
?>
<div class="thumbslider">
  <div class="postlist">
    <?php 
      while ( $slid->have_posts() ) : 
        $slid->the_post(); 
        $do_not_duplicate[] = get_the_ID();
    ?>
      <div class="item">
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h2>
        </div>
        <div class="thumbnail">
            <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
              <?php 
              if ( has_post_thumbnail() ) { 
                the_post_thumbnail( \'slider\' );
              } else {
                echo \'<img alt="Assahifa.com" title="Assahifa.com" src="https://placeholdit.imgix.net/~text?txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">\';
              } 
              ?>
            </a>
          </div>
        </div>
      <?php endwhile;wp_reset_postdata(); ?>
    </div>
</div>
现在我将从新闻循环中排除滑块帖子。这是新闻部分的新代码。正在合并$do_not_duplicate 具有的阵列get_option( \'sticky_posts\' ) 价值和传递到post__not_in 数组键。

<?php
   // cat eco
   $n=0;
   $cat_eco = ap_option(\'sec_eco_cat\');
   $count_eco = ap_option(\'sec_eco_count\');
   $sticky_posts = ( get_option( \'sticky_posts\' ) ) ? get_option( \'sticky_posts\' ) : array();
   $eco = new WP_Query(
     array(
       \'cat\'            => $cat_eco ,
       \'posts_per_page\' => $count_eco ,
       \'post__not_in\'   => array_merge( $do_not_duplicate, $sticky_posts ),
       \'offset\'         => 0,
      )
   );
   ?>
   <section class="main_eco clearfix">
   <div class="box">
   <div class="title" style="color: <?php echo ap_option(\'eco-color\'); ?>;"> 
   <h3 style="color: <?php echo ap_option(\'eco-color\'); ?>;"><a href="<?php  
   echo get_category_link( $cat_eco ); ?>" title="<?php echo 
   ap_option(\'sec_eco_title\'); ?>" style="color: <?php echo ap_option(\'eco- 
   color\'); ?>;"><?php echo ap_option(\'sec_eco_title\'); ?></a></h3></div>
   <div class="postlist">
      <?php while ( $eco->have_posts() ) : $eco->the_post();$n++; ?>
      <div class="item clearfix">
        <div class="thumbnail imgtrans">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( \'md\' );
            } else {
              echo \'<img alt="AisPanel" title="AisPanel" 
      src="https://placeholdit.imgix.net/~text? 
      txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">\';
            } 
            ?>
          </a>
        </div>
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();? 
       >"><?php the_title(); ?></a></h2>
          <!--<?php if($n == 1) the_excerpt(); ?>-->
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
      </div>
    </div>
    </section>

SO网友:Zawaya Vision

与朋友一起,我们找到了一个解决方案:-我们在函数中创建一个排除函数。然后我们把它叫做我们去的地方

这是函数的代码。php

    // Function exlude
        if( !function_exists(\'get_slide_ids\') ){
        function get_slide_ids() {  
        global $wp_query;
        $POST_LIMIT = ap_option(\'slider_count\');
        $POST_TAG = ap_option(\'slider_tag\');
        $ids= [];
        $args =  array(
                    \'post_status\' => \'publish\',
                    \'tag\'            => $POST_TAG ,
                    \'posts_per_page\' => $POST_LIMIT ,
                );
        $wp_query = new WP_Query($args);
        if ($wp_query->have_posts() ):
            while ($wp_query->have_posts()) : $wp_query->the_post();
                  $ids[] = get_the_ID(); 
            endwhile;   
            return $ids;
        endif; 
        return \'\';
        }
     }
要调用它,请添加语法not in,这是我的代码,请尝试将其采用到您的代码中:

     <?php
      /**
      // cat eco
      $n=0;
      $cat_eco = ap_option(\'sec_eco_cat\');
      $count_eco = ap_option(\'sec_eco_count\');
      $sticky_posts = ( get_option( \'sticky_posts\' ) ) ? get_option( \'sticky_posts\' ) : array();
      $eco = new WP_Query(
      array(
        \'cat\'            => $cat_eco ,
        \'posts_per_page\' => $count_eco ,
        \'post__not_in\'   => get_slide_ids(),
           )
         );
       ?>

相关推荐

GET_POSTS在页面模板中工作,但不在短码中工作

我正在尝试编写一个短代码,其中包括“get\\u posts”,以便获取博客帖子数据,然后在页面上显示最近的3篇文章。此代码在模板中工作。然而,当我将其放入输出缓冲区(ob\\u start)内的短代码中时,它无法检索帖子。相反,它会获取当前页面本身并循环浏览该页面(在本例中为主页)。你知道我怎样才能让它按照最初的意图在帖子中循环吗?以下是在模板中工作的代码:<?php $lastposts = get_posts( array(\'posts_per_page\' => 3) );?>