Created
November 6, 2018 21:20
-
-
Save sunnymui/69e829cc8abb0d44151a23011e0fc9e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Add Shortcode | |
function show_current_corresponding_shortcode_block() { | |
/* | |
Used for the woocommerce product page under the Flatsome theme. Adds a shortcode that when used, looks at the current product's categories, specifically looking at a designated parent category, then going through the subcategories to see which one is marked. It assumes that marked subcategory contains the id info you'll use in a shortcode to display a certain block of content that corresponds to it. It takes the first marked subcategory, inserts the desired id info (I'm using the slug here) into the shortcode template for a block, then returns a shortcode execution function to run that shortcode with the info it found. Note, this assumes you have a bunch of blocks already created with a standardized block naming pattern that'll fit the template and match category slugs to the block shortcodes. | |
Args: na | |
Return: function invocation that executes a shortcode for a specific block (obj) | |
*/ | |
// grab the current product's id | |
$current_product = get_the_ID(); | |
// block shortcode template that we'll print with corresponding name in there | |
$block_template = '[block id="some-block-ID"]'; | |
// placeholder to replace w/ actual category info in the block template | |
$placeholder = 'ID'; | |
// the id of the parent category to check for marked subcategories | |
// change the id to the parent category you want on your site | |
$parent_cat = 104; | |
// init var to store category identifier | |
$cat_identifier = ""; | |
// exclude a specific category from being shown if you want | |
$exclude_id = 'our-business'; | |
// get the product category terms of current product | |
$terms = get_the_terms( $current_product, 'product_cat' ); | |
// ensure categories have been set/are valid | |
if ( $terms && ! is_wp_error( $terms ) ) { | |
// loop through all the product's categories | |
foreach ($terms as $term) { | |
// if parent category of a category is the one we want | |
if ($term-> parent == $parent_cat) { | |
// get the category identifier info you want to use in the shortcode | |
// I used the slug because it's easy and already formatted | |
$cat_identifier = $term-> slug; | |
// check if this needs to be excluded | |
if ($cat_identifier = $exclude_id) { | |
// blank out the block template so it won't show if nothing found | |
$block_template = ''; | |
} else { | |
// replace the placeholder with found category info in the block template | |
$block_template = str_replace($placeholder, $cat_identifier, $block_template); | |
} | |
// exit loop so it only gets the first match | |
break; | |
} | |
} | |
} | |
// execute the corresponding shortcode | |
return do_shortcode($block_template); | |
} | |
// add the shortcode so you can use it in the post editor | |
// use it by just writing the first parameter in brackets: [show-corresponding-block] | |
add_shortcode( 'show-corresponding-block', 'show_current_corresponding_shortcode_block' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
conditional block shortcode insertion for flatsome and woocommerce