我正在尝试从侧边栏中的“最受欢迎帖子”小部件中隐藏页面。 我有一个“ WP Hide Post”插件,它可以帮助我从任何地方隐藏,但不会从小部件中隐藏页面。
答案 0 :(得分:1)
在WP_Query中使用exclude参数。
<?php
$excudeID = 30;// the post_id off the excluded page
$popularpost = new WP_Query( array( 'post__not_in' => array($excludeID) ) );
//use this query in your widget
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>
另一种选择是编写自己的小部件:
class drc_PopulairPostsWithoutThatone extends WP_Widget
{
public function __construct()
{
// Instantiate the parent object
parent::__construct(false, 'Populair Posts Title');
}
public function widget($args, $instance)
{
global $wpdb;
$excludeId = 30;
$return = "<ul>";
$query = $wpdb->prepare("SELECT * FROM wp_popularpostsdata, wp_posts WHERE wp_popularpostsdata.postid = $wpdb->posts.ID AND $wpdb->posts.post_type = 'page' AND wp_popularpostsdata.postid != %d ORDER BY pageviews DESC limit 10", $excludeId);
$tops = $wpdb->get_results($query);
foreach ($tops as $top) {
$return .= '<li><a href="' . get_permalink($top->postid) . '">' . get_the_title($top->postid) . '</a> - ' . $top->pageviews . '</li>';
}
$return .= "</ul>";
return $return;
}
public function update($new_instance, $old_instance)
{
// Save widget options
}
public function form($instance)
{
// Output admin widget options form
}
}
function drc_register_widgets()
{
register_widget('drc_PopulairPostsWithoutThatone');
}
add_action('widgets_init', 'drc_register_widgets');
查询来自:https://wordpress.org/support/topic/shortcode-get-the-least-popular-posts/ 有关小部件的文档:https://codex.wordpress.org/Function_Reference/register_widget