<?php

/**
 * PAGINATION ACTUALITES URL REWRITING
 */


function lp_pagination_rewrite_rule()
{
    add_rewrite_rule('(magazine|magazin|revista)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
}
add_action('init', 'lp_pagination_rewrite_rule', 10, 0);

function lp_post_rewrite_rule()
{
    add_rewrite_rule('(magazine|magazin|revista)/([^/]+)/([^/]+)/?$', 'index.php?name=$matches[3]', 'top');
}
add_action('init', 'lp_post_rewrite_rule', 10, 0);

function lp_pagination_add_query_vars($query_vars)
{
    $query_vars[] = 'paged';
    return $query_vars;
}
add_filter('query_vars', 'lp_pagination_add_query_vars');


function lp_permalink_prefix($url, $post)
{
    // Vérifier si le post est de type article
    if ($post->post_type == 'post') {
        // Récupérer la catégorie principale du post
        $category = get_the_category($post->ID);
        if ($category) {
            $category_slug = $category[0]->slug;
            $url = get_category_link($category[0]->term_id) . $post->post_name . '/';
            // Ajouter le préfixe et la catégorie au permalien
            // $url = trailingslashit(home_url('/actualites/' . $category_slug . '/' . $post->post_name));
        }
    }
    return $url;
}
add_filter('post_link', 'lp_permalink_prefix', 10, 2);
