函数文档

wp_get_custom_css_post()

💡 云策文档标注

概述

wp_get_custom_css_post() 函数用于获取指定主题的 custom_css 文章对象。它通过 WP_Query 查询或主题修改值缓存来检索,返回 WP_Post 对象或 null。

关键要点

  • 参数 $stylesheet 可选,默认为当前活动主题的样式表名称。
  • 返回 WP_Post 对象或 null(如果 custom_css 文章不存在)。
  • 对于当前活动主题,优先使用 get_theme_mod('custom_css_post_id') 缓存以提高性能。
  • 使用 WP_Query 查询 custom_css 文章类型,并设置优化参数如 no_found_rows 和 cache_results。

代码示例

function wp_get_custom_css_post( $stylesheet = '' ) {
    if ( empty( $stylesheet ) ) {
        $stylesheet = get_stylesheet();
    }

    $custom_css_query_vars = array(
        'post_type'              => 'custom_css',
        'post_status'            => get_post_stati(),
        'name'                   => sanitize_title( $stylesheet ),
        'posts_per_page'         => 1,
        'no_found_rows'          => true,
        'cache_results'          => true,
        'update_post_meta_cache' => false,
        'update_post_term_cache' => false,
        'lazy_load_term_meta'    => false,
    );

    $post = null;
    if ( get_stylesheet() === $stylesheet ) {
        $post_id = get_theme_mod( 'custom_css_post_id' );

        if ( $post_id > 0 && get_post( $post_id ) ) {
            $post = get_post( $post_id );
        }

        if ( ! $post && -1 !== $post_id ) {
            $query = new WP_Query( $custom_css_query_vars );
            $post  = $query->post;
            set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 );
        }
    } else {
        $query = new WP_Query( $custom_css_query_vars );
        $post  = $query->post;
    }

    return $post;
}

注意事项

  • 函数自 WordPress 4.7.0 版本引入。
  • 缓存机制可能需要在 custom_css 文章添加或删除时清除,当前代码中已标注为待办事项。
  • 相关函数包括 wp_update_custom_css_post() 和 wp_get_custom_css(),用于更新和获取自定义 CSS 内容。

📄 原文内容

Fetches the custom_css post for a given theme.

Parameters

$stylesheetstringoptional
A theme object stylesheet name. Defaults to the active theme.

Return

WP_Post|null The custom_css post or null if none exists.

Source

function wp_get_custom_css_post( $stylesheet = '' ) {
	if ( empty( $stylesheet ) ) {
		$stylesheet = get_stylesheet();
	}

	$custom_css_query_vars = array(
		'post_type'              => 'custom_css',
		'post_status'            => get_post_stati(),
		'name'                   => sanitize_title( $stylesheet ),
		'posts_per_page'         => 1,
		'no_found_rows'          => true,
		'cache_results'          => true,
		'update_post_meta_cache' => false,
		'update_post_term_cache' => false,
		'lazy_load_term_meta'    => false,
	);

	$post = null;
	if ( get_stylesheet() === $stylesheet ) {
		$post_id = get_theme_mod( 'custom_css_post_id' );

		if ( $post_id > 0 && get_post( $post_id ) ) {
			$post = get_post( $post_id );
		}

		// `-1` indicates no post exists; no query necessary.
		if ( ! $post && -1 !== $post_id ) {
			$query = new WP_Query( $custom_css_query_vars );
			$post  = $query->post;
			/*
			 * Cache the lookup. See wp_update_custom_css_post().
			 * @todo This should get cleared if a custom_css post is added/removed.
			 */
			set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 );
		}
	} else {
		$query = new WP_Query( $custom_css_query_vars );
		$post  = $query->post;
	}

	return $post;
}

Changelog

Version Description
4.7.0 Introduced.