WordPress 条件自定义字段

发布于 2024-10-06 19:27:21 字数 872 浏览 0 评论 0原文

我想在 WordPress 安装的单页中输出自定义字段。 基本上,我想在页面中输出相关的创意通用许可类型。因此,基本上,带有 cc-license 键的自定义字段可能只有一个值,但该值必须是六个创意通用许可证之一。

假设我已经插入了密钥 cc-license 的值。这是我的代码:

<?php 
$nilai = get_post_meta($post->ID, 'cc-license', true);
echo $nilai; //just want to check the output,its ok!
if ($nilai = 'Attribution Non-commercial Share Alike') { ?>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>                    
<?php } ?>

问题是,如果我将值更改为任何文本,它仍然会实现条件语句中的 HTML 代码。哪一部分是错误的?

I want to output a custom field in my single page of my WordPress install.
Basically, I want to output the related creative common license type in the page. So basically the custom field with key cc-license may have one value only, but the value must be one of the six creative common licenses.

Assuming I already insert the value for the key cc-license. So here is my code:

<?php 
$nilai = get_post_meta($post->ID, 'cc-license', true);
echo $nilai; //just want to check the output,its ok!
if ($nilai = 'Attribution Non-commercial Share Alike') { ?>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>                    
<?php } ?>

The problem is, if I change the value to any text, it still implement the HTML code in the conditional statement. Which part is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2024-10-13 19:27:21

试试这个,我想你可能刚刚错过了一个=

<?php 
    $nilai = get_post_meta($post->ID, 'cc-license', true);

    echo $nilai; //just want to check the output,its ok!

    if ($nilai == 'Attribution Non-commercial Share Alike') : ?>

        <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br />
        This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>  

    <?php endif;
?>

Try this, I think you might have just missed a =

<?php 
    $nilai = get_post_meta($post->ID, 'cc-license', true);

    echo $nilai; //just want to check the output,its ok!

    if ($nilai == 'Attribution Non-commercial Share Alike') : ?>

        <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br />
        This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>  

    <?php endif;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文