Last active
July 14, 2021 23:33
-
-
Save mihdan/b40225d4c2fe2e14b016022819c93ab4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( | |
'widget_posts_args', | |
function ( $args, $instance ) { | |
if ( isset( $instance['cpt'] ) ) { | |
$args['post_type'] = $instance['cpt']; | |
} | |
if ( isset( $instance['order_by'] ) ) { | |
$args['orderby'] = $instance['order_by']; | |
} | |
return $args; | |
}, | |
10, | |
2 | |
); | |
add_filter( | |
'widget_update_callback', | |
function ( $instance, $new_instance, $old_instance, WP_Widget $widget ) { | |
if ( ! $widget instanceof WP_Widget_Recent_Posts ) { | |
return $instance; | |
} | |
$instance['cpt'] = $new_instance['cpt']; | |
$instance['order_by'] = $new_instance['order_by']; | |
return $instance; | |
}, | |
10, | |
4 | |
); | |
add_action( | |
'in_widget_form', | |
function ( WP_Widget $widget, $form, $instance ) { | |
if ( ! $widget instanceof WP_Widget_Recent_Posts ) { | |
return; | |
} | |
$post_types = get_post_types( [ 'public' => true ], 'objects' ); | |
if ( ! $post_types ) { | |
return; | |
} | |
$order_fields = [ | |
'post_date' => 'Дата', | |
'post_title' => 'Название', | |
'post_name' => 'Слаг', | |
'rand' => 'Случайно', | |
]; | |
$cpt = $instance['cpt'] ?? 'post'; | |
$order_by = $instance['order_by'] ?? 'post_date'; | |
?> | |
<p> | |
<label for="<?php echo $widget->get_field_id('cpt'); ?>">Тип записей:</label> | |
<select id="<?php echo $widget->get_field_id('cpt'); ?>" name="<?php echo $widget->get_field_name('cpt'); ?>"> | |
<?php foreach ( $post_types as $post_type ) : ?> | |
<option value="<?php echo esc_attr( $post_type->name ); ?>"<?php selected( $cpt, $post_type->name ); ?>><?php echo esc_html( $post_type->label ); ?></option> | |
<?php endforeach; ?> | |
</select> | |
</p> | |
<p> | |
<label for="<?php echo $widget->get_field_id('order_by'); ?>">Сортировать по полю:</label> | |
<select id="<?php echo $widget->get_field_id('order_by'); ?>" name="<?php echo $widget->get_field_name('order_by'); ?>"> | |
<?php foreach ( $order_fields as $name => $label ) : ?> | |
<option value="<?php echo esc_attr( $name ); ?>"<?php selected( $order_by, $name ); ?>><?php echo esc_html( $label ); ?></option> | |
<?php endforeach; ?> | |
</select> | |
</p> | |
<?php | |
}, | |
10, | |
3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment