• Resolved Dario Ferrer

    (@metacortex)


    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?.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Dario Ferrer

    (@metacortex)

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

    Do you have an english version of your website?

    Thread Starter Dario Ferrer

    (@metacortex)

    Hello irocket,

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

    Thread Starter Dario Ferrer

    (@metacortex)

    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”.

    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:
    https://codex.www.ads-software.com/Template_Tags/query_posts

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

    https://www.ads-software.com/support/topic/57912#post-312858

    Thread Starter Dario Ferrer

    (@metacortex)

    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.

    Thread Starter Dario Ferrer

    (@metacortex)

    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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Show only one category in post list’ is closed to new replies.