ADD pagination

This commit is contained in:
2025-10-08 10:38:06 +00:00
parent 5a0cd8a1be
commit df3086e62d
6 changed files with 323 additions and 175 deletions

View File

@ -27,7 +27,7 @@ $eusk_subpagina_zerbitzu = ($parent && ($parent->post_name === 'zerbitzu-pribatu
$subpagina_industria = ($parent && ($parent->post_name === 'industria'));
// Verificar si es una página padre de servicios
$slug_actual = $post->post_name;
//error_log('✅ Slug actual: ' . $slug_actual);
//error_log('✅ (page.php) Slug actual: ' . $slug_actual);
$pagina_servicios = ($slug_actual === 'servicios-privados' || $slug_actual === 'servicios-publicos' || $slug_actual === 'zerbitzu-pribatuak' || $slug_actual === 'zerbitzu-publikoak');
@ -35,15 +35,15 @@ if ($parent && ($parent->post_name === 'contactos' || $parent->post_name === 'ko
// subpáginas de contactos
Timber::render('templates/page-contactos.twig', $context);
} elseif ($pagina_servicios) {
//error_log('✅ Página padre de servicios: ' . $slug_actual);
//error_log('✅ (page.php) Página padre de servicios: ' . $slug_actual);
Timber::render('templates/page-servicios.twig', $context);
} elseif ( $cast_subpagina_servicios || $eusk_subpagina_zerbitzu ) {
error_log('✅ Página "Servicios": ' . $parent->post_name);
error_log('✅ (page.php) Página "Servicios": ' . $parent->post_name);
Timber::render('templates/single-servicios.twig', $context);
} elseif ( $subpagina_industria ) {
error_log('✅ Página "Industria": ' . $parent->post_name);
//error_log('✅ (page.php) Página "Industria": ' . $parent->post_name);
Timber::render('templates/single-industria.twig', $context);
} else {
error_log('✅ Página: ' . $parent->post_name);
//error_log('✅ (page.php) Página: ' . $parent->post_name);
Timber::render(array('templates/page-' . $timber_post->post_name . '.twig'), $context);
}

View File

@ -34,6 +34,7 @@ class StarterSite extends Site {
add_filter('timber/twig', [$this, 'add_videos_embed_to_twig']); //videos embed
add_action('pre_get_posts', array($this, 'exclude_pages_from_search')); //exclude pages with ACF
// función que reconoce los campos ACF en los diferentes idiomas.
add_action('init', function() {
if (function_exists('pll_current_language')) {
// Forzar que ACF reconozca el idioma actual
@ -43,6 +44,41 @@ class StarterSite extends Site {
}
});
// Arreglar paginación para post type "opinion"
// add_action('init', function() {
// // Asegurarse de que opinion tenga archive habilitado
// global $wp_post_types;
// if (isset($wp_post_types['opinion'])) {
// $wp_post_types['opinion']->has_archive = true;
// }
// });
// Añadir regla de rewrite personalizada para paginación de "opinion"
// add_action('init', function() {
// add_rewrite_rule(
// '^opinion/page/([0-9]{1,})/?$',
// 'index.php?post_type=opinion&paged=$matches[1]',
// 'top'
// );
// // Si usas multiidioma, añade también para cada idioma
// add_rewrite_rule(
// '^([a-z]{2})/opinion/page/([0-9]{1,})/?$',
// 'index.php?lang=$matches[1]&post_type=opinion&paged=$matches[2]',
// 'top'
// );
// }, 10);
// Forzar flush de rewrite rules (solo una vez)
// add_action('init', function() {
// if (get_option('opinion_pagination_flushed') !== 'yes') {
// flush_rewrite_rules();
// update_option('opinion_pagination_flushed', 'yes');
// }
// }, 999);
parent::__construct();
}
@ -214,7 +250,7 @@ class StarterSite extends Site {
}
//section "portada> ENLACES"
private function getBotonesImagen($page_id = null) {
private function getBotonesEnlaces($page_id = null) {
if (is_null($page_id)) {
$page_id = get_the_ID();
@ -240,14 +276,55 @@ class StarterSite extends Site {
return $botones;
}
//Get last 3 articles with category "noticias"
private function recentPostsNews() {
return Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => -1,
'category_name' => 'noticias',
'post_status' => 'publish'
]);
//Get POSTS with category "noticias"
private function getPostsNews() {
$post = Timber::get_post();
// if ($post) { error_log('✅ Este es el ID Página news: ' . $post->ID);}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//84=portada CAST & 331= portada EUSKARA
if ( $post && ( $post->ID === 84 || $post->ID === 331) ) {
return [
'posts' => Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => -1,
'category_name' => 'noticias',
'post_status' => 'publish'
]),
'pagination' => null
];
//8=noticias CAST & 339= noticias EUSKARA
} elseif ( $post && ( $post->ID === 8 || $post->ID === 339) ) {
global $wp_query;
$temp_query = $wp_query;
$wp_query = new \WP_Query([
'post_type' => 'post',
'category_name' => 'noticias',
'posts_per_page' => 10,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
]);
// Timber::get_posts() SIN PARÁMETROS usa la query global ($wp_query)
$posts = Timber::get_posts(); // Lee de la query global que acabamos de crear
$pagination = new \Timber\Pagination($wp_query->max_num_pages);
$wp_query = $temp_query; // Restaurar query original
return [
'posts' => $posts,
'pagination' => $pagination
];
}
return ['posts' => [], 'pagination' => null];
}
private function getIconsRRSS() {
@ -287,68 +364,96 @@ class StarterSite extends Site {
}
private function getVideos() {
return Timber::get_posts([
'post_type' => 'videos',
'posts_per_page' => -1, // -1 = sin límite
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]);
$post = Timber::get_post();
//error_log('✅ ID Página: ' . $post->ID);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($post && ( $post->ID === 84 || $post->ID === 331) ) {
return [
'posts' => Timber::get_posts([
'post_type' => 'videos',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]),
'pagination' => null
];
} elseif ($post && ( $post->ID === 9 || $post->ID === 392) ) {
global $wp_query;
$temp_query = $wp_query;
$wp_query = new \WP_Query([
'post_type' => 'videos',
'posts_per_page' => 12,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
]);
// Timber::get_posts() SIN PARÁMETROS usa la query global ($wp_query)
$posts = Timber::get_posts(); // Lee de la query global que acabamos de crear
$pagination = new \Timber\Pagination($wp_query->max_num_pages);
$wp_query = $temp_query; // Restaurar query original
return [
'posts' => $posts,
'pagination' => $pagination
];
}
return ['posts' => [], 'pagination' => null];
}
private function getUltimoArticuloOpinion() {
return Timber::get_posts([
'post_type' => 'opinion',
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]);
private function getOpinion() {
// $current_lang = pll_current_language();
// // Consulta con filtro de idioma
// $posts = Timber::get_posts([
// 'post_type' => 'opinion',
// 'posts_per_page' => 1,
// 'post_status' => 'publish',
// 'orderby' => 'date',
// 'order' => 'DESC',
// 'lang' => $current_lang // ← Filtro por idioma
// ]);
$post = Timber::get_post();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//84=portada CAST & 331= portada EUSKARA
if ( $post && ( $post->ID === 84 || $post->ID === 331) ) {
return Timber::get_posts([
'post_type' => 'opinion',
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
]);
//2011=opinion CAST & 10962=opinion EUSKARA
} elseif ( $post && ($post->ID === 2011 || $post->ID === 10962) ) {
global $wp_query;
$temp_query = $wp_query;
// // Debug temporal - quítalo después
// $debug_all = Timber::get_posts([
// 'post_type' => 'opinion',
// 'posts_per_page' => -1,
// 'post_status' => 'publish'
// ]);
$wp_query = new \WP_Query([
'post_type' => 'opinion',
'posts_per_page' => 10,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
]);
// Timber::get_posts() SIN PARÁMETROS usa la query global ($wp_query)
$posts = Timber::get_posts(); // Lee de la query global que acabamos de crear
$pagination = new \Timber\Pagination($wp_query->max_num_pages);
$wp_query = $temp_query; // Restaurar query original
return $posts;
}
// $debug_spanish = Timber::get_posts([
// 'post_type' => 'opinion',
// 'posts_per_page' => -1,
// 'post_status' => 'publish',
// 'lang' => 'es'
// ]);
// $debug_euskera = Timber::get_posts([
// 'post_type' => 'opinion',
// 'posts_per_page' => -1,
// 'post_status' => 'publish',
// 'lang' => 'eu'
// ]);
// // Logging temporal
// error_log("=== DEBUG ARTICULOS OPINION ===");
// error_log("Idioma actual: " . $current_lang);
// error_log("Total artículos opinion: " . count($debug_all));
// error_log("Artículos en español: " . count($debug_spanish));
// error_log("Artículos en euskera: " . count($debug_euskera));
// error_log("Resultado consulta actual: " . count($posts));
// return $posts;
return ['posts' => [], 'pagination' => null];
}
//Get portada > Campañas
private function getCampanaArticles() {
return Timber::get_posts([
'post_type' => 'campana',
@ -359,6 +464,18 @@ class StarterSite extends Site {
]);
}
//Get portada > Conflictos (category)
private function getConflictsCategory() {
return Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 1,
'post_status' => 'publish',
'category_name' => 'conflictos laborales',
'orderby' => 'date',
'order' => 'DESC'
]);
}
public function generar_embed_universal($url) {
if (empty($url)) {
return false;
@ -433,8 +550,8 @@ class StarterSite extends Site {
'lang' => '',
]);
//error_log('✅ Número de posts "Documentos" encontrados: ' . count($posts));
$posts = Timber::get_posts($args);
//error_log('✅ Número de posts "Documentos" encontrados: ' . count($posts));
$documentos = [];
@ -466,13 +583,13 @@ class StarterSite extends Site {
/** Obetiene campos de "Enlaces" */
private function getEnlaces() {
$grupos = [];
// Buscar página por su slug & Cambia dinámicamente según idioma
$page = get_page_by_path( pll_current_language() === 'eu' ? 'loturak' : 'enlaces' );
//error_log('✅ Página "enlaces" encontrada con ID: ' . $page->ID);
if (!$page) {
error_log('🔴 Página "enlaces" no encontrada con get_page_by_path().');
return [];
@ -529,65 +646,8 @@ class StarterSite extends Site {
return $grupos;
}
// Get subpages of "servicios publicos"
private function getPaginasServiciosPublicos() {
$pag_servicios_publicos = get_page_by_path('accion-sindical/servicios-publicos');
if (!$pag_servicios_publicos) {
error_log('🔴 Página "Servicios públicos" no encontrada.');
return [];
} else {
//error_log('✅ Página "Servicios públicos" encontrada.');
}
$subpaginas = Timber::get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_servicios_publicos->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
foreach ($subpaginas as $pagina) {
//$pagina->imagen = $pagina->thumbnail ? $pagina->thumbnail->src('medium') : null;
$pagina->thumbnail= get_the_post_thumbnail_url($pagina->ID);
}
return $subpaginas;
}
// Get subpages of "servicios privados"
private function getPaginasServiciosPrivados() {
$pag_servicios_privados = get_page_by_path('accion-sindical/servicios-privados');
if (!$pag_servicios_privados) {
error_log('🔴 Página "Servicios privados" no encontrada.');
return [];
} else {
//error_log('✅ Página "Servicios privados" encontrada.');
}
$subpaginas = Timber::get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_servicios_privados->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
foreach ($subpaginas as $pagina) {
//$pagina->imagen = $pagina->thumbnail ? $pagina->thumbnail->src('medium') : null;
$pagina->thumbnail= get_the_post_thumbnail_url($pagina->ID);
}
return $subpaginas;
}
private function getSubpaginasServicios() {
// Detectar automáticamente si es servicios públicos o privados
$current_post = get_post();
$slug_actual = $current_post->post_name;
@ -599,46 +659,77 @@ class StarterSite extends Site {
} elseif ($slug_actual === 'servicios-privados') {
$path = 'accion-sindical/servicios-privados';
$tipo = 'privados';
} elseif ($slug_actual === 'zerbitzu-publikoak') {
$path = 'ekintza-sindikala/zerbitzu-publikoak';
$tipo = 'publikoak';
} elseif ($slug_actual === 'zerbitzu-pribatuak') {
$path = 'ekintza-sindikala/zerbitzu-pribatuak';
$tipo = 'pribatuak';
} else {
return [];
return [
'subpaginas' => [],
'pagination' => null
];
}
$pag_servicios = get_page_by_path($path);
if (!$pag_servicios) {
error_log("🔴 Página '$path' no encontrada.");
return [];
return [
'subpaginas' => [],
'pagination' => null
];
}
// Obtener el número de página actual
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 6;
$subpaginas = Timber::get_posts([
// Guardar la query global actual
global $wp_query;
$temp_query = $wp_query;
// Crear nueva WP_Query
$wp_query = new \WP_Query([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_servicios->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
'posts_per_page' => $posts_per_page,
'paged' => $paged,
]);
// Timber::get_posts() SIN PARÁMETROS usa la query global ($wp_query)
$subpaginas = Timber::get_posts();
foreach ($subpaginas as $pagina) {
$pagina->thumbnail = get_the_post_thumbnail_url($pagina->ID);
$pagina->tipo_servicio = $tipo; // Para usar en la plantilla si necesitas distinguir
$pagina->tipo_servicio = $tipo;
}
return $subpaginas;
// Crear objeto de paginación
$pagination = new \Timber\Pagination($wp_query->max_num_pages);
// Restaurar query original
$wp_query = $temp_query;
return [
'subpaginas' => $subpaginas,
'pagination' => $pagination
];
}
//Campos de contactos
public function getContactoFields() {
$post = Timber::get_post();
if (!$post) {
error_log('🔴 No hay páginas de contactos.');
return []; // devolver un array vacío para evitar el error
}
if (!$post) { return []; }
$form_shortcode = get_field('etiqueta_formulario', $post->ID); // o el campo ACF donde esté el shortcode
//error_log('✅ ID Página "Contactos": ' . $post->ID);
$contactsFields = [
'nombre' => get_field('nombre_contacto', $post->ID),
'direccion' => get_field('direccion', $post->ID),
@ -705,27 +796,65 @@ class StarterSite extends Site {
return $polylang_data;
}
private function getPostIndustry() {
$pag_industry = get_page_by_path('accion-sindical/industria');
if (!$pag_industry) {
error_log('🔴 Página "Industria" no encontrada.');
return [];
} else {
//error_log('✅ Página "Industria" encontrada.');
}
$subpaginas = Timber::get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_industry->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
]);
foreach ($subpaginas as $pagina) {
//$pagina->imagen = $pagina->thumbnail ? $pagina->thumbnail->src('medium') : null;
$pagina->thumbnail= get_the_post_thumbnail_url($pagina->ID);
}
return $subpaginas;
}
//Global variables
public function add_global_context($context) {
$context['footer_1'] = get_field('footer_text_1', 'option');
$context['footer_2'] = get_field('footer_text_2', 'option');
$context['recent_posts_noticias'] = $this->recentPostsNews();
$context['botones_imagen'] = $this->getBotonesImagen(); //ENLACES
$context['botones_imagen'] = $this->getBotonesEnlaces();
$context['redes_sociales'] = $this->getIconsRRSS();
$context['logos_header'] = $this->getLogosHeader();
$context['videos'] = $this->getVideos();
$context['ultimo_articulo_opinion'] = $this->getUltimoArticuloOpinion();
$context['posts_opinion'] = $this->getOpinion();
$context['posts_campana'] = $this->getCampanaArticles();
$context['paginas_acerca'] = $this->getPaginasAcerca();
$context['posts_conflicts'] = $this->getConflictsCategory();
$context['documentos'] = $this->getDocumentos();
$context['enlaces'] = $this->getEnlaces();
$context['subpaginas_servicios_publicos'] = $this->getPaginasServiciosPublicos();
$context['subpaginas_servicios_privados'] = $this->getPaginasServiciosPrivados();
//$context['subpaginas_servicios'] = $this->getSubpaginasServicios();
$context['contacto_info'] = $this->getContactoFields();
//$context['afiliate_fields'] = $this->getAfiliateFields();
$context['posts_industry'] = $this->getPostIndustry();
$context = array_merge($context, $this->getPolylangData());
$context['subpaginas_servicios'] = $this->getSubpaginasServicios();
$videos_data = $this->getVideos();
$context['videos'] = $videos_data['posts'];
$context['pagination'] = $videos_data['pagination'];
$post_noticias_data = $this->getPostsNews();
$context['posts_noticias'] = $post_noticias_data ['posts'];
$context['pagination'] = $post_noticias_data['pagination'];
$servicios_data = $this->getSubpaginasServicios();
$context['subpaginas_servicios'] = $servicios_data['subpaginas'];
$context['pagination'] = $servicios_data['pagination'];
return $context;
}

View File

@ -2,7 +2,7 @@
<div class="row align-items-stretch">
{# Primera columna - Carousel con vídeos embebidos #}
<div class="col-lg-8 col-12 mb-4 mb-lg-0">
<div class="col-lg-8 col-12 mb-4 mb-lg-0 order-2 order-lg-1 mt-5 mt-lg-0">
<h1 class="mb-4 position-relative fs-4 fw-bold sp-module-title">{{ __('Vídeos', 'Sindikatua')|upper }}</h1>
{% if videos %}
@ -115,7 +115,7 @@
</div>
{# Segunda columna - Último artículo de opinión #}
<div class="col-lg-4 col-12 opinion-article d-flex flex-column">
<div class="col-lg-4 col-12 opinion-article d-flex flex-column order-1 order-lg-2 ">
<h1 class="mb-4 position-relative fs-4 fw-bold sp-module-title">{{ __('Opinión', 'Sindikatua')|upper}}</h1>
<div class="flex-grow-1 d-flex">

View File

@ -53,8 +53,9 @@
<h2>no hay artículos</h2>
{% endif %}
{% set pagination = posts_opinion.pagination %}
<!-- PAGINATION -->
{% set pagination = posts_opinion.pagination %}
<div class="pagination-custom mt-5">
{% include 'partials/pagination-custom.twig' %}
</div>

View File

@ -73,7 +73,7 @@
<div class="row align-items-stretch">
{# Campañas #}
<div class="col-6">
<div class="col-12 col-sm-12 col-md-6">
<h1 class="mb-4 position-relative fs-4 fw-bold sp-module-title">{{ __('Campañas', 'Sindikatua')|upper }}</h1>
{% if posts_campana and posts_campana|length > 0 %}
@ -95,7 +95,7 @@
</div>
{# conflictos #}
<div class="col-6">
<div class="col-12 col-sm-12 col-md-6 mt-5 mt-md-0">
<h1 class="mb-4 position-relative fs-4 fw-bold sp-module-title">{{ __('Conflictos Laborales', 'Sindikatua')|upper }}</h1>
{% if posts_conflicts and posts_conflicts|length > 0 %}

View File

@ -4,32 +4,50 @@
<h1 class="mb-5 position-relative fs-4 fw-bold sp-module-title">{{ post.title }}</h1>
<section class="grid servicios servicios-publicos {{ post.post_name }}">
<div class="container">
<div class="row">
{% for pagina in subpaginas_servicios %}
<div class="col-12 mb-2">
<article>
<div class="card">
{% if pagina.thumbnail %}
<img src="{{ pagina.thumbnail.src}}"
alt="{{ pagina.thumbnail.alt }}"
class="card-img-top">
{% else %}
<p>no image</p>
{% endif %}
<div class="card-body">
<a href="{{ pagina.link }}" class="text-decoration-none">
<h5 href="{{ pagina.link }}" class="card-title fs-4 text-dark link-underline-animate">{{ pagina.title }}</h5>
</a>
<p class="card-text">{{ pagina.content }}</p>
{% if subpaginas_servicios |length > 0 %}
{% for pagina in subpaginas_servicios %}
<div class="col-12 mb-2">
<article>
<div class="card">
{% if pagina.thumbnail %}
<img src="{{ pagina.thumbnail.src}}"
alt="{{ pagina.thumbnail.alt }}"
class="card-img-top">
{% endif %}
<div class="card-body">
<a href="{{ pagina.link }}" class="text-decoration-none">
<h5 href="{{ pagina.link }}" class="card-title fs-4 text-dark link-underline-animate">{{ pagina.title }}</h5>
</a>
{# <p class="card-text">{{ pagina.content }}</p> #}
<p class="card-text"> {{
pagina.excerpt({
words: 50,
read_more: ''
})
}}</p>
</div>
</div>
</div>
</article>
</div>
</article>
</div>
{% else %}
<p>No hay subpáginas.</p>
{% endfor %}
</div>
<!-- PAGINATION -->
{% if pagination %}
<div class="pagination-custom mt-5">
{% include 'partials/pagination-custom.twig' %}
</div>
{% endif %}
{% else %}
<p>No hay subpáginas.</p>
{% endfor %}
</div>
<p>No hay servicios disponibles en esta sección.</p>
{% endif %}
</div>
</section>