wordpress中add_theme_support()函数如何使用

更新时间:2022-06-08 21:31:27 来源:青锋建站 作者:青锋建站
  WordPress功能函数add_theme_support(),为给定的特性注册主题支持。一般用于在function.php中使用 add_theme_support() 函数启用 WordPress 功能 。例如,启用缩略图,文章格式和导航菜单。 以下是青锋建站给大家分享的wordpress中add_theme_support()函数使用方法。

add_theme_support()语法:

add_theme_support( string $feature, mixed $args );

add_theme_support()示例

必须在主题的functions.php文件中调用才能工作。如果附加到一个钩子上,它必须是' after_setup_theme '。init钩子对于某些特性来说可能太晚了。
使用示例:
add_theme_support( 'title-tag' );
add_theme_support( 'custom-logo', array(
'height' => 480,
'width' => 720,
) );

add_theme_support()参数:

$feature
(string) (必需)要添加的特性。可能的核心价值包括:
'admin-bar'
'align-wide'
'automatic-feed-links'
'core-block-patterns'
'custom-background'
'custom-header'
'custom-line-height'
'custom-logo'
'customize-selective-refresh-widgets'
'custom-spacing'
'custom-units'
'dark-editor-style'
'disable-custom-colors'
'disable-custom-font-sizes'
'editor-color-palette'
'editor-gradient-presets'
'editor-font-sizes'
'editor-styles'
'featured-content'
'html5'
'menus'
'post-formats'
'post-thumbnails'
'responsive-embeds'
'starter-content'
'title-tag'
'wp-block-styles'
'widgets'
$args
(mixed) (可选) 与某些特性一起传递的额外参数。

add_theme_support()返回值

(void|false)成功时无效,失败时false。

add_theme_support()使用举例

add_theme_support(“post-formats”)
这个特性支持主题的Post格式。使用子主题时,请注意它将覆盖父主题定义的格式,而不是添加到它。要启用特定的格式(参见Post格式支持的格式),请使用:add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

使用has_post_format()来检查是否有一个' quote '格式分配给文章:
// In your theme single.php, page.php or custom post type
if ( has_post_format( 'quote' ) ) {
echo 'This is a quote.';
}

发布缩略图
  这个特性使Post缩略图对主题的支持成为可能。注意,您可以选择传递第二个参数$args,它带有一个Post类型的数组,您希望为其启用该特性。
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails', array( 'post' ) ); // Posts only
add_theme_support( 'post-thumbnails', array( 'page' ) ); // Pages only
add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); // Posts and Movies
  这个特性必须在' init '钩子被触发之前被调用。这意味着它需要被直接放置在functions.php中或附加在' after_setup_theme '钩子中的函数中。对于自定义文章类型,还可以使用register_post_type()函数添加文章缩略图。要在themes .php或single.php或自定义模板中显示缩略图,请使用:the_post_thumbnail();要在显示之前检查是否有一个post缩略图分配给该post,使用:
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}

自定义背景
这个特性启用了对主题的custom_background支持。
add_theme_support( 'custom-background' );
注意,你可以使用以下方式添加默认参数:
$defaults = array(
'default-image' => '',
'default-preset' => 'default', // 'default', 'fill', 'fit', 'repeat', 'custom'
'default-position-x' => 'left', // 'left', 'center', 'right'
'default-position-y' => 'top', // 'top', 'center', 'bottom'
'default-size' => 'auto', // 'auto', 'contain', 'cover'
'default-repeat' => 'repeat', // 'repeat-x', 'repeat-y', 'repeat', 'no-repeat'
'default-attachment' => 'scroll', // 'scroll', 'fixed'
'default-color' => '',
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => '',
);
add_theme_support( 'custom-background', $defaults );

自定义Header
这个特性启用了对主题的Custom_Headers支持。
add_theme_support( 'custom-header' );
注意,你可以使用以下方式添加默认参数:
$defaults = array(
'default-image' => '',
'random-default' => false,
'width' => 0,
'height' => 0,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '',
'header-text' => true,
'uploads' => true,
'wp-head-callback' => '',
'admin-head-callback' => '',
'admin-preview-callback' => '',
'video' => false,
'video-active-callback' => 'is_front_page',
);
add_theme_support( 'custom-header', $defaults );

自定义Logo
这个特性是在Version_4.5中首次引入的,它支持主题的Theme_Logo。
add_theme_support( 'custom-logo' );
注意,你可以使用以下方式添加默认参数:
add_theme_support( 'custom-logo', array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
) );

Feed Links
这个功能可以在头部自动提供帖子和评论的链接。这应该用来代替已弃用的automatic_feed_links()函数。
add_theme_support( 'automatic-feed-links' );

HTML5
这个特性允许对搜索表单、评论表单、评论列表、图片库和标题使用HTML5标记。
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );

标题标签
这个特性使插件和主题能够管理文档标题标签。这应该用来代替wp_title()函数。
add_theme_support( 'title-tag' );

定制选择性刷新小部件
  该特性支持对定制器中管理的小部件进行选择性刷新。这个功能在WordPress 4.5中就可以使用了。有关选择性刷新的原因和方式的更多信息,请阅读对小部件实现选择性刷新支持。
add_theme_support( 'customize-selective-refresh-widgets' );
  以上就是青锋建站给大家分享的wordpress中add_theme_support()函数的使用,青锋建站承接网站建设服务,包括织梦建站,phpcms建站,wordpress建站,CMS系统开发,SEO网站优化,网络营销推广,企业邮箱,400电话。

转载请注明来源网址:青锋建站-http://www.sjzphp.com/cmsxitong/wordpressxt/add_theme_support_1371.html

地方分站

电话 15632335515 | 邮箱 943703539@qq.com | QQ 943703539 | 微信 qingfengjianzhan

Copyright © 2016-2026 青锋建站 版权所有