Attachment page per category

时间:2015-12-01 作者:Nebojsa Lukic

我想显示特定类别的自定义附件页,即类别的附件页wallpapers 应具有不同于类别的模板cars. 有什么建议吗?

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

附件没有类别,因此in_category()has_category() 检查不起作用。我们需要做的是解决以下问题

获取当前附件对象。我们将利用get_queried_object() 它将保存正在查看的附件的当前post对象

确定当前附件的后父级。这很容易,从get_queried_object() 我们将利用post_parent 存储附件所属帖子ID的属性

然后我们将使用has_category() 确定附件的父级是否具有所需的类别

所有这些信息都将在attachment_template 如果附件父级属于所需类别,我们将在其中设置特定模板

您可以执行以下操作:(NOTE: 以下代码未经测试,至少需要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;
});
为了便于理解,我对代码进行了注释,并且还添加了您应该在哪里更改值以满足您的需要

编辑如果需要为多个类别设置相同的模板,则只需扩展in_category() 检查in_category() 接受类别ID、名称和slug的数组。像下面这样的方法会奏效

    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;
如果您需要扩展此功能以为不同类别设置不同的模板,我们需要进行更多检查

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;
});