Files
Minecraft-Server-News/includes/shortcode.php
2026-05-11 09:17:15 +02:00

141 lines
5.5 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) exit;
add_shortcode( 'server_news', 'mcn_render_shortcode' );
function mcn_render_shortcode( $atts ) {
$atts = shortcode_atts( [
'anzahl' => 10,
'kategorie' => '',
'layout' => 'grid', // grid | list
'badge' => '',
], $atts, 'server_news' );
$args = [
'post_type' => 'mc_news',
'posts_per_page' => intval( $atts['anzahl'] ),
'post_status' => 'publish',
'meta_query' => [],
'tax_query' => [],
];
// Kategorie-Filter
if ( ! empty( $atts['kategorie'] ) ) {
$args['tax_query'][] = [
'taxonomy' => 'mc_news_category',
'field' => 'slug',
'terms' => array_map( 'trim', explode( ',', $atts['kategorie'] ) ),
];
}
// Angepinnte zuerst
$pinned_args = $args;
$pinned_args['meta_query'][] = [ 'key' => '_mcn_pinned', 'value' => '1' ];
$pinned_query = new WP_Query( $pinned_args );
$normal_args = $args;
$normal_args['meta_query'][] = [
'relation' => 'OR',
[ 'key' => '_mcn_pinned', 'compare' => 'NOT EXISTS' ],
[ 'key' => '_mcn_pinned', 'value' => '', 'compare' => '=' ],
];
$normal_query = new WP_Query( $normal_args );
ob_start();
?>
<div class="mcn-wrapper mcn-layout-<?php echo esc_attr( $atts['layout'] ); ?>">
<?php // ---- Filter-Bar ---- ?>
<div class="mcn-filterbar">
<button class="mcn-filter-btn active" data-filter="all">📋 Alle</button>
<?php
$cats = get_terms( [ 'taxonomy' => 'mc_news_category', 'hide_empty' => true ] );
foreach ( $cats as $cat ) {
$icon = ! empty( $cat->description ) ? $cat->description . ' ' : '';
printf(
'<button class="mcn-filter-btn" data-filter="%s">%s%s</button>',
esc_attr( $cat->slug ),
$icon,
esc_html( $cat->name )
);
}
?>
</div>
<div class="mcn-grid">
<?php
// Angepinnte zuerst
foreach ( [ $pinned_query, $normal_query ] as $query ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
$pinned = get_post_meta( $post_id, '_mcn_pinned', true );
$badge = get_post_meta( $post_id, '_mcn_badge', true );
$server_ip = get_post_meta( $post_id, '_mcn_server_ip', true );
$cats = get_the_terms( $post_id, 'mc_news_category' );
$cat_slugs = $cats ? implode( ' ', wp_list_pluck( $cats, 'slug' ) ) : '';
$cat_name = $cats ? $cats[0]->name : '';
$cat_icon = $cats ? $cats[0]->description : '📰';
?>
<article class="mcn-card <?php echo $pinned ? 'mcn-pinned' : ''; ?>"
data-category="<?php echo esc_attr( $cat_slugs ); ?>">
<?php if ( $pinned ) : ?>
<div class="mcn-pin-label">📌 Angepinnt</div>
<?php endif; ?>
<?php if ( $badge ) : ?>
<div class="mcn-badge mcn-badge-<?php echo esc_attr( strtolower( $badge ) ); ?>"><?php echo esc_html( $badge ); ?></div>
<?php endif; ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="mcn-card-image">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium_large' ); ?>
</a>
</div>
<?php endif; ?>
<div class="mcn-card-body">
<?php if ( $cat_name ) : ?>
<span class="mcn-cat-tag"><?php echo $cat_icon . ' ' . esc_html( $cat_name ); ?></span>
<?php endif; ?>
<h2 class="mcn-card-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<div class="mcn-card-meta">
<span class="mcn-date">📅 <?php echo get_the_date( 'd.m.Y' ); ?></span>
<span class="mcn-author">✍️ <?php the_author(); ?></span>
</div>
<div class="mcn-card-excerpt">
<?php echo wp_trim_words( get_the_excerpt(), 20, '…' ); ?>
</div>
<?php if ( $server_ip ) : ?>
<div class="mcn-server-ip">
🖥️ <span><?php echo esc_html( $server_ip ); ?></span>
<button class="mcn-copy-btn" data-ip="<?php echo esc_attr( $server_ip ); ?>">Kopieren</button>
</div>
<?php endif; ?>
<a href="<?php the_permalink(); ?>" class="mcn-read-more">Weiterlesen →</a>
</div>
</article>
<?php
}
}
wp_reset_postdata();
?>
</div><!-- .mcn-grid -->
<div class="mcn-empty" style="display:none">
<p>😔 Keine News in dieser Kategorie gefunden.</p>
</div>
</div><!-- .mcn-wrapper -->
<?php
return ob_get_clean();
}