File: /home/wicomm2/public_html/pages/blog.php
<?php
/*
Template Name: Página do Blog
*/
get_header();
$banner_main = get_field('banner_principal');
$title = get_field('titulo');
$paged = max(1, get_query_var('paged') ? get_query_var('paged') : get_query_var('page'));
$current_category = isset($_GET['categoria']) ? sanitize_text_field($_GET['categoria']) : '';
$current_order = isset($_GET['ordem']) ? sanitize_text_field($_GET['ordem']) : 'recentes';
$order = 'DESC';
if ($current_order === 'antigos') {
$order = 'ASC';
}
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
'orderby' => 'date',
'order' => $order,
);
if (!empty($current_category) && $current_category !== 'todas') {
$args['category_name'] = $current_category;
}
$blog_query = new WP_Query($args);
$categories = get_categories(array(
'taxonomy' => 'category',
'hide_empty' => true,
));
?>
<main class="wc-blog-page">
<div class="wc-container">
<header class="wc-blog-page__header" style="background-image: url('<?php echo $banner_main ?>');">
<div class="wc-home-limit__screen">
<h1>Blog da Wicomm</h1>
<p><?php echo $title ?></p>
</div>
</header>
<div class="wc-blog-page__filters">
<div class="wc-blog-page__categories">
<ul class="wc-blog-page__categories-list">
<li>
<a href="<?php echo esc_url(add_query_arg(array(
'categoria' => 'todas',
'ordem' => $current_order,
'paged' => false,
), get_permalink())); ?>"
class="<?php echo (empty($current_category) || $current_category === 'todas') ? 'is-active' : ''; ?>">
Todas
</a>
</li>
<?php foreach ($categories as $category): ?>
<li>
<a href="<?php echo esc_url(add_query_arg(array(
'categoria' => $category->slug,
'ordem' => $current_order,
'paged' => false,
), get_permalink())); ?>"
class="<?php echo ($current_category === $category->slug) ? 'is-active' : ''; ?>">
<?php echo esc_html($category->name); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="wc-blog-page__order">
<form method="get" action="<?php echo esc_url(get_permalink()); ?>">
<input type="hidden" name="categoria" value="<?php echo esc_attr($current_category ?: 'todas'); ?>">
<label for="ordem" class="screen-reader-text">Ordenar posts</label>
<select name="ordem" id="ordem" onchange="this.form.submit()">
<option value="recentes" <?php selected($current_order, 'recentes'); ?>>
Mais recentes
</option>
<option value="antigos" <?php selected($current_order, 'antigos'); ?>>
Mais antigos
</option>
</select>
</form>
</div>
</div>
<?php if ($blog_query->have_posts()): ?>
<div class="wc-blog-page__grid">
<?php while ($blog_query->have_posts()):
$blog_query->the_post(); ?>
<?php get_template_part('template-parts/components/blog-card'); ?>
<?php endwhile; ?>
</div>
<?php
$prev_link = get_previous_posts_link('Anterior', $blog_query->max_num_pages);
$next_link = get_next_posts_link('Próximo', $blog_query->max_num_pages);
$total_pages = (int) $blog_query->max_num_pages;
?>
<?php if ($prev_link || $next_link || $total_pages > 1): ?>
<nav class="wc-pagination" aria-label="Paginação do blog">
<div class="wc-pagination__inner">
<div class="wc-pagination__prev">
<?php if ($prev_link): ?>
<?php echo $prev_link; ?>
<?php endif; ?>
</div>
<div class="wc-pagination__numbers">
<span class="wc-pagination__status">
Página <span><?php echo esc_html($paged); ?></span> de <span><?php echo esc_html($total_pages); ?>
</span>
</span>
</div>
<div class="wc-pagination__next">
<?php if ($next_link): ?>
<?php echo $next_link; ?>
<?php endif; ?>
</div>
</div>
</nav>
<?php endif; ?>
<?php else: ?>
<p>Nenhum post encontrado.</p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div>
</main>
<?php get_footer(); ?>