dqyy38265 2012-04-28 14:20
浏览 38
已采纳

WordPress选项页面复选框 - 如何转换为条件注释?

I'm working on a WordPress theme and I've created an options page that is already setup and functioning for inputs and textareas. Which can easily be carried to the template pages with:

<?php $options = get_option('kittytheme_options'); echo $options['kittens']; ?>

Now, how would one modify that to work as a checkbox conditional? The following is probably wrong in many ways, but should illustrate what I'm trying:

<?php $options = get_option('kittytheme_options'); if ( $options['kittycheckbox'] == '0' ) : ?>Theme By: <a href="http://someurl.com/">Company Name</a><?php endif; ?>

So basically the idea is that the link should show by default being equal to 0 or nothing. But, when the checkbox to hide this area is checked in the options it would equal 1 therefore hiding it.

From the options page/form here's the checkbox:

<input id="kittytheme_options[kittycheckbox]" name="kittytheme_options[kittycheckbox]" type="checkbox" value="1" <?php checked( '1', $options['kittycheckbox'] ); ?> />
<label class="description" for="kittytheme_options[kittycheckbox]"><?php _e( 'Hide Kitty Credit', 'kittytheme' ); ?></label>

And the final validation function is:

function theme_options_validate( $input ) {
global $select_options;
if ( ! isset( $input['kittycheckbox'] ) )
$input['kittycheckbox'] = null;
$input['kittycheckbox'] = ( $input['kittycheckbox'] == 1 ? 1 : 0 );
return $input;
}
  • 写回答

2条回答 默认 最新

  • douzhiji2020 2012-04-29 10:05
    关注

    UPDATED:

    Your code is almost good exept for the theme_options_validate function that still work but it coded in a wrong fashion; below how it should be:

    function theme_options_validate( $input ) {
       $input['hidecred'] = isset($input['hidecred']) ? 1: 0;
       return $input;
    }
    

    So i guess that the real problem is that you are not including the conditional statement in all the desired WP pages. most probably you are including it only in page.php and not in page-blog.php and so on...

    $options = get_option('startuppro_options');
    if ( $options['hidecred'] ){
      // do something here
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分