0

hope somebody can help me with this because I am struggling for a while. I want to use two single.php on my website, one to display the regular posts and another to display some products that I will style different then the posts. I duplicated the single.php and named it products.php but I cannot figure out how to make the permalink grab my products.php and not the single.php. Please, is there a way to accomplish this? Thank you very much!

  • what differentiates a product from a regular post? – Milo Aug 27 '13 at 14:33
  • The product will have a gallery and the text will be displayed different then the post, I need to add featured products bellow and I have to disable comments. Thanks! – luchid Aug 27 '13 at 14:48
  • I meant in terms of how WordPress would know which template to load, what defines a product as such versus a regular post? – Milo Aug 27 '13 at 15:46
  • I used this function in my functions.php add_filter('single_template', create_function( '$the_template', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-{$cat->slug}.php"; } return $the_template;' ) – luchid Aug 27 '13 at 16:02
  • ok, "category" was the answer I was looking for. so this is solved then? add your solution as an answer if that's the case. – Milo Aug 27 '13 at 16:39
1

Try using a custom post type (named "products") and rename your single.php file to single-products.php. It should be much easier like this.

The plugin below (free) should help you create a custom post type without any coding.

http://wordpress.org/plugins/custom-post-type-maker/

  • Thank you very much for answering me, Ciprian. I will try what you suggested me but I hoped I can solve it with permalinks. Thanks again! – luchid Aug 27 '13 at 10:39
1

I solved my problem by adding in my function.php this function:

add_filter('single_template',
    create_function(
        '$the_template',
        'foreach( (array) get_the_category() as $cat ) {
            if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) {
                return TEMPLATEPATH . "/single-{$cat->slug}.php";
            }
        }
        return $the_template;'
    )
);

and naming my second single.php with the slug name. Here's the tutorial I followed. Thank you all for helping me!

0

in your product.php page add this on top

/*
Template Name: anYtemplatEnamE
*/

and then from admin section you will assign this anYtemplatEnamE template to any page.

  • Thank you very much for answering me, I am new with WordPress and I used categories, instead of pages. Is there a different solution if I used categories? Thank you! – luchid Aug 27 '13 at 10:45
  • sorry i dint get you . – codepixlabs Aug 27 '13 at 10:52
  • As I said, I am new with WordPress and I try to explain as best I can, hope I am not upseting you. So, to list my products, I used <php wp_list_categories(); ?>. From my category.php, I need to link the products to a different single.php then the one it displays posts. Thank you! – luchid Aug 27 '13 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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