我正在尝试创建/添加一个小部件到客户的网站,列出所有非管理员作者及其帖子的链接。我能够做到这一点,但其中一位作者的帖子几乎完全使用了我为菜谱创建的CPT,因此该小部件不会链接到她的任何帖子。
由于这一点,并且由于将来可能会出现另一个CPT的类似情况,我希望我的小部件查询每个帖子类型,以查看是否有任何给定的作者发布了具有该帖子类型的条目,然后链接到显示所有帖子的存档页,而不管帖子类型如何。
我搜索过法典、各种插件、谷歌搜索等等。。。但还没有找到解决这个具体问题的办法。
我为这个自定义小部件创建了一个插件,并包含下面的代码。另外,我在插件的widgets部分使用exclude\\u admin参数时也遇到了问题。如果有任何帮助,我们也将不胜感激。谢谢
<?php
class HEA_Author_List_Widget extends WP_Widget {
public function __construct() {
$widget_details = array(
\'classname\' => \'hea-author-list\',
\'description\' => \'A widget to display authors and link to their posts.\',
);
parent::__construct( \'hea-author-list\', \'HEA Author List\', $widget_details );
}
public function form( $instance ) {
// Field Values
$title = ( !empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : \'\';
$count = ( !empty( $instance[\'count\'] ) ) ? $instance[\'count\'] : 3;
$orderby = ( !empty( $instance[\'orderby\'] ) ) ? $instance[\'orderby\'] : \'post_count\';
$order = ( !empty( $instance[\'order\'] ) ) ? $instance[\'order\'] : \'desc\';
// Field Options
$orderby_options = array(
\'ID\' => \'ID\',
\'email\' => \'Email\',
\'post_count\' => \'Post Count\'
);
$order_options = array(
\'desc\' => \'Descending\',
\'asc\' => \'Ascending\'
);
?>
<div class=\'unread-posts\'>
<p>
<label for="<?php echo $this->get_field_name( \'title\' ); ?>">Title: </label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_name( \'count\' ); ?>">Authors To Show: </label>
<input class="widefat" id="<?php echo $this->get_field_id( \'count\' ); ?>" name="<?php echo $this->get_field_name( \'count\' ); ?>" type="text" value="<?php echo esc_attr( $count ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_name( \'orderby\' ); ?>">Order By: </label>
<select class=\'widefat\' id="<?php echo $this->get_field_id( \'orderby\' ); ?>" name="<?php echo $this->get_field_name( \'orderby\' ); ?>">
<?php foreach( $orderby_options as $value => $name ) : ?>
<option <?php selected( $orderby, $value ) ?> value=\'<?php echo $value ?>\'><?php echo $name ?></option>
<?php endforeach ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_name( \'order\' ); ?>">Order By: </label>
<select class=\'widefat\' id="<?php echo $this->get_field_id( \'order\' ); ?>" name="<?php echo $this->get_field_name( \'order\' ); ?>">
<?php foreach( $orderby_options as $value => $name ) : ?>
<option <?php selected( $order, $value ) ?> value=\'<?php echo $value ?>\'><?php echo $name ?></option>
<?php endforeach ?>
</select>
</p>
</div>
<?php
}
public function widget( $args, $instance ) {
echo $args[\'before_widget\'];
if( !empty( $instance[\'title\'] ) ) {
echo $args[\'before_title\'] . apply_filters( \'widget_title\', $instance[\'title\'], $instance, $this->id_base ) . $args[\'after_title\'];
}
$authors = array(
\'orderby\' => $instance[\'orderby\'],
\'order\' => $instance[\'order\'],
\'number\' => $instance[\'count\'],
\'exclude_admin\' => true,
\'hide_empty\' => false,
\'show_fullname\' => true,
);
$users = wp_list_authors( $authors );
if( !empty( $users ) || empty( $users ) ) {
echo \'<ul>\';
foreach( $users as $user ) {
echo \'<li>\';
echo the_author_posts_link();
echo \'</li>\';
}
echo \'</ul>\';
}
echo $args[\'after_widget\'];
}
#public function update( $new_instance, $old_instance ) {}
}
add_action( \'widgets_init\', function(){
register_widget( \'HEA_Author_List_Widget\' );
});
?>