79 lines
2.8 KiB
PHP
79 lines
2.8 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) exit;
|
|
|
|
add_action( 'widgets_init', function() {
|
|
register_widget( 'MCN_Latest_News_Widget' );
|
|
} );
|
|
|
|
class MCN_Latest_News_Widget extends WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct( 'mcn_latest_news', '⛏ Minecraft Server News', [
|
|
'description' => 'Zeigt die neuesten Server-News in der Sidebar.',
|
|
] );
|
|
}
|
|
|
|
public function widget( $args, $instance ) {
|
|
$title = apply_filters( 'widget_title', $instance['title'] ?? 'Server News' );
|
|
$anzahl = intval( $instance['anzahl'] ?? 3 );
|
|
|
|
echo $args['before_widget'];
|
|
if ( $title ) echo $args['before_title'] . $title . $args['after_title'];
|
|
|
|
$query = new WP_Query( [
|
|
'post_type' => 'mc_news',
|
|
'posts_per_page' => $anzahl,
|
|
'post_status' => 'publish',
|
|
] );
|
|
|
|
if ( $query->have_posts() ) {
|
|
echo '<ul class="mcn-widget-list">';
|
|
while ( $query->have_posts() ) {
|
|
$query->the_post();
|
|
$cats = get_the_terms( get_the_ID(), 'mc_news_category' );
|
|
$icon = ( $cats && ! empty( $cats[0]->description ) ) ? $cats[0]->description . ' ' : '📰 ';
|
|
printf(
|
|
'<li class="mcn-widget-item"><a href="%s">%s%s</a><span class="mcn-widget-date">%s</span></li>',
|
|
get_permalink(),
|
|
$icon,
|
|
get_the_title(),
|
|
get_the_date( 'd.m.Y' )
|
|
);
|
|
}
|
|
echo '</ul>';
|
|
wp_reset_postdata();
|
|
} else {
|
|
echo '<p>Noch keine News vorhanden.</p>';
|
|
}
|
|
|
|
echo $args['after_widget'];
|
|
}
|
|
|
|
public function form( $instance ) {
|
|
$title = $instance['title'] ?? 'Server News';
|
|
$anzahl = $instance['anzahl'] ?? 3;
|
|
?>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Titel:</label>
|
|
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'title' ); ?>"
|
|
name="<?php echo $this->get_field_name( 'title' ); ?>"
|
|
value="<?php echo esc_attr( $title ); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id( 'anzahl' ); ?>">Anzahl News:</label>
|
|
<input class="widefat" type="number" min="1" max="10"
|
|
id="<?php echo $this->get_field_id( 'anzahl' ); ?>"
|
|
name="<?php echo $this->get_field_name( 'anzahl' ); ?>"
|
|
value="<?php echo esc_attr( $anzahl ); ?>">
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update( $new, $old ) {
|
|
return [
|
|
'title' => sanitize_text_field( $new['title'] ),
|
|
'anzahl' => intval( $new['anzahl'] ),
|
|
];
|
|
}
|
|
}
|