query_posts , take the category and subcategory

Asked

Viewed 621 times

1

Good morning to you all. For the first time I come to ask for help, after many attempts (I’m a beginner).

I’m looking to list several posts (poste_type called=familia_fish ) by category and subcategory. (I’m trying to take advantage of a loop that’s running on another page that shows all the posts.)

<?php query_posts('familia_peixe_familia=familia_ordem_peixe & showposts= -1 & orderby=title');?>       
                            <?php if (have_posts()): while(have_posts()) : the_post();?>

                                <div id="box-categoria-ficha-peixe-conteudo">
                                    <div class="box-categoria-ficha-peixe-conteudo-faixa">                          

                                            <div class="h3-ficha-peixe-22"><?php if (get_post_meta($post->ID, 'classificacao_peixe_ordem', true)):?><a href="<?php  $key="classificacao_peixe_ordem";echo get_post_meta($post->ID, $key, true);?>" title="<?php echo get_post_meta( $post->ID, 'familia_peixe_ordem', true ); ?>" >Lista ordem dos :  <?php if (get_post_meta($post->ID, 'classificacao_peixe_ordem', true)){echo get_post_meta( $post->ID, 'classificacao_peixe_ordem', true );} ?></a><?php endif?></div>


                                    </div>
                                        <div class="box-categoria-ficha-peixe-conteudo-titulo"><?php the_title();?></div>
                                        <div class="box-categoria-ficha-peixe-conteudo-texto">  <?php echo excerpt(80); ?>

                                    </div>

                            <?php endwhile; else:?>
                            <?php endif;?>

This is what I intended inserir a descrição da imagem aqui

1 answer

1

You haven’t said what error you’re getting, but I assume you’re not getting results, right? Your current code is specifying taxonomy familia_peixe_familia and the term familia_ordem_peixe but does not specify the post_type, then query_posts() is looking for in type Post.

Rewriting without using query_posts() because using query_posts is never a good idea:

<?php 
$consulta = new WP_Query( array ( 
    'post_type' => 'familia_peixe',
    'posts_per_page' => 100, // use 100 ao invés de -1 para evitar problemas futuros
    'tax_query' => array( array(
        'taxonomy' => 'familia_peixe_familia', // taxonomia
        'term' => 'familia_ordem_peixe', // termo
        'field' => 'slug',
    ) ),
    'orderby' => 'title',
) );

if ( $consulta->have_posts()): while( $consulta->have_posts()) : $consulta->the_post(); ?>
  • Thank you Ricardo. The problem I had, but it wasn’t 100% a problem was that I was willing to make the sub-categories below each mother category. The code as written worked well, but not by the right hierarchy. I could not do as I wanted 100%, but I could solve another way. Thanks again for the help

Browser other questions tagged

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