php
  • wordpress
  • syntax
  • checked
  • 2017-05-17 35 views 1 likes 
    1

    只有几个星期的年龄,我建立了一个简单的插件来显示菜单中的社交分享按钮。简称Building Your Own Social Sharing Plugin for WordPressWordPress的检查=“检查”的语法

    然后,插件成功创建。但我有一个语法问题,为什么如果我在程序中添加了以下语法,checked =“checked”将显示两次。

    echo "<input type='checkbox' name='social-share' value='1' " . checked(1, get_option('social-share'), true) ." /> "; 
    

    之后,我发现了另一个代码来防止这种情况发生。

    ?> 
        <input type="checkbox" name="social-share" value="1" <?php checked(1, get_option('social-share'), true); ?> /> Check for Yes 
    <?php 
    

    Function Reference/checked displaying checked='checked'

    checked('1', get_option('wwo_enable_'.$lrole), false) 
    

    我想知道如果任何人都可以使用,为什么HTML格式来显示<?php checked(1, get_option('social-share-wechat'), true); ?>语法不会出现两次告诉我?

    回答

    0

    您看到意外的结果,因为您正在回显已经生成输出的函数。

    checked()的第三个参数是'echo',它确定函数是应该输出一个值还是返回它。你所需要的功能,你在使用它的上下文返回一个值

    echo '<input type="checkbox" name="social-share" value="1" ' . checked(1, get_option('social-share'), false) . '/>'; 
    

    https://codex.wordpress.org/Function_Reference/checked

    的代码,你带手柄不同的事情取而代之。

    <input type="checkbox" name="social-share" value="1" <?php checked(1, get_option('social-share'), true); ?> /> 
    

    这里你不再回应checked()的结果,因此您确实希望它生成输出,因此将第三个参数设置为true是正确的。

    +0

    嗨@Nathan道森,感谢您的答复。你的意思是回显'checked(1,get_option('social-share'),true)'会返回另一个回显结果,这样它会显示** checked =“checked”**两次? – Ilove112

    +0

    在'checked()'中设置第三个参数'echo'为true时,它将输出检查的字符串。您在echo语句中使用它,因此您需要返回值而不是输出。 –

    +0

    我明白了。非常感谢。 – Ilove112

    0

    请你尝试下面的事情:

    <input type="checkbox" name="social-share" value="1" <?php if (1 == get_option('social-share')) echo 'checked="checked"'; ?> > 
    
    +0

    您发布的代码是'checked()'函数存在的全部原因。 –

    相关问题