今天写的一个需求是更新网站作者文章列表页面的schema
。
// Get the "/author" page.
$author_page = get_page_by_path( self::$author_page_slug );
// Get author ID.
$author_id = get_queried_object_id();
// Get author name.
$author_name = get_the_author_meta( 'display_name', $author_id );
// Get author URL.
$author_url = get_author_posts_url( $author_id );
// Create the schema parts array.
$schema_parts_array =
[
[
'name' => esc_html__( 'Home', 'abc' ),
'item' => site_url(),
],
$author_page ? [
'name' => $author_page->post_title,
'item' => site_url( self::$author_page_slug ),
] : null,
[
'name' => $author_name,
'item' => $author_url,
],
];
如果author_page
不存在的话,则第二个值为null,可以用array_filter()
,不添加回调函数,直接可以剔除非数组内容。
// Create the schema parts array.
$schema_parts_array = array_filter(
[
[
'name' => esc_html__( 'Home', 'abc' ),
'item' => site_url(),
],
$author_page ? [
'name' => $author_page->post_title,
'item' => site_url( self::$author_page_slug ),
] : null,
[
'name' => $author_name,
'item' => $author_url,
],
]
);