Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php genesis框架中自定义元字段的条件显示_Php_Wordpress_If Statement_Echo_Genesis - Fatal编程技术网

Php genesis框架中自定义元字段的条件显示

Php genesis框架中自定义元字段的条件显示,php,wordpress,if-statement,echo,genesis,Php,Wordpress,If Statement,Echo,Genesis,我试图使我的网页仅在有数据显示时才在自定义元字段中显示数据。这就是我现在拥有的 我尝试了以下代码,但echo语句失败:标签“State”不显示,但显示了“genesis\u custom\u field('State')”的字段数据 <?php add_action( 'genesis_before_entry_content', 'state_meta' ); function state_meta() { if(genesis_custom_field('state'))

我试图使我的网页仅在有数据显示时才在自定义元字段中显示数据。这就是我现在拥有的

我尝试了以下代码,但echo语句失败:标签“State”不显示,但显示了“genesis\u custom\u field('State')”的字段数据

<?php
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(genesis_custom_field('state'))
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>

尝试以下方法:

<?php
// works on php 5.6
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(!empty(genesis_get_custom_field('state')))
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>

<?php
// works on php < 5.6
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(genesis_get_custom_field('state') != '')
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>

尝试以下方法:

<?php
// works on php 5.6
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(!empty(genesis_get_custom_field('state')))
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>

<?php
// works on php < 5.6
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(genesis_get_custom_field('state') != '')
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>


您的答案在我的Site5沙箱(使用PHP5.6.22)中非常有效,但当我在live site(非常不幸地在GoDaddy托管并使用PHP5.4)上尝试同样的方法时,网站崩溃了。我看到的错误是“致命错误:无法在中的写入上下文中使用函数返回值…”,然后它标识包含以if开头的代码的行(!empty…我猜这是一个PHP问题。你有什么建议吗?非常感谢!!!@user3044291将该行更改为:
,然后尝试:
,效果非常好。谢谢你,谢谢你,谢谢你花时间帮我解决这个问题!@user3044291欢迎。如果你接受答案并在可能的时候进行投票,那就太好了。你的答案很有效在我的Site5沙盒(使用PHP5.6.22)中非常完美,但当我在live site(非常不幸地托管在GoDaddy并使用PHP5.4)上尝试同样的事情时,该站点崩溃。我看到的错误是“致命错误:无法在写入上下文中使用函数返回值…”,然后它标识包含以if开头的代码的行(!empty…我猜这是一个PHP问题。你有什么建议吗?非常感谢!!!@user3044291将该行更改为:
,然后尝试:
,效果非常好。谢谢你,谢谢你,谢谢你花时间帮我解决这个问题!@user3044291欢迎。如果你能接受答案并在可能的时候进行投票,那就太好了。