WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show only one category in post list (9 posts)

  1. Dario Ferrer
    Member
    Posted 6 years ago #

    Hi,

    I've already some hours without resolve this issue. All of my posts are classified in three different categories, and I want to show only one category in my index. Actually I have something like this:

    Fruits Vegetables Plants
    This is an apple's article

    and i would want this

    Vegetables
    This is an apple's article

    Now, "Fruits" "Vegetables" and "Plants" belong to a parent category called "Good Food". How I can show only "Vegetables" as category?.

  2. Dario Ferrer
    Member
    Posted 6 years ago #

    Nobody knows? Please, if somebody can help... I have not been found information in internet about this.

  3. irocket
    Member
    Posted 6 years ago #

    Do you have an english version of your website?

  4. Dario Ferrer
    Member
    Posted 6 years ago #

    Hello irocket,

    Mi website is in spanish (throught an es_ES file placed in Languages directory).

  5. Chris_K
    Member
    Posted 6 years ago #

  6. Dario Ferrer
    Member
    Posted 6 years ago #

    Hi HandySolo,

    thank you very much for your suggest. I proved this plugin aprox. 3 days ago, but it don't worked in the cat links placed above the post titles. My template code looks thus (home.php):

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<?php the_category(' - ') ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    		 <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    	</div>
    	<?php endwhile; else: ?>
    	<?php _e('Sorry, no posts matched your criteria.'); ?>
    
    <?php endif; ?>

    Is code right? As you can see, posts are showed as only titles, resulting in some like this:

    Fruits - Vegetables - Plants
    This is an apple's article

    I want to show only one category above. In this case, category ID (Vegetables) is "3".

  7. Kafkaesqui

    Posted 6 years ago #

    Right above this section of your template:

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

    Add the following:

    <?php query_posts('cat=3'); ?>

    Reference:
    http://codex.wordpress.org/Template_Tags/query_posts

    If you need pagination on the page (next/previous posts links), see:

    http://wordpress.org/support/topic/57912#post-312858

  8. Dario Ferrer
    Member
    Posted 6 years ago #

    Hello Kafkaesqui,

    Ops! there is a detail I forgot to mention. The article "This is an apple's article" was assigned to three different categories, which belong to parent cats. I'm very, very sorry for this critical omission:

    Parent-1 (ID=4)
    -- Fruit (ID=16)

    Parent-2 (ID=3)
    -- Vegetables (ID=18)

    Parent-3 (ID=5)
    -- Plants (ID=17)

    In this order I had the modification in this way:

    <?php query_posts('cat=16'); ?> // Fruit
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<?php the_category() ?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    		 <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    	</div>
    	<?php endwhile; else: ?>
    	<?php _e('Sorry, no posts matched your criteria.'); ?>
    
    <?php endif; ?>

    However, results are same as before. Query_posts certainly limit posts of a specific category, but multiple cat names continue appearing above the title.

    Until this moment, the nearest result was give get_the_category tag with this string:

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>

    This shows only one cat as I need, but limited to array[0], it's a shame:

    Vegetables
    This is an apple's article

    Fruit
    This is an orange's article

    Fruit
    This is a watermelon's article

    The right way is to show "Vegetables" in each cat, but it don't works. Also I proved with various conditional tags ways without success.

  9. Dario Ferrer
    Member
    Posted 6 years ago #

    Solved guys!

    Based in this topic I had modify the string:

    <?php foreach((get_the_category()) as $cat) {
      if (!($cat->category_parent == 3))
      if (!($cat->category_parent == 4))
      echo $cat->cat_name . ' ';
    };?>

    The complete string:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php foreach((get_the_category()) as $cat) {
      if (!($cat->category_parent == 3))
      if (!($cat->category_parent == 4))
      echo $cat->cat_name . ' ';
    };?>
    
    <div class="post" id="post-<?php the_ID(); ?>">
      <h3 class="storytitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    </div>
    
    <?php endwhile; else: ?>
    <?php _e('Sorry, no posts matched your criteria.'); ?>
    
    <?php endif; ?>

    This chapter is finished. Thanks very much for your help. I'm happy.

Topic Closed

This topic has been closed to new replies.

About this Topic