I'm trying to find a way to get my posts with different look depending on their category.

I have first tried to do it using the template hierarchy but it seems there is no pattern for the category posts. (ie single-cat-mycategory.php)

So then within single.php I tried the conditional tagging using is_category() but my understanding it's only working for the archive pages.

Finally, I'm now trying to use the is_single() conditional tagging where first I'm looking for all the posts corresponding to my category and pass it as a parameter of is_single()

In functions.php

function get_post_id_by_cat(){

 $args = [
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'category_name' => 'my_category',
$cat_query = new WP_Query( $args );
if ($cat_query -> have_posts()){
        while ($cat_query -> have_posts()){
                $cat_query -> the_post();
                $post_ids[] = get_the_ID();


In single.php

$my_cat_posts = get_post_id_by_cat();

if (is_single($my_cat_posts)) {

                        while ( have_posts() ) : the_post();
      //do my stuff here

My first question is this in term of Wordpress design a good way to do this or is there a better way as my concern is mainly for performance, with this method.

If it's ok, My issue is that $post_ids is storing the data in a php object format and not an array so is_single() doesn't get the posts ids properly as parameter. And i don't get how I can get it converted to an array.

$post_ids[] = to_array(get_the_ID()); return

Uncaught Error: Call to undefined function to_array()

or $post_ids[] = (array)get_the_ID(); return each post ids stil has object but within a array like this:

array(8) {
  array(1) {

or $post_ids[] = get_object_vars(get_the_ID()); return

get_object_vars() expects parameter 1 to be object, integer given

But if pass this array it does work properly:

$array = ['5415','5413','5411','5401'];

Hope this is clear enough,

Thanks for any input!


2 Answers 2


Think what you need is in_category function ...... Please go through


and check whether it suits your needs ......

and one more filter single_template is there that you can check .

  • Effectively seems much more efficient that what i'm trying to do now, looking at it now. Thanks ! Mar 17, 2017 at 6:55

Thanks @Anoop D I found the solution I was looking at the begining when looking at [single_template][1] !

Here is the model I used to have a single-cat-mycat.php per category !

function get_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'my_post_type') {
          $single_template = dirname( __FILE__ ) . '/post-type-template.php';
     return $single_template;
add_filter( 'single_template', 'get_custom_post_type_template' );

This issue is fix ! Now let's go to the next challenge :D 



Your Answer

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

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