Voici comment créer une page d’archives facilement et sans plugin !

Fonction d’archivage d’articles pour WordPress

Copier coller le code suivant en functions.php :

function astuceswp_archiveData()
{
global $wpdb;


$data = [];
$day = intval(get_query_var('dayNb'));
$month = intval(get_query_var('monthNb'));
$year = intval(get_query_var('yearNb'));


if ($year !== 0 && $month !== 0 && $day !== 0) {

$query = getQuery(
[
'date_query' => [
'year' => $year,
'month' => $month,
'day' => $day,
]
],
-1
);


$data = [
'posts' => $query['posts'],
'year' => $year,
'month_name' => monthNbToFrenchName($month),
'month_nb' => $month,
'day' => $day < 10 ? '0' . $day : $day
];
} elseif ($year !== 0 && $month !== 0) {
$data = [
'days' => $wpdb->get_col("SELECT DISTINCT DAY(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '" . $year . "' AND MONTH(post_date) = '" . $month . "' AND post_type in ('post', 'pays') ORDER BY post_date DESC"),
'year' => $year,
'month_name' => monthNbToFrenchName($month),
'month_nb' => $month,
'nb_days_in_month' => date("t", mktime(0, 0, 0, $year, 1, $month)),
];
} else {
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type in ('post', 'pays') ORDER BY post_date DESC");
foreach ($years as $year) {
$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '" . $year . "' AND post_type in ('post', 'pays') ORDER BY post_date ASC");
$data[$year] = $months;
}
}

return $data;
}

Templates Parts

Template Jour

<?php 
/**
* day
*
*/

$archive_data = astuceswp_archiveData();
$blogposts = new WP_query($archive_data);
?>


<h1>ARCHIVES DE <?php echo $archive_data['month_name']; ?> <?php echo $archive_data['year']; ?></h1>
<h2>Articles du <?php echo $archive_data['day']; ?> <?php echo $archive_data['month_name']; ?> <?php echo $archive_data['year']; ?></h2>
<ul class="row">
<?php 
if ( $blogposts->have_posts() ) : 
while ($blogposts->have_posts()) : $blogposts->the_post();
// Get loop markup
get_template_part( 'template-parts/loop/content', 'blogposts' );
// Close blogposts loop
endwhile;
endif;




?>
</ul>

Template Mois

<?php 
/**
* month
*
*/

$archive_data = astuceswp_archiveData();

?>

<h1>ARCHIVES DE <?php echo $archive_data['month_name']; ?><?php echo $archive_data['year']; ?></h1>
<div class="row">
<?php for($day = 1; $day <= $archive_data['nb_days_in_month']; $day++):
if($day === 1 || $day % 6 == 1):?>
<ul class="col-lg-2 col-md-4 col-sm-4">
<?php endif; ?>
<li>
<?php if(in_array($day, $archive_data['days'])){?>
<a href="<?php echo ('/archive-par-date-taxonomies/' . $archive_data['year'] . '/' . $archive_data['month_nb'] . '/' . $day . '/');?>">

<?php
echo $day. ' '. $archive_data['month_name']. ' '.$archive_data['year'] . '</a>';
} else {
echo $day. ' '. $archive_data['month_name']. ' '.$archive_data['year'] . '';

} ?>
</li>
<?php if($day % 6 == 0):?>
</ul>
<?php endif; 
endfor;
if($day % 6 != 1):?>
</ul>
<?php endif; ?>
</div>

Template Archives pages

<?php 
/**
* Template Name: Archives
*
*/

get_header();
the_post();

function whichTemplateToUse()
{
$day = intval(get_query_var('dayNb'));
$month = intval(get_query_var('monthNb'));
$year = intval(get_query_var('yearNb'));

if ($year !== 0 && $month !== 0 && $day !== 0) {
$return = 'day';
} elseif ($year !== 0 && $month !== 0) {
$return = 'month';
} else {
$return = 'noDate';
}
return $return;
}


function monthNbToFrenchName($monthNb)
{
$dateObj = DateTime::createFromFormat('!m', $monthNb);
$monthName = $dateObj->format('F');

return str_replace(
array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
array('Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'),
$monthName
);
}

$archive_data = astuceswp_archiveData();
$which_template_to_use = whichTemplateToUse();
?>
<div class="container archive-seo padding-header">
<?php if($which_template_to_use === 'day') {
get_template_part( 'template-parts/archives/day'); /*Chemin de vos templates parts*/
}
elseif($which_template_to_use === 'month'){ 
get_template_part( 'template-parts/archives/month'); /*Chemin de vos templates parts*/
}else{?>
<h1>ARCHIVES DES ARTICLES</h1>

<div class="row">
<?php foreach($archive_data as $yearNb => $months):?>
<div class="col-lg-2 col-md-4 col-sm-4">
<h3><?php echo $yearNb; ?></h3>
<ul>
<?php for($monthNb = 1; $monthNb <= 12; $monthNb++) :
$dateObj = DateTime::createFromFormat('!m', $monthNb);
$monthName = str_replace(
array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
array('Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'),
$dateObj->format('F')
);
?>
<li>
<?php if(in_array($monthNb, $months)){?>
<a href="<?php echo('/archive-par-date-taxonomies/' . $yearNb . '/' . $monthNb . '/'); ?>"><?php echo $monthName; ?></a>
<?php } else{
echo $monthName; 
} ?>
</li>
<?php endfor; ?>
</ul>
</div>
<?php endforeach; ?>
</div>

<?php }?>
</div>

<?php
get_footer();
0 0 votes
Article Rating
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Commentaires en ligne
Afficher tous les commentaires