钩子文档

post_type_labels_{$post_type}

💡 云策文档标注

概述

post_type_labels_{$post_type} 是一个 WordPress 过滤器钩子,用于修改特定文章类型的标签。钩子名称中的动态部分 $post_type 指文章类型的 slug,例如 post_type_labels_post 或 post_type_labels_page。

关键要点

  • 钩子名称是动态的,基于文章类型 slug,如 post_type_labels_post 或 post_type_labels_page。
  • 参数 $labels 是一个对象,包含文章类型的标签作为成员变量,而不是数组。
  • 可用于自定义文章类型标签,例如修改“特色图像”相关字符串。

代码示例

/**
 * Changes strings referencing Featured Images for a post type
 * 
 * @param 		object 		$labels 		Current post type labels
 * @return 		object 					Modified post type labels
 */
function change_featured_image_labels( $labels ) {

	$labels->featured_image 	= 'Headshot';
	$labels->set_featured_image 	= 'Set headshot';
	$labels->remove_featured_image 	= 'Remove headshot';
	$labels->use_featured_image 	= 'Use as headshot';

	return $labels;

} // change_featured_image_labels()

add_filter( 'post_type_labels_employee', 'change_featured_image_labels', 10, 1 );

注意事项

  • 此过滤器的 $labels 参数是一个对象,不是数组,修改时需使用对象属性访问方式。

📄 原文内容

Filters the labels of a specific post type.

Description

The dynamic portion of the hook name, $post_type, refers to the post type slug.

Possible hook names include:

  • post_type_labels_post
  • post_type_labels_page
  • post_type_labels_attachment

See also

Parameters

$labelsobject
Object with labels for the post type as member variables.

Source

$labels = apply_filters( "post_type_labels_{$post_type}", $labels );

Changelog

Version Description
3.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    If you need to modify the post type labels, note the labels parameter for this filter is an object, not an array.

    In the example below, the post type is “employee” and the new label reference is “headshot”.

    /**
     * Changes strings referencing Featured Images for a post type
     * 
     * @param 		object 		$labels 		Current post type labels
     * @return 		object 					Modified post type labels
     */
    function change_featured_image_labels( $labels ) {
    
    	$labels->featured_image 	= 'Headshot';
    	$labels->set_featured_image 	= 'Set headshot';
    	$labels->remove_featured_image 	= 'Remove headshot';
    	$labels->use_featured_image 	= 'Use as headshot';
    
    	return $labels;
    
    } // change_featured_image_labels()
    
    add_filter( 'post_type_labels_employee', 'change_featured_image_labels', 10, 1 );