Last active
October 19, 2016 13:46
-
-
Save renventura/e00bfeb8f1afddf318c9 to your computer and use it in GitHub Desktop.
Insert Custom Fields into WordPress Shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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