Monthly Archives: July 2019

Keep mega menu open to style

/* mega menu - force menu 213 to stay open
#mega-menu-wrap-primary #mega-menu-primary[data-effect="fade_up"] li.mega-menu-item#mega-menu-item-213 > ul.mega-sub-menu { opacity: 1; transform: translate(0, 0); }
#mega-menu-wrap-primary #mega-menu-primary li.mega-menu-item#mega-menu-item-213 > ul.mega-sub-menu { visibility: visible !important; }
end force menu 213 open */

Shortcode to display child or sibling pages

/* shorcode for menu [wpb_childpages]
Show children (1 level), if no children show sibbings (unless top level page)
*/
function wpb_list_child_pages() {
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if ( is_page() && count( $children ) > 0 ) {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0&depth=1' ); // shows children
} else {
if ($post->post_parent == 0) { // $post->post_parent = 0 for top level (no parents) - don't show other top level pages!
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0&include=' . $post->ID ); // shows just this page
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' ); // shows siblings
}
}
if ( $childpages ) {
$string = '

    ' . $childpages . '

';
}

return $string;
}
add_shortcode('wpb_childpages', 'wpb_list_child_pages');

Make shortcode dynamic in Elementor

Problem adding shortcode with static value to template (which is used for all post or products etc) eg

[premmerce_get_bundles_by_main_product_id id="X"]

Create new shortcode, in corporating original shortcode:

function so_extend_frequent_bought_shortcode() {
global $product;
$id = $product->get_id();

return do_shortcode( '[premmerce_get_bundles_by_main_product_id id="' . $id . '"]');
}
add_shortcode( 'my_new_shortcode', 'so_extend_frequent_bought_shortcode' );

Similarily, to add dynamic Javascript to elementor product template:

// shortcode to add tab-click js to product pages
function redraw_data_table_shortcode( $atts ) {
global $product;
$id = $product->get_id();
$output = '
';
return $output;
}
add_shortcode( 'redrawdatatable', 'redraw_data_table_shortcode' );

News custom post type

// News custom post type
function cw_post_type_news() {

$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
//'comments', // post comments
'revisions', // post revisions
//'post-formats', // post formats
);

$labels = array(
'name' => _x('News', 'plural'),
'singular_name' => _x('News', 'singular'),
'menu_name' => _x('News', 'admin menu'),
'name_admin_bar' => _x('News', 'admin bar'),
'add_new' => _x('Add new', 'add new'),
'add_new_item' => __('Add new News'),
'new_item' => __('New News'),
'edit_item' => __('Edit News'),
'view_item' => __('View News'),
'all_items' => __('All News'),
'search_items' => __('Search News'),
'not_found' => __('No news found.'),
);

$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('with_front' => false, 'slug' => 'information/media/in-the-press'), // with_front=false do not use same slug as blog
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('news', $args);
}
add_action('init', 'cw_post_type_news');

Add animated scroll-to-ID functionality

// add scroll to ID functionality
function load_script_to_scroll_to_id(){
?>

Woocommerce use same base url for products and categories

// use same base url for products and categories
function wpse_291143_generate_taxonomy_rewrite_rules( $wp_rewrite ) {

global $wp_rewrite;
$base = "shop";

$rules = array();
$terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false ));

foreach($terms as $term) {
$term_children = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => intval($term->term_id),
'hide_empty' => false
)
);
if($term_children) {
foreach ( $term_children as $term_child ) {
$rules[$base . '/' . $term->slug . '/' . $term_child->slug . '/?$'] = 'index.php?product_cat=' . $term_child->slug;
}
}
$rules[$base . '/' . $term->slug . '/?$'] = 'index.php?product_cat=' . $term->slug;
}

$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'wpse_291143_generate_taxonomy_rewrite_rules');

Add “Products” to breadcrumb on woocommerce category pages

// add "Products" to breadcrumb on category pages and other missing levels
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' );
function wpseo_breadcrumb_add_woo_shop_link( $links ) {
global $post;
if ( is_woocommerce()
&& !is_shop() // not top level shop page
&& !is_product() // not on single product page
) {
$breadcrumb[] = array(
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ),
'text' => 'Products',
);
array_splice( $links, 1, -2, $breadcrumb );
} elseif ( is_post_type_archive( 'news' ) || is_singular( 'news' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
array_splice( $links, 1, -2, $breadcrumb );
} elseif ( is_home() ) { // any other archive...
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
array_splice( $links, 1, -2, $breadcrumb );

} elseif ( is_singular( 'post' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
$breadcrumb[] = array(
'url' => get_permalink( '54' ),
'text' => 'Blog',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}