1

I wanted to ask if there is a shorter way then by checking each time if the post is coming from a specific category.

As you can see the categories cover the alphabet, A B C etc and each time I check with a custom loop etc, is there a shorter way to write this or am I on track.

At the moment how I have it set up works.

<?php  if ( is_category( 'a' )  ) { ?> 
<?php $THEresults = array( 'post_type' => 'plants', 'orderby'=> 'title', 'order' => 'ASC', 'category_name' => 'a',);
    $loop = new WP_Query( $THEresults );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php get_template_part( 'content' ); ?>
<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

 <?php  } elseif ( is_category( 'b' ) ) { ?>
<?php
    $THEresults = array( 'post_type' => 'plants', 'orderby'=> 'title', 'order' => 'ASC', 'category_name' => 'b',);
    $loop = new WP_Query( $THEresults );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php get_template_part( 'content' ); ?>
<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

EDIT FROM COMMENTS

I have a custom post type ‘plants’ and the posts created are arranged into categories in alphabetical form such as category A, B, C etc. When you click on category A then the above code will only display those posts in category A, for B, C etc. The above works but didn’t know if there was a shorter way to check if the post belongs to category A, B, C etc 

4
  • 2
    TLDR; is that equivalent to in_category()? Apr 22, 2015 at 6:35
  • 1
    is_category() dosen't check if a post belongs to category; that functions check if you are in a category archive page (for exmpale yoursite.com/category/health). Can you explain what you are trying to do?
    – cybmeta
    Apr 22, 2015 at 6:38
  • I have a custom post type ‘plants’ and the posts created are arranged into categories in alphabetical form such as category A, B, C etc. When you click on category A then the above code will only display those posts in category A, for B, C etc. The above works but didn’t know if there was a shorter way to check if the post belongs to category A, B, C etc
    – Bjarni
    Apr 22, 2015 at 7:09
  • You should post important info like this directly in your question. Most people do not read comments and will miss this valuable info and because of this simply skip your question :-). Always file an edit with important info by clicking on the edit button below your question Apr 22, 2015 at 7:35

1 Answer 1

2

You are complicating everything with a custom query. You should never ever replace the main query with a custom one on the home page or any type of archive page, this breaks page functionalities as you have seen. You should take your time and read this post I have done on this particular issue

You just need the default loop in category.php, nothing else

if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();

        //Your html and template tags

    }
}

EDIT

Just some extra info for interest sake

You don't need all that statements to check the category. You can simply access the queried object and get whatever needed info from there. get_queried_object() returns the complete queried object, get_queried_object_id() returns the current object id. So you could have just passed the following to your query arguments

'cat' => get_queried_object_id(),

instead of doing al those plenty if/else statements

EDIT 2

It seems your biggest headche is actually your custom post type. By default, they are excluded from the main query on category pages. With the changes you've made according to recommendation from my original past, you will not see custom post type posts, just posts from the default post type post

As I have described in the linked post, you should use pre_get_posts to alter the main query to suite your needs. And this what you should do here as well.

In your functions.php, you need to add the following code, this will add your custom post type to your category pages

add_action( 'pre_get_posts', function ( $q ) 
{
    if ( !is_admin() && $q->is_main_query() && $q->is_category() ) {
        $q->set( 'post_type', array( 'post', 'plants' ) ); //This will show normal posts and plants
    }
});

The above will now show normal posts and posts from the post type plants. If you don't need to show normal posts, just remove post from the array

11
  • Added extra info for interest sake :-) Apr 22, 2015 at 8:00
  • Thanks @PieterGosen for the extra info. I tried with just the basic loop but does not show the CPT post/categories, it does however just show the generic posts. My original code does work but I will follow up on your advice and do some reading on 'cat' => get_queried_object_id(), to see if I can shorten the code.Will get back into this tomorrow and let you know how I go.
    – Bjarni
    Apr 22, 2015 at 9:13
  • I will post a solution a bit later to include your custom post type without a custom query :-) Apr 22, 2015 at 9:19
  • That would be handy, very interested in learning easy to do the job better. This is how the front end result looks dropbox.com/s/ffitpx6gcvlaaah/cpt-plants.png?dl=0
    – Bjarni
    Apr 22, 2015 at 9:27
  • I'll have to pop back again tomorrow, chat soon, thanks for the tips so far.
    – Bjarni
    Apr 22, 2015 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.