如何根据自定义字段的内容隐藏div

时间:2016-03-15 作者:fbb

我发现其他几个帖子也在问同样的问题,但出于某种原因,我似乎无法找到适合我的解决方案。为了澄清,我希望有一个应用于帖子但不包含任何值的自定义字段,以便我可以设置一个模板帖子(在本例中为公文包项目),并简单地复制它,但在字段不适用的地方省略值,当然,如果没有值,我不希望显示相关的html。所以,我得到的是:

<?php $fbb_CurrentMetaSet = get_post_meta($post->ID, \'fbb_ProjectData_Process\', false);
            if (!empty($fbb_CurrentMetaSet)){?>
            <div id="fbb_ProjectData_Process" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Process:</h5></div>
                <div>
                        <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                            echo \'<div>\'.$fbb_MetaDataSingle.\'</div>\'; 
                        }?> 
                </div>
            </div>
            <?php } ?>
如果我只检查自定义字段是否存在(if($fbb\\u CurrentMetaSet)),则此代码可以工作,但由于某些原因!empty()方法无效。有人能解释为什么吗?仅供参考,我正在使用最新的wordpress和x主题。

可能是更大的上下文产生了一个解释,所以下面我粘贴了整个相关代码集。我的测试代码可以在最后一节中找到。一旦我让它工作起来,我打算将其复制到上面的类似部分:

<div id="fbb_ProjectDataWrap">
            <div id="fbb_ProjectData_Client" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Client:</h5></div>
                <ul>
                <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, \'fbb_ProjectData_Client\', false);?>
                    <?phpforeach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                        echo \'<li>\'.$fbb_MetaDataSingle.\'</li>\'; 
                    }
                }?>
                </ul>
            </div>
            <div id="fbb_ProjectData_Tools" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Tools:</h5></div>
                <ul>
                <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, \'fbb_ProjectData_Tools\', false); ?>                    
                    <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                        echo \'<li>\'.$fbb_MetaDataSingle.\'</li>\';
                    } ?>
                </ul>
            </div>
            <div id="fbb_ProjectData_About" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>About:</h5></div>
                <div>
                <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, \'fbb_ProjectData_AboutTheProject\', false); ?>                  
                    <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                        echo \'<div>\'.$fbb_MetaDataSingle.\'</div>\';
                    } ?>
                </div>
            </div>
            <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, \'fbb_ProjectData_Process\', false);
            if (!empty($fbb_CurrentMetaSet)){?>
            <div id="fbb_ProjectData_Process" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Process:</h5></div>
                <div>
                        <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                            echo \'<div>\'.$fbb_MetaDataSingle.\'</div>\'; 
                        }?> 
                </div>
            </div>
            <?php } ?>
        </div>

2 个回复
最合适的回答,由SO网友:vancoder 整理而成

您的第三个参数get_post_meta() 设置为false. 这意味着它将返回一个数组。即使您没有为这个自定义字段设置值,它仍然会在DB-so中记录一个数组元素empty() 将返回false。

尝试切换到true 第三个参数。应该返回一个空字符串。

SO网友:Pieter Goosen

我认为我们可以做得更好更安全一些。我们可以一次性获得所有的post meta(这只是让你的代码干涸一点,请参见this post on custom field performance)。

我们可以尝试以下操作:(代码在执行过程中会被注释,请注意,所有代码都未经测试)

选项1-呼叫get_post_meta() 一次

$meta   = get_post_meta( get_the_ID() );

// First make sure we have data before continuing
if ( $meta ) :
    // Get your field values and set defaults
    $fields            = [];
    $field_names       = [\'Client\', \'Tools\', \'About\', \'Process\'];
    foreach ( $field_names as $field_name ) {
        if ( \'About\' === $field_name ) {
            $name = \'AboutTheProject\';
        } else {
            $name = $field_name;
        }
        $fields[$field_name] = ( isset( $meta[\'fbb_ProjectData_\' . $name] ) )
            ? filter_var( $meta[\'fbb_ProjectData_\' . $name][0], FILTER_SANITIZE_STRING ) 
            : \'\';
    }

    // Lets make sure we have at least one key/value pair with avalue
    if ( array_filter( $fields ) ) : ?>
        <div id="fbb_ProjectDataWrap">
        <?php // Lets loop through $fields and display them
        foreach ( $fields as $key=>$field ) : 
            // Make sure $field is not empty, if so, continue
            if ( !$field )
                continue;

            // We have values, lets display them
            ?>

            <div id="fbb_ProjectData_<?php echo $key; ?>" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title">
                    <h5><?php echo $key; ?>:</h5>
                </div>
                <ul>
                    <li>
                        <div>
                            <?php echo $field; ?>
                        </div>
                    </li>
                </ul>
            </div>
        <?php endforeach; ?> 
        </div>
    <?php
    endi
f;endif;

选项2-呼叫get_post_meta() 对于每个键,因为自定义字段是缓存的,如果调用get_post_meta() (参见链接帖子),我们可以get_post_meta() 对于每个键。只需记住设置$singletrue

// Get your field values and set defaults
$fields            = [];
$field_names       = [\'Client\', \'Tools\', \'About\', \'Process\'];
foreach ( $field_names as $field_name ) {
    if ( \'About\' === $field_name ) {
        $name = \'AboutTheProject\';
    } else {
        $name = $field_name;
    }
    $fields[$field_name] = filter_var( 
        get_post_meta( get_the_ID(), \'fbb_ProjectData_\' . $name, true ), 
        FILTER_SANITIZE_STRING 
    );
}

// Lets make sure we have at least one key/value pair with avalue
if ( array_filter( $fields ) ) : ?>
    <div id="fbb_ProjectDataWrap">
    <?php // Lets loop through $fields and display them
    foreach ( $fields as $key=>$field ) : 
        // Make sure $field is not empty, if so, continue
        if ( !$field )
            continue;

        // We have values, lets display them
        ?>

        <div id="fbb_ProjectData_<?php echo $key; ?>" class="fbb_ProjectDataSetSection">
            <div class="fbb_Title">
                <h5><?php echo $key; ?>:</h5>
            </div>
            <ul>
                <li>
                    <div>
                        <?php echo $field; ?>
                    </div>
                </li>
            </ul>
        </div>
    <?php endforeach; ?> 
    </div>
<?php
endif;

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: