[英]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.