从"最近的帖子小部件"中排除当前帖子

时间:2015-01-15 17:37:58

标签: widget posts

在用于在侧边栏小部件中显示最近帖子的插件中,我们如何将过滤器应用于插件的functions.php,以便它不会在显示中包含当前页面/帖子?< / p>

插件作者回答说,在他进入长时间的沉默之前:&#34;您可以将自定义参数添加到rpwe_default_query_arguments过滤器。只需添加exclude =&gt; get_the_ID()到过滤器。&#34;

在这里,我们添加它吗?

// Allow plugins/themes developer to filter the default query.
    $query = apply_filters( 'rpwe_default_query_arguments', $query );

如何?

这是插件:https://wordpress.org/plugins/recent-posts-widget-extended/

我在这个网站上找到了一些看似简单的指导:http://themeshaper.com/2009/05/03/filters-wordpress-child-themes/ 但后来......在尝试纠正语法时,我的网站(localhost)出现错误。 &#34; = GT;&#34;似乎没有正确使用。

这是我到目前为止所做的:     add_filter(&#39; rpwe_default_query_arguments&#39;,&#39; rpwe_exclude_current&#39;);     function rpwe_exclude_current($ query){         &#39;排除&#39; =&GT; get_the_ID()         $ posts = new WP_Query($ query);         返回$ posts;     }

1 个答案:

答案 0 :(得分:0)

以下是在我的情况下有效的答案:

add_filter( 'rpwe_default_query_arguments', 'my_function_name' );
function my_function_name( $args ) {
    if( is_singular() && !isset( $args['post__in'] ) )
    $args['post__not_in'] = array( get_the_ID() );
    return $args;
}

以下是我发现的the site