FIX pagination

This commit is contained in:
2025-10-09 10:32:56 +00:00
parent 77376d301b
commit d1e5638ed2
7 changed files with 121 additions and 100 deletions

View File

@@ -44,39 +44,81 @@ 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;
// }
// });
//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'
// );
//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);
// 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);
//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);
/** POST "QUÉ ES LA CNT" */
// Previene borrar el post "Qué es la cnt" porque forma parte del menú //
$ids_especiales = [10982, 4791, 10981];
define('POST_ESPECIAL_TYPE', 'acerca');
// Añadir clase personalizada al tr del listado de posts
add_filter('post_class', function($classes, $class, $post_id) use ($ids_especiales) {
if (in_array($post_id, $ids_especiales)) {
$classes[] = 'post-especial';
}
return $classes;
}, 10, 3);
// Añadir estilo en el admin
add_action('admin_head', function() use ($ids_especiales) {
if (empty($ids_especiales)) return;
$css = '';
foreach ($ids_especiales as $id) {
$css .= "tr#post-$id td { background-color: #fffae6 !important; border-top: 2px solid #f0c000 !important; }";
}
echo "<style>$css</style>";
});
// Ocultar el enlace "Mover a la papelera" en el listado
add_filter('post_row_actions', function($actions, $post) use ($ids_especiales) {
if ($post->post_type === POST_ESPECIAL_TYPE && in_array($post->ID, $ids_especiales)) {
unset($actions['trash']);
}
return $actions;
}, 10, 2);
// Mensaje de advertencia en los posts
add_filter('display_post_states', function($post_states, $post) use ($ids_especiales) {
if ($post->post_type === 'acerca' && in_array($post->ID, $ids_especiales)) {
$post_states['protegido'] = '🔒 No se puede borrar';
}
return $post_states;
}, 10, 2);
/** FIN *************POST "QUÉ ES LA CNT" */
parent::__construct();
@@ -280,8 +322,7 @@ class StarterSite extends Site {
private function getPostsNews() {
$post = Timber::get_post();
// if ($post) { error_log('✅ Este es el ID Página news: ' . $post->ID);}
//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
@@ -302,7 +343,7 @@ class StarterSite extends Site {
$wp_query = new \WP_Query([
'post_type' => 'post',
'category_name' => 'noticias',
'category_name' => 'noticias',
'posts_per_page' => 10,
'post_status' => 'publish',
'orderby' => 'date',
@@ -310,20 +351,19 @@ class StarterSite extends Site {
'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
];
$pagination = new \Timber\Pagination();
$wp_query = $temp_query; // Restaurar query original
return $posts;
}
return ['posts' => [], 'pagination' => null];
//error_log('❌ getPostsNews - No coincide con ninguna página');
return [
'posts' => [],
'pagination' => null
];
}
@@ -387,7 +427,7 @@ class StarterSite extends Site {
$wp_query = new \WP_Query([
'post_type' => 'videos',
'posts_per_page' => 12,
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
@@ -396,15 +436,10 @@ class StarterSite extends Site {
// 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;
}
return ['posts' => [], 'pagination' => null];
@@ -446,7 +481,6 @@ class StarterSite extends Site {
$wp_query = $temp_query; // Restaurar query original
return $posts;
}
return ['posts' => [], 'pagination' => null];
@@ -646,9 +680,9 @@ class StarterSite extends Site {
return $grupos;
}
/** Servicios públicos y privados */
private function getSubpaginasServicios() {
// Detectar automáticamente si es servicios públicos o privados
$current_post = get_post();
$slug_actual = $current_post->post_name;
@@ -668,7 +702,7 @@ class StarterSite extends Site {
} else {
return [
'subpaginas' => [],
'pagination' => null
'pagination_Servicios' => null
];
}
@@ -677,20 +711,17 @@ class StarterSite extends Site {
error_log("🔴 Página '$path' no encontrada.");
return [
'subpaginas' => [],
'pagination' => null
'pagination_Servicios' => null
];
}
// Obtener el número de página actual
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Obtener el número de página actual
$posts_per_page = 6;
// Guardar la query global actual
global $wp_query;
global $wp_query; // Guardar la query global actual
$temp_query = $wp_query;
// Crear nueva WP_Query
$wp_query = new \WP_Query([
$wp_query = new \WP_Query([ // Crear nueva WP_Query
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $pag_servicios->ID,
@@ -700,24 +731,17 @@ class StarterSite extends Site {
'paged' => $paged,
]);
// Timber::get_posts() SIN PARÁMETROS usa la query global ($wp_query)
$subpaginas = Timber::get_posts();
$subpaginas = Timber::get_posts(); // Timber::get_posts() SIN PARÁMETROS usa la query global ($wp_query)
foreach ($subpaginas as $pagina) {
$pagina->thumbnail = get_the_post_thumbnail_url($pagina->ID);
$pagina->tipo_servicio = $tipo;
}
// Crear objeto de paginación
$pagination = new \Timber\Pagination($wp_query->max_num_pages);
// Restaurar query original
$wp_query = $temp_query;
$pagination = new \Timber\Pagination($wp_query->max_num_pages); // Crear objeto de paginación
$wp_query = $temp_query; // Restaurar query original
return $subpaginas;
return [
'subpaginas' => $subpaginas,
'pagination' => $pagination
];
}
//Campos de contactos
@@ -838,24 +862,14 @@ class StarterSite extends Site {
$context['posts_conflicts'] = $this->getConflictsCategory();
$context['documentos'] = $this->getDocumentos();
$context['enlaces'] = $this->getEnlaces();
//$context['subpaginas_servicios'] = $this->getSubpaginasServicios();
$context['contacto_info'] = $this->getContactoFields();
$context['posts_industry'] = $this->getPostIndustry();
$context['posts_noticias'] = $this->getPostsNews();
$context['subpaginas_servicios'] = $this->getSubpaginasServicios();
$context['videos'] = $this->getVideos();
$context = array_merge($context, $this->getPolylangData());
$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;
}