Custom Taxonomy in plugin and template

First of all – plugins are for generating content, themes are for displaying it. So really, a plugin shouldn’t do this. But there are grey areas – for example in an ‘events’ related plugin, it would be desirable to display dates, venue etc – things that a WordPress theme wouldn’t normally display. I would suggest … Read more

Make all subcategories use the template of its category parent?

Here is a code I used to do it: // make category use parent category template function load_cat_parent_template($template) { $cat_ID = absint( get_query_var(‘cat’) ); $category = get_category( $cat_ID ); $templates = array(); if ( !is_wp_error($category) ) $templates[] = “category-{$category->slug}.php”; $templates[] = “category-$cat_ID.php”; // trace back the parent hierarchy and locate a template if ( !is_wp_error($category) … Read more

Preventing index.php?category_name=something from redirecting

This should work: add_action( ‘init’, ‘wpa12742_init’ ); function wpa12742_init(){ add_rewrite_rule( ‘category/(.+?)/(\d{4})/?$’, ‘index.php?category_name=$matches[1]&year=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘category/(.+?)/(\d{4})/page/(\d+)/?$’, ‘index.php?category_name=$matches[1]&year=$matches[2]&paged=$matches[3]’, ‘top’ ); } EDIT On second thought, that’s not enough, since you’ll get caught by redirect_canonical(). Add this too: add_filter( ‘term_link’, ‘wpa12743_term_link’, 10, 3 ); function wpa12743_term_link( $link, $term, $taxonomy ){ if(‘category’ != $taxonomy && !preg_match( ‘@^\d{4}$@’, get_query_var(‘year’) … Read more

Displaying a WooCommerce product via PHP

Use do_shortcode(). For example, in a template, if you were wanting to display products specifically by ID: <?php echo do_shortcode(‘[products ids=”1, 2, 3, 4, 5″]’); ?> WooCommerce comes with several shortcodes which can be used to insert content inside posts and pages: http://docs.woothemes.com/document/woocommerce-shortcodes/ You can add shortcodes to a post or page easily via the … Read more

Loading custom page template via plugin

To add custom template in page attributes template section you have to first add your template to dropdown and load it in template_include hook when current page has selected it as current template. /** * Add “Custom” template to page attirbute template section. */ function wpse_288589_add_template_to_select( $post_templates, $wp_theme, $post, $post_type ) { // Add custom … Read more