1
I created a custom post type called "downloads" and created categories for these using register_taxonomy() called downloads-Category.
In WP-Admin, I managed to register all my files and separate them by categories. It’s working fine. However, on the site, I could not do this filtering. See the link (https://ideia.marfin.co/downloads/), when clicking on any category, no filter is performed.
I don’t know where I went wrong! You can help me?
functions.php
register_post_type('downloads',
array(
'labels' => array(
'name' => __('Downloads'),
'featured_image' => __('Imagem Capa'),
'singular_name' => __('Download'),
),
'taxonomies' => array('downloads-category'),
'supports' => array(
'title',
'editor',
'custom-fields',
'thumbnail'
),
'menu_icon' => 'dashicons-category',
'with_front' => true,
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'downloads'),
)
);
$labels = array(
'name' => _x('Categorias', 'taxonomy general name'),
'singular_name' => _x('Categoria', 'taxonomy singular name'),
'search_items' => __('Procurar categoria'),
'all_items' => __('Todos'),
'edit_item' => __('Editar categoria'),
'update_item' => __('Editar categoria'),
'add_new_item' => __('Adicionar categoria'),
'new_item_name' => __('Nova categoria')
);
register_taxonomy('downloads-category',array('downloads'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'show_in_nav_menus' => true,
'rewrite' => array('slug' => 'categoria-de-downloads', 'with_front' => false),
));
Archive.php
<?php get_header(); ?>
<?php if(is_post_type_archive('downloads')){ ?>
<section id="conteudo">
<div class="title">
<div class="container">
<h1>Downloads</h1>
<?php if (function_exists('yoast_breadcrumb')){ yoast_breadcrumb('<p id="breadcrumbs">','</p>'); }?>
</div>
</div>
<div class="content-master">
<div class="container">
<div class="row more-gutter">
<aside class="col-sm-3">
<?php
$args = array(
'orderby' => 'name',
'taxonomy'=>'downloads-category',
'parent' => 0
);
$categories = get_categories($args);
foreach ($categories as $category){
echo '<li><a href="'.get_category_link($category->term_id).'">'.$category->name.'</a></li>';
}
?>
</aside>
<div class="col-sm-9">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$infoDownload = array(
'post_type' => 'downloads',
'paged' => $paged
);
$resultadoDownload = new WP_Query($infoDownload);
if ($resultadoDownload->have_posts()) :
while ($resultadoDownload->have_posts()) : $resultadoDownload->the_post();
$postThumb = (has_post_thumbnail()) ? get_the_post_thumbnail_url() : get_stylesheet_directory_uri()."/img/layout/sem-imagem.jpg";
$postThumb = "<img src=\"".$postThumb."\" class=\"img-fluid\">";
?>
<div class="row lista-download">
<div class="col-sm-4">
<?php echo $postThumb; ?>
</div>
<div class="col-sm-8">
<h3><?php the_title() ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php
wp_reset_postdata();
endwhile;
wpbs_pagination();
endif;
?>
</div>
</div>
</div>
</div>
</section>
<?php }elseif(is_tax('downloads-category')){ ?>
<section id="conteudo">
<div class="title">
<div class="container">
<h1>Nome da categoria</h1>
<?php if (function_exists('yoast_breadcrumb')){ yoast_breadcrumb('<p id="breadcrumbs">','</p>'); }?>
</div>
</div>
<div class="content-master">
<div class="container">
<div class="row more-gutter">
<aside class="col-sm-3">
<?php
$args = array(
'orderby' => 'name',
'taxonomy'=>'downloads-category',
'parent' => 0
);
$categories = get_categories($args);
foreach ($categories as $category){
echo '<li><a href="'.get_category_link($category->term_id).'">'.$category->name.'</a></li>';
}
?>
</aside>
<div class="col-sm-9">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$infoDownload = array(
'post_type' => 'downloads',
'tax_query' => array(
array(
'taxonomy'=>'downloads-category',
'field' => 'slug'
)
),
'paged' => $paged
);
$resultadoDownload = new WP_Query($infoDownload);
if ($resultadoDownload->have_posts()) :
while ($resultadoDownload->have_posts()) : $resultadoDownload->the_post();
$postThumb = (has_post_thumbnail()) ? get_the_post_thumbnail_url() : get_stylesheet_directory_uri()."/img/layout/sem-imagem.jpg";
$postThumb = "<img src=\"".$postThumb."\" class=\"img-fluid\">";
?>
<div class="row lista-download">
<div class="col-sm-4">
<?php echo $postThumb; ?>
</div>
<div class="col-sm-8">
<h3><?php the_title() ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php
wp_reset_postdata();
endwhile;
wpbs_pagination();
endif;
?>
</div>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>