Loop of the Wordpress category

Asked

Viewed 921 times

0

I’m trying to make a loop just the category I’m in Wordpress and I’m not getting. Somebody give me a light? I’m trying to file Archive.php of the theme.

obs. Should display only posts in the open category.

Below the code of my Archive.php:

<?php
if (is_home()){
query_posts("showposts=5&cat=1,2,3,4,5,6,7,8,9,10,11,12"); while(have_posts()) : the_post(); ?>
<div class="LatestPosts">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="TxtNotice">
<?php //echo excerpt('20'); ?> <?php  echo the_title(); ?>[...]
</br>

</a>
</div>
<?php 
endwhile; wp_reset_query(); }?>
  • Try it this way: $query = new wp_query( array( 'cat' => '1,2,3,4,5,6,7,8,9,10,11,12' ) );

  • Thanks for the feedback Fernando, but in my case I want to bring only the posts of the category I am.

2 answers

1

In fact wordpress it will recognize the file Category.php by default to display the posts of a category, just put the following code:

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

<!-- DADOS DOS POSTS AQUI -->

<?php endwhile; endif;?>

After that when you click on a category wordpress will call this page and already send the category as parameter to it, thus displaying the posts of a certain category.

I hope to help you.

0

Felipe, what you’re looking for is get_queried_object();

He seeks the object in question.

Or by declaring $post as global and returning the category:

global $post;
$postcat = get_the_category( $post->ID );

with this you filter your posts only by the $postcat variable;

Note that you can always use var_dump() to be aware of the results.

For example, when using :

global $post; $postcat = get_the_category( $post->ID );

You can give a var_dump($postcat); and check the returned value, if that’s what you need, problem solved :)

Browser other questions tagged

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