简体   繁体   English

我如何让我的自定义字段仅在其中写入了某些内容时才显示-Wordpress?

[英]How do I get my custom fields to only show when something is written in them - Wordpress?

Im working on customizing a Wordpress theme with some custom fields. 我正在使用一些自定义字段自定义Wordpress主题。 For this I'm using the plugin "Advanced Custom Fields", but I want it to display these fields ONLY IF something is written in them. 为此,我使用的是“高级自定义字段”插件,但我希望它仅在其中写入了某些内容时才显示这些字段。 This is the code I'm using to display the custom fields: 这是我用来显示自定义字段的代码:

<p class="tittelboks">Artikkelforfatter:</p>
        <?php if( get_field('artikkelforfatter') )
        {
        echo '<p>' . get_field('artikkelforfatter') . '</p>';
        } ?>

How do I change the code so that it only echos the information AND the label (in this case .tittelboks) if something is written in the meta boxes? 如果在meta框中写了一些内容,如何更改代码,使其仅回显信息和标签(在本例中为.tittelboks)?

Michael 麦可

<?php if( $field = get_field('artikkelforfatter') ): ?>
    <p class="tittelboks">Artikkelforfatter:</p>
    <p><?php echo $field; ?></p>
<?php endif; ?>

This is a different way of doing an if statement, so you don't have to enclose your HTML in quotes and worry about escaping. 这是执行if语句的另一种方式,因此您不必将HTML括在引号中,也不必担心转义。 The two lines in the middle will only print out if get_field('artikkelforfatter') returns a value or true. 仅当get_field('artikkelforfatter')返回一个值或true时,中间的两行才会打印出来。 That value will be assigned to the $field variable. 该值将分配给$field变量。

Something like this should work: 这样的事情应该起作用:

<?php
$artikkel = get_field( 'artikkelforfatter' );

if ( ! empty( $artikkel ) ) {
?>
<p class="tittelboks">Artikkelforfatter:</p>
<p><?php echo $artikkel; ?></p>
<?php
}
?>

To use and display custom fields need you may try to put this in your loop. 要使用和显示自定义字段,您可以尝试将其放入循环中。

<?php $what_name_you_want=get_post_meta($post->ID,'Your Custom Field Name',true); ?>

    <?php echo $what_name_you_want; ?>// This call the value of custom field

Only replace 仅更换

what_name_you_want with your favorite name what_name_you_want和您喜欢的名字

and

Your Custom Field Name with the name of custom field 您的自定义字段名称和自定义字段的名称

If the value of custom field is empty the echo will be empty to. 如果自定义字段的值为空,则回显将为空。

Tell me if it's work 告诉我是否有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不破坏下面的自定义字段的情况下让自定义帖子类型显示在存档页面上? - How do I get Custom Post Types to show up on Archive pages without breaking my Custom Fields below? 仅当满足某些条件时,如何才能从数据库中获取要显示的日期? - How do I get a date from my database to show, only when certain conditions are met? 如何仅在Firefox和Explorer上显示内容? - How do I make something show only on Firefox and Explorer? 我如何显示所有字段的价值? - How do i show value all fields?Wordpress 如何从Wordpress自定义字段调用超链接? - How can i call hyperlink from my wordpress custom fields? 高级自定义字段在类别页面上显示不正确。 我如何只限于分类页面? - Advanced custom fields displaying incorrectly on category pages. How do i limit to my taxonomy page only? 如何让我的整个页脚显示在所有页面模板上? 它目前只出现在主页上。 使用wordpress - How do I get my entire footer to show up on all page templates? It is currently only showing up on the home page. Using wordpress 如何在wordpress网站上获取博客页面,以在帖子编辑器中显示文本和图像,使它们看起来相同? - How do I get the blog page on my wordpress site to show the text and images in the post editor to look the same? 在WordPress中,我怎样才能在页面中显示具有子页面的内容? - In WordPress, how can I only show something in a page has children pages? 我可以在wordpress中为自定义字段编写自定义批量操作吗? 如果是,那有什么方法呢? - Can i write custom bulk action for my custom fields in wordpress? if yes, what are the ways to do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM