函数文档

_wp_preview_terms_filter()

💡 云策文档标注

概述

_wp_preview_terms_filter() 是一个 WordPress 过滤器函数,用于在预览时设置文章格式的术语查找。它通过检查请求参数和文章状态来调整返回的术语数组。

关键要点

  • 函数名:_wp_preview_terms_filter(),用于过滤术语查找以设置文章格式。
  • 参数:接受 $terms(数组)、$post_id(整数)和 $taxonomy(字符串)三个必需参数。
  • 返回值:返回一个数组,可能为空或包含单个文章格式术语。
  • 功能:在预览模式下,根据 $_REQUEST['post_format'] 的值更新术语数组,仅支持一个文章格式。
  • 条件检查:包括检查当前文章、请求参数、分类法和文章类型,确保仅在特定条件下应用过滤。
  • 相关函数:使用 get_term_by()、sanitize_key() 和 get_post() 来获取和处理数据。
  • 引入版本:WordPress 3.6.0。

代码示例

function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
    $post = get_post();

    if ( ! $post ) {
        return $terms;
    }

    if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id
        || 'post_format' !== $taxonomy || 'revision' === $post->post_type
    ) {
        return $terms;
    }

    if ( 'standard' === $_REQUEST['post_format'] ) {
        $terms = array();
    } else {
        $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );

        if ( $term ) {
            $terms = array( $term ); // Can only have one post format.
        }
    }

    return $terms;
}

📄 原文内容

Filters terms lookup to set the post format.

Parameters

$termsarrayrequired
$post_idintrequired
$taxonomystringrequired

Return

array

Source

function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
	$post = get_post();

	if ( ! $post ) {
		return $terms;
	}

	if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id
		|| 'post_format' !== $taxonomy || 'revision' === $post->post_type
	) {
		return $terms;
	}

	if ( 'standard' === $_REQUEST['post_format'] ) {
		$terms = array();
	} else {
		$term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );

		if ( $term ) {
			$terms = array( $term ); // Can only have one post format.
		}
	}

	return $terms;
}

Changelog

Version Description
3.6.0 Introduced.