it-swarm.cn

如何根据主题选项字段显示/隐藏某些内容?

我正在为我的主题构建一个主题选项页面,我已经设法使大部分工作正常。但现在我试图基于一个简单的复选框显示和隐藏我主题中的某些部分。一个例子:

显示精选内容滑块? - 是 - 不

当用户选择是时,我想在我的主题的某个模板中启用以下代码:

<?php locate_template( array( 'includes/slider.php'), true ) ?>

那么如何包装此代码以便仅在单击“是”时显示?我认为这是某种条件陈述,但我现在已经知道如何处理这个问题。由于我无法编写php,我需要一些帮助:)这个选项的名称叫做bpslick_featured。

提前致谢!

1
Bowe Frankema

@鲍

您需要在复选框的选项函数中创建一个数组,并为其指定一个默认状态,并为其指定“复选框”类型。这是一个示例,假设您已经为管理面板准备了代码

<?php
// Set variables for the options panel
$themename = "your_theme_name";
$themeshortname = "yt";
$mythemeoptions = array();

//The Option function
function cool_theme_options() {
global $themename, $themeshortname, $mythemeoptions;

$themeoptions = array (

array( "name" => __('Show featured content slider','your_theme_name'),
"desc" => __('When checked, the slider will be added to the home page.','your_theme_name'),
"id" => "show_featured_slider",
"std" => "false",
"type" => "checkbox"
   ),
 );
}

//The Option Form

function my_cool_theme_admin() {

  global $themename, $themeshortname, $themeoptions;

  // Saved or Updated message
  if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
  if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';

  // The form
  ?>

  <div class="wrap">
  <h2><?php echo $themename; ?> Options</h2>

  <form method="post">

  <?php wp_nonce_field('theme-save'); ?>
  <table class="form-table">

  <?php foreach ($themeoptions as $value) {

    // Output the appropriate form element
    switch ( $value['type'] ) {

      case 'text':
      ?>

      <tr valign="top">
        <th scope="row"><?php echo $value['name']; ?>:</th>
        <td>
          <?php foreach ($value['options'] as $key=>$option) {
            if ($key == get_option($value['id'], $value['std']) ) {
              $checked = "checked=\"checked\"";
            } else {
              $checked = "";
            }
            ?>
            <input type="radio" name="<?php echo $value['id']; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />
          <?php } ?>
          <?php echo $value['desc']; ?>
        </td>
      </tr>
      <?php
      break;

      case "checkbox":
      ?>
      <tr valign="top">
        <th scope="row"><?php echo $value['name']; ?></th>
        <td>
          <?php
          if(get_option($value['id'])){
            $checked = "checked=\"checked\"";
          } else {
            $checked = "";
          }
          ?>
          <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
          <?php echo $value['desc']; ?>
        </td>
      </tr>
      <?php
      break;

      default:
      break;
    }
  }
  ?>

  </table>

  <p class="submit">
    <input name="save" type="submit" value="Save changes" class="button-primary" />
    <input type="hidden" name="action" value="save" />
  </p>

  </form>

  <form method="post">
    <?php wp_nonce_field('theme-reset'); ?>
    <p class="submit">
      <input name="reset" type="submit" value="Reset" />
      <input type="hidden" name="action" value="reset" />
    </p>
  </form> 

接下来在functions.php中添加函数,如果选中该框,则调用滑块。

<?php

function cool_theme_slider_option() {
  // load the custom options
  global $themeoptions;
  foreach ($themeoptions as $value) {
    $$value['id'] = get_option($value['id'], $value['std']);
  }

     if ($show_featured_slider  == 'true') {

        locate_template( array( 'includes/slider.php'), true )

     }
} // end function

add_action('wp_head', 'cool_theme_slider_option');

?>

此代码尚未经过检查以确保其完全正常工作,但旨在显示在主题中使用复选框选项的示例。

2
Chris_O