Files
Pruebas/Wordpress/wordpress/wp-content/plugins/tabs-con-card/index.php
2026-04-14 13:50:04 -06:00

114 lines
4.5 KiB
PHP

<?php
/**
* Plugin Name: Tabs con Cards
* Author: Emiliano Garcia
* Description: Plugin que pone un Tab/Nav con Cards
* Version: 1.0
*/
if( !function_exists("new_post_type")){
function new_post_type(){
register_post_type('cards_type', [
'labels' => ['name' => 'Cards', 'singular_name' => 'Card'],
'public' => true,
'has_archive' => false,
'menu_icon' => 'dashicons-images-alt2',
'supports' => ['title', 'thumbnail'],
'taxonomies' => ['category'],] );
}
}
add_action('init', 'new_post_type');
function mostrar_tabs($atts) {
$atts = shortcode_atts(array(
'categorias' => 'todos,humanidades,comunidad,cultura,gobierno',
'entradas' => 3
), $atts);
$entradas = intval($atts['entradas']);
// Se convierte la cadena de un texto en un arreglo
$categorias_array = explode(',', str_replace(' ', '', $atts['categorias']));
ob_start();
?>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php foreach ($categorias_array as $index => $cat): ?>
<li class="nav-item" role="presentation">
<button class="nav-link <?php echo ($index === 0) ? 'active' : ''; ?>"
id="<?php echo esc_attr($cat); ?>-tab"
data-bs-toggle="tab"
data-bs-target="#tab-<?php echo esc_attr($cat); ?>"
type="button"
role="tab"
aria-controls="tab-<?php echo esc_attr($cat); ?>"
aria-selected="<?php echo ($index === 0) ? 'true' : 'false'; ?>">
<?php echo esc_html(ucfirst($cat)); ?>
</button>
</li>
<?php endforeach; ?>
</ul>
<div class="tab-content mt-4" id="myTabContent">
<?php foreach ($categorias_array as $index => $cat): ?>
<div class="tab-pane fade <?php echo ($index === 0) ? 'show active' : ''; ?>"
id="tab-<?php echo esc_attr($cat); ?>"
role="tabpanel"
aria-labelledby="<?php echo esc_attr($cat); ?>-tab">
<div class="row row-cols-1 row-cols-md-3 g-4"> <?php
$args = array(
'post_type' => 'cards_type',
'posts_per_page' => $entradas,
'post_status' => 'publish'
);
if (strtolower($cat) !== 'todos') {
$categoria_obj = get_category_by_slug($cat);
if ($categoria_obj) {
$args['category'] = $categoria_obj->term_id;
} else {
$args['category'] = -1;
}
}
$posts = get_posts($args);
if ($posts) :
foreach ($posts as $post) :
$id = $post->ID;
$titulo = get_the_title($id);
$imagen = get_the_post_thumbnail_url($id, 'medium');
$descripcion = get_field('descripcion', $id);
$enlace = get_field('enlace', $id);
?>
<div class="col">
<div class="card h-100">
<?php if($imagen): ?>
<img src="<?php echo esc_url($imagen); ?>" class="card-img-top" alt="<?php echo esc_attr($titulo); ?>">
<?php endif; ?>
<div class="card-body">
<h5 class="card-title"><?php echo esc_html($titulo); ?></h5>
<p class="card-text"><?php echo esc_html($descripcion); ?></p>
<?php if($enlace): ?>
<a href="<?php echo esc_url($enlace); ?>" class="btn btn-primary">Ver más</a>
<?php endif; ?>
</div>
</div>
</div>
<?php
endforeach;
else :
echo '<div class="col-12"><p>No hay noticias en esta categoría.</p></div>';
endif;
?>
</div> </div> <?php endforeach; ?>
</div> <?php
return ob_get_clean();
}
add_shortcode('noticias','mostrar_tabs');