Wordpress - Custom Post Type and New Taxonomy - Display records from selected category only

Asked

Viewed 308 times

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(); ?>

1 answer

0

You need to create a file called taxonomy-downloads.php, follow below the content it should contain:

<div class="container">
                    <div class="row">
                        <div class="col-md-12">
                            <center><?php the_archive_title('<h1 id="title" class="archive-title">','</h1>'); ?></center>
                            <p><?php the_archive_description(); ?></p>
                        </div>  
                        <?php

                            if(have_posts()) :
                                while (have_posts()) : the_post();  
                        ?>

                        <div class="col-sm-12 col-md-6 col-lg-6 col-xl-3">
                                <div class="card border-1">
                                    <div class="image">
                                        <center><?php the_post_thumbnail(); ?></center>
                                    </div>
                                    <div class="card-body">
                                        <div class="info">
                                            <p><?php the_title(); ?></p>
                                        </div>
                                        <div class="contentcurso">
                                            <p class="content"><?php the_content(); ?></p>
                                        </div>
                                    </div>
                                </div>
                            <br/>
                        </div>

                    <?php

                    endwhile;

                    ?>
                    <div class="row">
                        <div class="container">
                        <?php the_posts_pagination(
                            array(
                                'prev_text' => 'Anterior',
                                'next_text' => 'Próximo',
                                'screen_reader_text' => ' '
                            )
                        ); ?>
                        </div>
                    </div>
                    <?php

                    wp_reset_postdata();

                    // Caso não encontre nenhum post                

                    else:
                        echo "Nada encontrado";
                    endif;

                    ?>


                    </div>                  
                </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.