Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:18:30+00:00 2026-05-25T19:18:30+00:00

I love the Custom Post Templates plugin so much I want to build it

  • 0

I love the Custom Post Templates plugin so much I want to build it into my themes.

The function can be found here, but it only works for pages. How do I get this to also work for posts?

add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:18:30+00:00Added an answer on May 25, 2026 at 7:18 pm

    According to the website you linked, this should work for posts and pages. Also, according to the WordPress docs (http://codex.wordpress.org/Plugin_API/Filter_Reference/_single_template), the ‘single_template’ filter is used for both posts and pages.

    Do you have a separate single.php file with the correct category ID in the name? e.g. single-2.php

    If no correctly named single.php file is found, it will revert back to the standard single.php file. Is this what you’re seeing?

    EDIT:

    If you wanted this option actually in the WordPress Admin area, I would suggest using Post Meta to store the value of the single.php file you want to use.

    You could add a custom meta box to the admin page http://codex.wordpress.org/Function_Reference/add_meta_box then you could use some of your code from your original question to get all the available single.php template files, and drop them into a drop down list:

    $templates = array();
    foreach( (array) get_the_category() as $cat ) { 
        if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) 
            $templates[] = TEMPLATEPATH . "/single-{$cat->term_id}.php";
    }
    

    You would then need to add a filter similar to your original one:

    function custom_single_template($t){
        global $post;
    
        if( get_post_meta($post->ID, 'name_of_key', true) ) {
    
          return TEMPLATEPATH . get_post_meta($post->ID, 'name_of_key', true);
    
        }
    
        return $t;
    
    }
    add_filter('single_template', 'custom_single_template');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I love this plugin and I'm trying to implement it into a custom photo
I'm no designer and can't get a custom design done. I would love it
I've already seen this post: Can I embed a custom font in an iPhone
In Magento I'm creating a custom module and would love to be able to
I love vim and the speed it gives me. But sometimes, my fingers are
I love the way I can profile a Java/.Net app to find performance bottlenecks
I love scaffolding and it extremely helpful for prototyping. But Should we use scaffolding
I have the following script that I run as a custom build step in
Hi I recently fell in love with an extended upload button control I found
Background First of all, much gratitude to atebits for their very informative blog post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.