GeekerCode更好的WordPress主题,值得信任的WordPress主题开发商

极美的主题、极致的插件

WordPress 向当前主题添加功能函数:add_theme_support()

摘要:【函数介绍】add_theme_support()用于在我们的当前使用的主题添加一些特殊的功能,函数一般写在主题的functions.php文件中,当然也可以再插件中使用钩子来调用该函数,如果是挂在钩子上,那他必须挂在”after_setu

【函数介绍】

add_theme_support()用于在我们的当前使用的主题添加一些特殊的功能,函数一般写在主题的functions.php文件中,当然也可以再插件中使用钩子来调用该函数,如果是挂在钩子上,那他必须挂在”after_setup_theme”钩子上。

【函数使用】

<?php add_theme_support( $feature ); ?>

【函数参数】

$feature

(string) (必须) 需要添加特殊功能名称,可以是以下参数:

  • 'post-thumbnails' —– 增加缩略图支持
  • 'automatic-feed-links' 自动输出RSS
  • 'post-formats'—– 增加文章格式功能
  • 'custom-background'—– 增加自定义背景
  • 'custom-header'—– 增加自定义顶部图像
  • 'menus'——自定义导航菜单

默认: None

【函数实例】

1、让主题支持不同文章类型:

add_theme_support( 'post-formats', array( 'aside', 'gallery' ) ); //添加日志与相册文章类型

判断不同类型文章代码:

// in your theme single.php, page.php or custom post type

if ( has_post_format( 'quote' ) ) {
    echo 'This is a quote.';
}

2、缩略图支持控制:

  • add_theme_support( 'post-thumbnails' ); //常规用法,在所有样式的文章、页面中使用缩略图功能
  • add_theme_support( 'post-thumbnails', array( 'post' ) ); //仅在post中使用缩略图功能
  • add_theme_support( 'post-thumbnails', array( 'page' ) );//仅在page中使用缩略图功能
  • add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); //仅在 post 和 movies 中使用 

缩略图显示使用the_post_thumbnail,判断文章是否有缩略图代码:

if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}

建站!你有充足的理由选择我们

世界上超过30%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress主题开发商
QQ咨询在线咨询问答互助微信号geekercode微信公众号云服务器