2

I want to display a custom attachment page for a specific category, i.e. attachment page for category wallpapers should have a different template than category cars. Any suggestions?

2

Attachments does not have categories, so a simple in_category() or has_category() check will not work. What we will need to do to solve is this issue is the following

  • Get the current attachment object. We will make use of get_queried_object() which will hold the current post object of the attachment being viewed

  • Determine the post parent of the current attachment. This is easy, from get_queried_object() we will make use of the post_parent property which stores the ID of the post the attachment belongs to

  • We will then use has_category() to determine if the attachment's parent has the desired category

  • All of this info will be used inside attachment_template where we will set a specific template if an attachment parent is in the required category

You can do the following: (NOTE: The following code is untested and requires at least PHP5.4)

add_filter( 'attachment_template', function ( $template )
{
    // Get the current attachment object
    $attachment = get_queried_object();

    // Check if current attachment's parent is attached to our desired category or not
    if ( 
         !has_category( // If our parent does not have this category, return 
            1, // Category id, or name or slug, change as needed
            $attachment->post_parent // The id from the post parent
         )
    ) 
        return $template;

    // We have made it to here, lets set the new template for this attachment
    // Check if the template exist before we add it
    $locate_template = locate_template( 'image-mycategory.php'); // Change as needed
    if ( !$locate_template )
       return $template;

    // YEAH, everything checks out, finaly. Set the new template
    return $template = $locate_template;
});

I have commented the code for easy following, and I also have added where you should change value to suite your needs

EDIT

If you need to set the same template for multiple categories, you can just extend the in_category() check. in_category() accepts an array of category ID's, names, and slugs. Something like the following would work

    if ( 
         !has_category( // If our parent does not have this category, return 
            [1, 2, 3] // Array of category id's, or names or slugs, change as needed
            $attachment->post_parent // The id from the post parent
         )
    ) 
        return $template;

If you need to extend this to set different templates for different categories, we need a few more checks

add_filter( 'attachment_template', function ( $template )
{
    // Get the current attachment object
    $attachment = get_queried_object();

    /**
     * Check if current attachment's parent is attached to our desired category or not and set template part
     * For example, templates will be image-cars.php and image-tech.php, so our
     * $parts will be cars and tech
     */
    $part = '';

    // Check for cars category and set $part
    if ( 
         has_category( // If our parent have this category, set template part
            1, // Category id, or name or slug, change as needed
            $attachment->post_parent // The id from the post parent
         )
    ) 
        $part = 'cars';

    // Check for tech category and set $part
    if ( 
         has_category( // If our parent have this category, set template part
            2, // Category id, or name or slug, change as needed
            $attachment->post_parent // The id from the post parent
         )
    ) 
        $part = 'tech';

    // Check if $part have a value, if not, bail
    if ( !$part )
        return $template;

    // We have made it to here, lets set the new template for this attachment
    // Check if the template exist before we add it
    $locate_template = locate_template( "image-$part.php"); // Change as needed
    if ( !$locate_template )
       return $template;

    // YEAH, everything checks out, finally. Set the new template
    return $template = $locate_template;
});
  • Tnx Pieter it Works! Is this work only for one specific template..ie Wallpapers...what if you also wanna set for few more categories? ie Cars, Tech...you just need to repeat if statement or? – Nebojsa Lukic Dec 2 '15 at 23:12
  • I have added an edit with the desired added part. Hope that helps. Enjoy ;-) – Pieter Goosen Dec 3 '15 at 4:10
  • Pieter, this is what I ask..tnx so much!... Maybe one day wordpress will implement this feature ;) – Nebojsa Lukic Dec 3 '15 at 15:58
  • My pleasure, glad I could help. Enjoy ;-) – Pieter Goosen Dec 3 '15 at 16:31

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.