Wordpress:自定义循环以排除小部件$args中分配的post-id


Wordpress: Custom loop to exclude post id assigned within widget $args

function.php中注册小部件以显示定义的post_id meta:

class featured_widget extends WP_Widget
{
  /**
     * Display front-end contents.
     */
    function widget($args, $instance)
    {
        $post = get_post($instance['post_id']);
...
}

}

我想从我的循环中排除分配的$postpost_id

if (have_posts()) : while (have_posts()) : the_post();

1。如何获取post_id

WordPress将小部件数据存储在选项表中,option_namewidget_{$id_base}。例如,当您构建这样的小部件时:

function __construct() {
    parent::__construct('so37244516-widget',
        __('A label', 'text-domain'), [
        'classname'   => 'so37244516-widget-class',
        'description' => __('Some descriptions', 'text-domain')
    ]);
}

option_name应该是widget_so37244516-widget。然后要检索小部件数据,我们只需要使用:

$data = get_option('widget_so37244516-widget');

但是,因为一个小部件可以有多个实例,所以$data是一个具有不可预测键的关联数组。(每次我们将小部件拖动到侧边栏并保存它时,都会返回该小部件的一个新实例)。

因此,如果整个站点中只有一个小部件实例,那么$data[2]['post_id']就是我们需要的值。如果有多个实例,我们需要循环使用$data,比较一些键和值以找出正确的键和值。和往常一样,var_dump($data)非常有帮助。

2.从循环中排除post_id的帖子

假设$exclude_id是我们从步骤1中得到的值。

  1. 如果要进行自定义循环,请使用@hemnath_mouli的方法:
$query = new WP_Query([
    'post__not_in' => [$exclude_id]
]);
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        // Do loop.
    endwhile;
    wp_reset_query(); // Must have.
else :
    // Do something.
endif;

记得做wp_reset_query()

  1. 如果使用默认循环,请在functions.php中尝试@Deepti_chipdey的方法:
add_action('pre_get_posts', function($query)
{
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set('post__not_in', [$exclude_id]);
    }
});

请确保将is_home()更改为您的首选项页面。

您需要使用pre-get-posts挂钩。

Tyr这个代码

function exclude_single_posts_home($query) {
  if ($query->is_home() && $query->is_main_query()) {
    $query->set('post__not_in', array($post));
   }
}
add_action('pre_get_posts', 'exclude_single_posts_home');

如果您想导出帖子,则必须在WP_Query 中使用post__not_in

$post = new WP_Query( array( 'post__not_in' => array( $exclude_ids ) ) );

希望这对你有帮助。!

如果您想排除单个帖子,请按照上面提到的步骤操作

但除非你单独给出帖子id,否则你只需要制作所有你想在一个类别中排除的帖子,并通过简单的方式将其排除。

从某些类别中排除帖子

    <?php $query = new WP_Query( 'cat=-3,-8' ); ?>// 3 and 8 are category id

详细示例

    <?php $query = new WP_Query( 'cat=-3,-8' ); ?>
    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <div class="post">
    <!-- Display the Title as a link to the Post's permalink. -->
     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
   <small><?php the_time( 'F jS, Y' ); ?> by <?php the_author_posts_link(); ?></small>
    <div class="entry">
     <?php the_content(); ?>
    </div>
     <p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
     </div> <!-- closes the first div box -->
     <?php endwhile; 
          wp_reset_postdata();
          else : ?>
          <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
      <?php endif; ?>

推荐链接:点击我