Get Custom Field Values Shortcode

Below, you’ll find a detailed guide on how to add the Get Custom Field Values Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Get Custom Field Values Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Get Custom Field Values Plugin and the shortcodes it provides:

Plugin Icon
Get Custom Field Values

"Get Custom Field Values is a powerful WordPress plugin designed to retrieve and display custom field values directly from your posts, pages, or custom post types."

★★★★★ (4) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [get_custom_field_values]

Get Custom Field Values [get_custom_field_values] Shortcode

The get-custom-field-values shortcode enables you to fetch and display custom field values from your posts. It’s versatile, allowing you to specify the post ID, field, and output format. This PHP code defines the shortcode function. It first checks if the author has permission to use shortcodes. It then sets default values and overwrites them with any provided in the shortcode. It determines the type of post data to retrieve (current, random, or specific) and formats the output accordingly. If ‘id’ or ‘class’ attributes are defined, the output is wrapped in a span tag.

Shortcode: [get_custom_field_values]

Parameters

Here is a list of all possible get_custom_field_values shortcode parameters and attributes:

  • post_id – specifies the ID of the post to fetch custom field values from
  • field – the name of the custom field to be retrieved
  • before – text or HTML to be placed before the custom field value
  • after – text or HTML to be placed after the custom field value
  • none – text to display if no custom field value is found
  • between – text to be placed between multiple custom field values
  • before_last – text to be placed before the last custom field value in a list
  • limit – limits the number of custom field values to be retrieved
  • this_post – fetches custom field values from the current post
  • random – fetches custom field values from a random post
  • id – adds a unique ID to the span tag wrapping the output
  • class – adds a custom CSS class to the span tag wrapping the output

Examples and Usage

Basic example – Displaying custom field value of the current post

[get_custom_field_value field="my_custom_field" /]

Advanced examples

Displaying custom field value of a specific post with post ID and wrapping it with a CSS class

[get_custom_field_value post_id="123" field="my_custom_field" class="my_class" /]

Displaying custom field value of a specific post with post ID, applying a custom ‘before’ and ‘after’ text, and defining text to display if no value is found

[get_custom_field_value post_id="123" field="my_custom_field" before="Start: " after=" End" none="No value found" /]

Displaying random custom field value from any post and wrapping it with a CSS ID and class

[get_custom_field_value random="true" field="my_custom_field" id="my_id" class="my_class" /]

Displaying recent custom field value from any post and limiting the number of values to display

[get_custom_field_value recent="true" field="my_custom_field" limit="5" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [get_custom_field_values] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( $this->shortcode,         array( $this, 'shortcode' ) );

Shortcode PHP function:

function shortcode( $atts, $content = null ) {
		$defaults = array();

		if ( ! $this->can_author_use_shortcodes() ) {
			return '';
		}

		foreach ( $this->widget_handler->get_config() as $opt => $values ) {
			// Unlike widgets, shortcodes generally exist in the context of a post, so
			// override widget default.
			if ( 'this_post' === $opt ) {
				$values['default'] = true;
			}

			$defaults[ $opt ] = isset( $values['default'] ) ? $values['default'] : '';
		}

		$atts2 = shortcode_atts( $defaults, $atts );

		foreach ( array_keys( $this->widget_handler->get_config() ) as $key ) {
			$$key = $atts2[ $key ];
		}

		$ret = '';

		if ( $post_id ) {
			if ( 'current' == $post_id ) {
				$ret = c2c_get_current_custom( $field, $before, $after, $none, $between, $before_last );
			} elseif ( $random ) {
				$ret = c2c_get_random_post_custom( $post_id, $field, $limit, $before, $after, $none, $between, $before_last );
			} else {
				$ret = c2c_get_post_custom( $post_id, $field, $before, $after, $none, $between, $before_last );
			}
		} else {
			if ( $this_post ) {
				$ret = c2c_get_custom( $field, $before, $after, $none, $between, $before_last );
			} elseif ( $random ) {
				$ret = c2c_get_random_custom( $field, $before, $after, $none );
			} else {
				$ret = c2c_get_recent_custom( $field, $before, $after, $none, $between, $before_last, $limit );
			}
		}

		// If either 'id' or 'class' attribute was defined, then wrap output in span
		if ( $ret && ( $id || $class ) ) {
			$tag = '<span';
			if ( $id ) {
				$tag .= ' id="' . esc_attr( $id ) . '"';
			}
			if ( $class ) {
				$tag .= ' class="' . esc_attr( $class ) . '"';
			}
			$tag .= ">$ret</span>";
			$ret = $tag;
		}

		return $ret;
	}

Code file location:

get-custom-field-values/get-custom-field-values/get-custom.shortcode.php

Conclusion

Now that you’ve learned how to embed the Get Custom Field Values Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *