As far as I know, this can’t be done with Elementor.
See PHP’s empty() function for how to check if a value is blank: https://www.php.net/manual/en/function.empty.php
e.g.,
<?php
$value = get_post_meta( get_the_ID(), 'some_field', true );
if ( empty( $value ) ) {
printf( 'The value of some_field is empty.' );
}else {
printf(
'The value of some_field is %s.',
esc_html( $value )
);
}
Hi @pdclark,
Can you tell me where exactly I should place this code and also – what is the purpose of the code? Will it solve my issue?
Thanks.
It depends on your template.
Using Elementor, you can create and use a shortcode using add_shortcode: https://developer.ww.wp.xz.cn/reference/functions/add_shortcode/ , then using the shortcode in Elementor.
If you’re having trouble reading the purpose of the lines above or the documentation, then it may be time to seek professional help.
The code can be placed directly into a PHP or Pods template, or can be wrapped in a shortcode and placed in a plugin or your theme’s functions.php
@monologuehq The following code, if installed to a plugin, for example at wp-content/plugins/shortcode-empty/shortcode-empty.php, will add a shortcode [empty]. Its usage is [empty field="field_name" before="Prefix: " after=" ...suffix." empty="Value empty."], where the field with name “field_name” will display if not empty with optional “before” and “after” content, otherwise the contents of empty="This will display if empty." will display instead:
<?php
/**
* Plugin Name: Shortcode — Empty
* Description: Display a field if not empty, otherwise display alternative text. Set "empty" to <code>""</code> (blank) to display nothing if empty. Usage: <code>[empty field="field_name" before="Prefix: " after=" ...suffix." empty="Value empty."]</code>
* Plugin Author: 𝜑δ•ℂᴹ
* Plugin URI: https://pd.cm/
* Version: 1
*/
add_action(
'plugins_loaded',
function(){
add_shortcode(
'empty',
function( $atts ) {
ob_start();
if ( ! isset( $atts['before'] ) || empty( $atts['before'] ) ) {
$atts['before'] = '';
}
if ( ! isset( $atts['after'] ) || empty( $atts['after'] ) ) {
$atts['after'] = '';
}
if ( ! isset( $atts['empty'] ) || empty( $atts['empty'] ) ) {
$atts['empty'] = '';
}
$value = get_post_meta( get_the_ID(), $atts['field'], true );
if ( ! empty( $value ) ) {
printf(
'%s%s%s',
$atts['before'],
esc_html( $value ),
$atts['after']
);
}else {
printf(
'%s',
$atts['empty']
);
}
return ob_get_clean();
}
);
}
);