Here are a few useful code snippets to use with Cwicly
Dynamic menu (Polylang example)
While this is more geared towards Polylang, this can be applied to any type of condition.
Apply a specific menu ID with conditions to all Cwicly Menu blocks or targetted Menu blocks.
Make sure to change the Menu ID values to ones corresponding to menus on your installation.
/** * Modify a specific - or not - Cwicly Menu block menu ID based on various conditions. * Function using the `render_block_data` filter-hook. * * @paramarray $parsed_block The block being rendered. * @paramarray $source_block An un-modified copy of $parsed_block, as it appeared in the source content. * @paramWP_Block|null $parent_block If this is a nested block, a reference to the parent block. * * @returnarray */functioncc_render_block_data_filter($parsed_block, $source_block, $parent_block){// Make sure that we are only modifying the Cwicly Menu block and that it has attributes.if ($parsed_block['blockName'] ==='cwicly/menu'&&isset($parsed_block['attrs'])) {// Set conditions to true to target all Cwicly Menu blocks $conditions =true;/* // Optional: target specific menu with block ID if (isset($parsed_block['attrs']['id']) && $parsed_block['attrs']['id'] === 'menu-c413s33f') { $conditions = true; } else { $conditions = false; } */if ($conditions) {// Default language slug $lang_slug ='';// Get current language slug if Polylang is installedif (function_exists('pll_current_language')) { $lang_slug =pll_current_language('slug'); } else { $lang_slug =''; }// Set menu ID for each language slug (or default)if ($lang_slug ==='en') { $parsed_block['attrs']['menuSelected'] ="10"; // Menu ID } elseif ($lang_slug ==='fr') { $parsed_block['attrs']['menuSelected'] ="11"; // Menu ID } else {// Default menu ID $parsed_block['attrs']['menuSelected'] ="10"; // Menu ID } } }return $parsed_block;}add_filter('render_block_data','cc_render_block_data_filter',10,3);
Classic editor block <p> tags missing
If you don't have any other block on your post than one or multiple Classic Editor blocks, you might notice that no paragraph breaks are applied.
You can add the following code to your WordPress theme's functions.php file or use a custom snippet plugin to automatically apply <p> tags.
/** * Registers the apply_wpautop_if_no_blocks function as a filter for the the_content hook, * which means that it will be called for every post or page on your site. * * The function checks if the post has blocks, and if it does not, it applies wpautop to the content. */functioncc_apply_wpautop_if_no_blocks( $content ) {// Check if the post has blocksif ( has_blocks() ) {// The post has blocks, so don't apply wpautopreturn $content; } else {// The post has no blocks, so apply wpautopreturnwpautop( $content ); } }add_filter('the_content','cc_apply_wpautop_if_no_blocks');
Classic block image styles
The styles for Classic Editor block images aren't retained in the Cwicly Theme.
If you still use the Classic Editor and want to apply the default styling that comes with figures and images, add the following CSS snippet to a Cwicly global stylesheet.
/** * Function for `wp_get_nav_menu_items` filter-hook. * * @paramarray $items An array of menu item post objects. * @paramobject $menu The menu object. * @paramarray $args An array of arguments used to retrieve menu item objects. * * @returnarray */functioncc_cust_get_nav_menu_items_filter($items, $menu, $args){if (is_admin()) {return $items; }// Only apply changes to specific menu// if ($menu->term_id !== 33) {// return $items;// } $e =newstdClass(); $e->title ="A new Title"; $e->url ="/your-url"; $e->menu_order =500; $e->post_type ="nav_menu_item"; $e->type ="custom"; $e->object ="custom"; $e->object_id =0; $e->db_id =0; $e->menu_item_parent =0; $e->ID =mt_rand(); $e->xfn ="current"; $e->target =""; $e->post_excerpt =""; $e->classes = []; // CSS classes $items[] = $e;return $items;}add_filter('wp_get_nav_menu_items','cc_cust_get_nav_menu_items_filter',10,3);
Modify WooCommerce default number of products per page
A WooCommerce shop page can have up to 16 products by default. When Inheriting the Query from the URL, use the code snippet below to modify the number of products output for the query.
/** * If Inheriting the Query from URL, change the number of products that are displayed per page (shop page, cateogyr page, etc.)
*/add_filter('loop_shop_per_page','cc_loop_shop_per_page',20);functioncc_loop_shop_per_page($current){// $cols contains the current number of products per page $current =20; // Change this to your number of productsreturn $current;}