Skip to content

Instantly share code, notes, and snippets.

@renventura
Last active October 19, 2016 13:46
Show Gist options
  • Save renventura/e00bfeb8f1afddf318c9 to your computer and use it in GitHub Desktop.
Save renventura/e00bfeb8f1afddf318c9 to your computer and use it in GitHub Desktop.
Insert Custom Fields into WordPress Shortcode
<?php //* Mind this opening PHP tag
/**
* This snippet will take a custom field and add its data to a shortcode.
* In this example, we are adding a few WooCommerce products before the content of a Genesis theme.
* The code is taking the product IDs entered in the home page's edit panel and dynamically inserting
* them into a WooCommerce shortcode, which then outputs the products with those IDs.
*
* Read more about this example and the process of adding custom fields into shortcodes at EngageWP.com
* http://www.engagewp.com/how-to-insert-custom-fields-into-shortcodes
*/
add_action( 'genesis_before_content', 'display_featured_products' );
function display_featured_products() {
global $post;
if ( is_front_page() ) {
$custom_ids = get_post_meta( $post->ID, 'featured_products_ids', true );
echo do_shortcode( '[products ids="' . $custom_ids . '"]' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment