Creare una Widget per Wordpress con post e immagine in evidenza
Con questo tutorial vedremo come creare una Widget di Wordpress per visualizzare post di qualsiasi post_type con la rispettiva immagine in evidenza e con la possibilità di scegliere il tipo di ordinamento dei post. Per creare la nostra widget faremo riferimento alla documentazione di wordpress relativa alle Widget API che estenderemo con il nostro codice:
1. Inserire nel file functions.php del nostro tema wordpress la seguente classe con la relativa do_action per aggiungere la nostra widget a quelle preesistenti:
[php]
__( ‘Ultimi Aritcoli con Immagine in Evidenza’ ), ) //sostituire text_domain con quello relativo al tema corrente
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( ‘widget_title’, $instance[‘title’] ); // titolo della widget che sarà visualizzato nella sidebar
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
//INSERIREMO QUI IL CODICE PER LA VISUALIZZAZIONE DEI POST
echo $after_widget;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ ‘title’ ] ) ) {
$title = $instance[ ‘title’ ];
}
else {
$title = ”;
}
?>
get_field_name( ‘post_type_show’ ); ?>”>
echo ‘
- ‘;
- ‘;
echo ‘
Download
foreach($post_to_show as $post){
echo ‘