arcath/theme-options

定义主题选项并获取它们的类。

0.0.3 2023-02-08 09:58 UTC

This package is auto-updated.

Last update: 2024-09-08 13:47:57 UTC


README

轻松制作主题选项

安装

您可以从Composer安装 arcath/theme-options 或将 theme-options.php 放置到您的主题中并引入它。

用法

创建一个新的ThemeOptions实例

$themeOptions = new Arcath\ThemeOptions('slug');

init动作中定义所有字段,例如:

function slug_custom_fields(){
  global $themeOptions;

  $themeOptions->addThemeOptionCustomControl('logo_light',
    array(
      'type' => 'option',
      'default' => 0
    ),
    array(
      'section' => 'images',
      'label' => __('Light Logo', 'slug'),
      'description' => __('Light Logo used on darker backgrounds', 'glug'),
    ),
    'WP_Customize_Image_Control'
  );

  $themeOptions->addThemeOption('email',
    array(
      'capability' => 'edit_theme_options',
      'type'       => 'option',
      'default'    => 'foo@bar.com',
    ),
    array(
      'label' => 'Email',
      'section' => 'text',
      'description' => __('Your Email')
    )
  );
}

add_action('init', 'slug_custom_fields');

customize_register中创建您的部分/面板并应用主题选项。

function slug_apply_custom_fields($wp_customize){
  global $themeOptions;

  $wp_customize->add_panel('global', array(
    'priority' => 1,
    'capability'     => 'edit_theme_options',
    'theme_supports' => '',
    'title'          => __('Global Options', 'slug'),
    'description'    => __('Global options that affect every page.', 'edit2017'),
  ));

  $wp_customize->add_section(
    'images',
    array(
      'title' => 'Images',
      'description' => 'Images used throughout the Theme',
      'capability' => 'edit_theme_options',
      'panel' => 'global'
    )
  );

  $wp_customize->add_section(
    'text',
    array(
      'title' => 'Text',
      'description' => 'Text Fields',
      'capability' => 'edit_theme_options',
      'panel' => 'global'
    )
  );
}

add_action('customize_register', 'slug_apply_custom_fields');

然后在您的主题中,您可以使用以下方式获取任何选项

<?php
global $themeOptions;
echo($themeOptions->themeOption('email'));
?>

Theme Options 处理返回默认值,如果它在文章元数据中没有设置等...

每个选项在返回之前都会经过一个过滤器。这允许您根据页面模板等更改默认值...

<?php
// Template Name: RED
global $themeOptions;

add_filter('theme_slug_color', function($color){
  // Over ride option to red on this page
  return "#f00";
})

echo($themeOptions->themeOption('color'));
?>