Suporte » Plugins » Listando post type nas categorias

  • Resolvido raphaelbdesigner

    (@raphaelbdesigner)


    Boa tarde galera,

    Sou novo com wordpress e estou tendo dificuldade com o famoso Post Type. Registrei um novo tipo de post mais ele não é listado no nas categorias, apenas os posts normais.

    add_action( 'init', 'type_post_projetos' );
    
    function type_post_projetos() {
    
        $labels = array(
    	    'name' => _x('Portfólio', 'post type general name'),
    	    'singular_name' => _x('Projeto', 'post type singular name'),
    	    'add_new' => _x('Adicionar Novo', 'Novo item'),
    	    'add_new_item' => __('Novo Projeto'),
    		'edit_item' => __('Editar Projeto'),
    	    'new_item' => __('Novo Projeto'),
    	    'all_items' => __('Todos os Projetos'),
    	    'view_item' => __('Ver Projeto'),
    	    'search_items' => __('Procurar Itens'),
    		'not_found' =>  __('Nenhum registro encontrado'),
    		'not_found_in_trash' => __('Nenhum registro encontrado na lixeira'),
    	    'parent_item_colon' => '',
    	    'menu_name' => 'Projetos'
        );
    
        $args = array(
    	    'labels' => $labels,
    	    'public' => true,
    	    'publicly_queryable' => true,
    	    'show_ui' => true,
    	    'show_in_menu' => true,
    	    'show_in_nav_menus' => true,
    	    'show_in_admin_bar' => true,
    	    'show_admin_column' => true,
    	    'rewrite' => array('slug' => 'portfolio','with_front' => false),
    	    'capability_type' => 'post',
    	    'has_archive' => true,
    	    'hierarchical' => false,
    	    'menu_position' => null,
    	    'supports' => array('title','editor','thumbnail','comments', 'excerpt', 'custom-fields', 'revisions', 'trackbacks', 'page-attributes'),
    	    'taxonomies' => array( 'category', 'post_tag', 'wo_plataforma' ),
    	    'query_var'  => 'projetos',
    	    'label' => 'Projetos',
    	    'can_export' => true,
        );
    
    		register_post_type( 'projetos' , $args );
    		flush_rewrite_rules();
    		}

    Meu loop de repetição está assim

    <?php while (have_posts('projetos')) : the_post(); ?>
    
          <div class="col-lg-3 col-md-4 col-sm-6 projeto-bloco">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive")); ?>
    
            <div class="title-project margin-tp-small">
            <h3 class="margin-tp-small margin-bt-zero"><?php the_title(); ?></h3></a>
            <p class="margin-tp-zero"><?php the_category(',') ?> <?php echo strip_tags(get_the_tag_list('',' • ',''));?>.</p>
            </div>
          </div>
    
          <?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>

    Alguém consegue me ajudar?

Visualizando 3 respostas - 1 até 3 (de um total de 3)
  • Moderador Leo Baiano

    (@leobaiano)

    O que você quer é que os posts do seu CPT – Custom Post Type apareçam quando você acessar a página de uma categoria?

    1 – A lista de categorias aparece quando está editando um post do CPT projetos?
    2 – Existem posts do CPT projetos na categoria que você está acessando?

    Se sim para as duas perguntas, talvez um pre_get_posts te ajude, tenta o código abaixo no seu functions.php

    add_filter('pre_get_posts', 'add_post_type_in_taxonomies_list');
    function add_post_type_in_taxonomies_list( $query ) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var( 'post_type' );
    	if ($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array( 'post','projetos' );
        $query->set( 'post_type', $post_type );
    	return $query;
        }
    }
    Criador do tópico raphaelbdesigner

    (@raphaelbdesigner)

    Muito Obrigado Leo!

    Funcionou perfeitamente para o que eu queria! Não achei que seria tão simples. =)

    Moderador Leo Baiano

    (@leobaiano)

    Na maioria das vezes a solução é a mais simples, nós é que ficamos correndo atrás de soluções miraculosas.

Visualizando 3 respostas - 1 até 3 (de um total de 3)
  • O tópico ‘Listando post type nas categorias’ está fechado para novas respostas.