• Resolvido brunobincoletto

    (@brunobincoletto)


    Tenho a seguinte situação.

    Preciso fazer um cadastro de produtos, onde terei apenas nome, descrição, linha e coleção.

    Então criei um custom post type para o nome e descrição e uma taxonomia para cadastrar as coleções e linha.

    Segue o código abaixo.

    <?php
    
    add_action( 'init', 'watch_type' );
    
    function watch_type() {
    
        $labels = array(
            'name' => _x('Watches', 'post type general name'),
            'singular_name' => _x('Watch', 'post type singular name'),
            'add_new' => _x('Add New', 'Watch'),
            'add_new_item' => __('Add New Watch'),
            'edit_item' => __('Edit Watch'),
            'new_item' => __('New Watch'),
            'all_items' => __('All Watches'),
            'view_item' => __('View Watch'),
            'search_items' => __('Search Watches'),
            'not_found' =>  __('No Watches found'),
            'not_found_in_trash' => __('No Watches found in Trash'),
            'parent_item_colon' => '',
            'menu_name' => 'Watches'
        );
    
        register_post_type( 'watch', array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'has_archive' => false,
            'rewrite' => array(
             'slug' => 'watches',
             'with_front' => false,
            ),
            'capability_type' => 'post',
            'has_archive' => false,
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array('title','editor')
            )
        );
    
        register_taxonomy( 'watch_category', array( 'watch' ), array(
            'hierarchical' => true,
            'label' => __( 'Watch Category' ),
            'labels' => array( // Labels customizadas
            'name' => _x( 'Watch Category', 'taxonomy general name' ),
            'singular_name' => _x( 'Category', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Categories' ),
            'all_items' => __( 'All Categories' ),
            'parent_item' => __( 'Parent Category' ),
            'parent_item_colon' => __( 'Parent Category:' ),
            'edit_item' => __( 'Edit Category' ),
            'update_item' => __( 'Update Category' ),
            'add_new_item' => __( 'Add New Category' ),
            'new_item_name' => __( 'New Category Name' ),
            'menu_name' => __( 'Category' ),
        ),
            'show_ui' => true,
            'show_in_tag_cloud' => true,
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'watches/categories',
                'with_front' => false,
            ),
            )
        );
        flush_rewrite_rules( false );
    
        register_taxonomy_for_object_type( 'tags', 'watch' );
    
    }
    
    ?>

    Até ai tudo bem, cadastrei as coleções como categoria principal na taxonomia que criei e as linhas como sub categorias.

    Criei o arquivo taxonomy-watch_category.php para exibir a listagem dos produtos.

    Porém quando acesso uma das categorias ele exibe TODOS os produtos cadastrados e não somente os produtos ligados aquela categoria.

    taxonomy-watch_category.php

    <?php if ( have_posts() ) : ?>
    			<?php $args = array( 'post_type' => 'watch', 'posts_per_page' => 10 );
    				$loop = new WP_Query( $args );
    				while ( $loop->have_posts() ) : $loop->the_post();   ?>
    				<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    					<p><?php the_excerpt(); ?></p>
    			<?php endwhile; ?>
    		<?php endif; ?>

  • O tópico ‘Listar custom post type por taxonomia’ está fechado a novas respostas.