├── README.md ├── baseplugin.php ├── bootstrap_navwalker_images.php ├── custom-post-type.php ├── functions.php ├── gallery-html-custom.php ├── gallery-loop.php ├── image-background.php ├── loop-custom.php ├── loop-pagination.php ├── loop-template-filter.php ├── loop.php ├── shortcode.php ├── taxonomy-menu.php ├── taxonomy.php └── woocommerce-optimize-enque-scripts-styles.php /README.md: -------------------------------------------------------------------------------- 1 | # WordPress Snippets Snippets 2 | 3 | A collection of WordPress utilities by [MarchettiDesign.net](http://www.marchettidesign.net/) 4 | 5 | 6 | ## List of the utilities 7 | 8 | - Custom Post Type 9 | 10 | - Basic Functions.php 11 | 12 | - Gallery Loop 13 | 14 | - Image Background Single Post / Page 15 | 16 | - Loop Custom 17 | 18 | - Loop with Pagination 19 | 20 | - Loop with filter based on Template selected 21 | 22 | - Loop Standard 23 | 24 | - Taxonomy Menu 25 | 26 | - Taxonomy 27 | 28 | - Woocommerce Scripts Optimizations 29 | 30 | 31 | ## Usage 32 | Copy and paste the snippets. 33 | 34 | ## Licensing 35 | MIT © 2016 [Andrea Marchetti](http://www.marchettidesign.net/) 36 | -------------------------------------------------------------------------------- /baseplugin.php: -------------------------------------------------------------------------------- 1 | '. __( 'Contact us now!', 'bsp' ) .'

'; 63 | } 64 | add_action( 'wp_body_open', 'bsp_header_code' ); 65 | 66 | 67 | /* 68 | 69 | HOOKS 70 | 71 | Modo per aggiungere o modificare codice all'interno di wordpress in punto definito 72 | es. hooks per aggiungere codice dopo il body, nel footer, hooks per comabiare il titolo delle pagine wordpress 73 | 74 | 2 tipi di hooks 75 | 76 | - actions inseriscono codice in un punto preciso di wordpress https://developer.wordpress.org/plugins/hooks/actions/ 77 | - filtri filtrano dati per esempio (titolo o il content) facendo qualcosa https://developer.wordpress.org/plugins/hooks/filters/ 78 | 79 | */ 80 | 81 | /* filter */ 82 | function bsp_change_and_link_excerpt( $more ) { 83 | 84 | return $more . ' Leggi tutto »'; 85 | } 86 | add_filter( 'excerpt_more', 'bsp_change_and_link_excerpt', 999 ); 87 | 88 | 89 | /* Action */ 90 | function bsp_add_content_footer() { 91 | echo 'Scrivici su whatsapp!'; 92 | } 93 | add_action( 'wp_footer', 'bsp_add_content_footer' ); 94 | 95 | 96 | /* 97 | 98 | SHORTCODE 99 | 100 | Mode per inserire codice dentor al content di wordpress in maniera dinamica: https://developer.wordpress.org/plugins/shortcodes/ 101 | 102 | Esempio Slider custom, o testo dinamico: 103 | 104 | */ 105 | 106 | 107 | function bsp_mex_natale(){ 108 | 109 | $deliver = Date('d.m.y', strtotime('+3 days')); 110 | 111 | return ' 🚚 Ordina oggi e ricevi il '. $deliver.''; 112 | 113 | } 114 | 115 | add_shortcode('mex-natale','bsp_mex_natale'); 116 | 117 | 118 | 119 | /* ! Shortcode Slider 120 | ------------------------------------------------------------------------- */ 121 | function bsp_slider() { 122 | 123 | 124 | $custom_loop = new WP_Query( array( 125 | 'post_type' => 'post', 126 | 'posts_per_page'=> 6, 127 | )); 128 | 129 | 130 | $buffer = '
'; 131 | $buffer .= '
'; 132 | $buffer .= ''; 149 | $buffer .= '
'; 150 | $buffer .= '
'; 151 | 152 | return $buffer; 153 | 154 | 155 | } 156 | // register shortcode 157 | add_shortcode('slider', 'bsp_slider'); 158 | 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /bootstrap_navwalker_images.php: -------------------------------------------------------------------------------- 1 |
\n"; 25 | } 26 | 27 | /** 28 | * @see Walker::start_el() 29 | * @since 3.0.0 30 | * 31 | * @param string $output Passed by reference. Used to append additional content. 32 | * @param object $item Menu item data object. 33 | * @param int $depth Depth of menu item. Used for padding. 34 | * @param int $current_page Menu item ID. 35 | * @param object $args 36 | */ 37 | public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { 38 | $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; 39 | 40 | /** 41 | * Dividers, Headers or Disabled 42 | * ============================= 43 | * Determine whether the item is a Divider, Header, Disabled or regular 44 | * menu item. To prevent errors we use the strcasecmp() function to so a 45 | * comparison that is not case sensitive. The strcasecmp() function returns 46 | * a 0 if the strings are equal. 47 | */ 48 | if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) { 49 | $output .= $indent . ''; 199 | $fb_output .= ''; 200 | 201 | if ( $container ) 202 | $fb_output .= ''; 203 | 204 | echo $fb_output; 205 | } 206 | } 207 | } 208 | 209 | add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2); 210 | 211 | function ad_filter_menu($sorted_menu_objects, $args) { 212 | 213 | // check for the right menu to filter 214 | // here we check for the menu with name 'Posts Menu' 215 | // given that you call it in you theme with: 216 | // wp_nav_menu( array( 'menu' => 'Posts Menu' ) ); 217 | // if you call the menu using theme_location, eg: 218 | // wp_nav_menu( array( 'theme_location' => 'top_manu' ) ); 219 | // check for: 220 | if ($args->theme_location != 'header') 221 | //if ($args->menu != 'Main Menu') 222 | return $sorted_menu_objects; 223 | 224 | // edit the menu objects 225 | foreach ($sorted_menu_objects as $menu_object) { 226 | // searching for menu items linking to posts or pages 227 | // can add as many post types to the array 228 | if ( in_array($menu_object->object, array('page')) ) { 229 | 230 | // set the title to the post_thumbnail if available 231 | // thumbnail size is the second parameter of get_the_post_thumbnail() 232 | 233 | if($menu_object->menu_item_parent != 0 ){ // if the item have a menu parent 234 | // $menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'thumbnail') . $menu_object->title : $menu_object->title; 235 | 236 | } 237 | 238 | } 239 | } 240 | 241 | return $sorted_menu_objects; 242 | } 243 | -------------------------------------------------------------------------------- /custom-post-type.php: -------------------------------------------------------------------------------- 1 | __('Services' , 'language-slug'), 15 | 'singular_name' => __('Service', 'language-slug'), 16 | 'add_new' => __('Add Service', 'language-slug'), 17 | 'add_new_item' => __('Add New Service', 'language-slug'), 18 | 'edit_item' => __('Edit Service', 'language-slug'), 19 | 'new_item' => __('New Service', 'language-slug'), 20 | 'all_items' => __('All Services', 'language-slug'), 21 | 'view_item' => __('View Service', 'language-slug'), 22 | 'search_items' => __('Search Service' , 'language-slug'), 23 | 'not_found' => __('Service Not found', 'language-slug'), 24 | 'not_found_in_trash' => __('Service Not found in the trash', 'language-slug'), 25 | ); 26 | 27 | $args = array( 28 | 'labels' => $labels, 29 | 'public' => true, 30 | 'rewrite' => array('slug' => 'services'), 31 | 'has_archive' => true, 32 | 'hierarchical' => true, 33 | 'menu_position' => 22, 34 | 'menu_icon' => 'dashicons-welcome-write-blog', 35 | 'supports' => array( 36 | 'title', 37 | 'editor', 38 | 'thumbnail', 39 | 'excerpt', 40 | 'page-attributes' 41 | ), 42 | 43 | ); 44 | 45 | register_post_type('my_service_cpt', $args); 46 | } 47 | 48 | ?> 49 | -------------------------------------------------------------------------------- /functions.php: -------------------------------------------------------------------------------- 1 | esc_html__( 'Header', 'slug_theme' ) 37 | ) ); 38 | 39 | // Load theme languages 40 | load_theme_textdomain( 'slug_theme', get_template_directory().'/languages' ); 41 | 42 | } 43 | 44 | } 45 | add_action( 'after_setup_theme', 'slug_theme_setup' ); 46 | 47 | 48 | /* Register sidebars 49 | /* ------------------------------------ */ 50 | if ( ! function_exists( 'slug_theme_sidebars' ) ) { 51 | 52 | 53 | function slug_theme_sidebars() { 54 | register_sidebar(array( 'name' => esc_html__( 'Primary', 'slug_theme' ),'id' => 'primary','description' => esc_html__( 'Normal full width sidebar.', 'slug_theme' ), 'before_widget' => '
','after_widget' => '
','before_title' => '

','after_title' => '

')); 55 | } 56 | 57 | } 58 | add_action( 'widgets_init', 'slug_theme_sidebars' ); 59 | 60 | 61 | /* Enqueue javascript 62 | /* ------------------------------------ */ 63 | if ( ! function_exists( 'slug_theme_scripts' ) ) { 64 | 65 | function slug_theme_scripts() { 66 | 67 | // all script 68 | wp_enqueue_script( 'slug_theme-script', get_template_directory_uri() . '/js/script.js', array( 'jquery' ),'', true ); 69 | if ( is_singular() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); } 70 | 71 | } 72 | 73 | } 74 | add_action( 'wp_enqueue_scripts', 'slug_theme_scripts' ); 75 | 76 | 77 | /* Enqueue css 78 | /* ------------------------------------ */ 79 | if ( ! function_exists( 'slug_theme_styles' ) ) { 80 | 81 | function slug_theme_styles() { 82 | wp_enqueue_style( 'slug_theme-style', get_template_directory_uri().'/style.css'); 83 | 84 | } 85 | 86 | } 87 | add_action( 'wp_enqueue_scripts', 'slug_theme_styles' ); 88 | 89 | ?> 90 | -------------------------------------------------------------------------------- /gallery-html-custom.php: -------------------------------------------------------------------------------- 1 | ID; 14 | $gallery = ""; 15 | 16 | if (empty($pid)) {$pid = $post['ID'];} 17 | 18 | if (!empty( $atts['ids'] ) ) { 19 | $atts['orderby'] = 'post__in'; 20 | $atts['include'] = $atts['ids']; 21 | } 22 | 23 | extract(shortcode_atts(array('orderby' => 'menu_order ASC, ID ASC', 'include' => '', 'id' => $pid, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'large', 'link' => 'file'), $atts)); 24 | 25 | $args = array('post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'orderby' => $orderby); 26 | 27 | if (!empty($include)) {$args['include'] = $include;} 28 | else { 29 | $args['post_parent'] = $id; 30 | $args['numberposts'] = -1; 31 | } 32 | 33 | if ($args['include'] == "") { $args['orderby'] = 'date'; $args['order'] = 'asc';} 34 | 35 | $images = get_posts($args); 36 | 37 | $gallery .= ''; 51 | 52 | return $gallery; 53 | } ?> 54 | -------------------------------------------------------------------------------- /gallery-loop.php: -------------------------------------------------------------------------------- 1 | ID, false ); 42 | 43 | if($gallery != ''){ 44 | 45 | $ids = explode( ",", $gallery['ids'] ); 46 | 47 | /* loop image gallery */ 48 | 49 | foreach( $ids as $id ) { 50 | 51 | $image_url = wp_get_attachment_image_src( $id , 'medium' ); 52 | $thumbnail_image = get_posts(array('p' => $id, 'post_type' => 'attachment')); 53 | 54 | ?> 55 | 56 | 63 | 64 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /image-background.php: -------------------------------------------------------------------------------- 1 | ID ), 'medium' ); 8 | 9 | ?> 10 | 11 |
12 | -------------------------------------------------------------------------------- /loop-custom.php: -------------------------------------------------------------------------------- 1 | 9 | 'page', 15 | 'posts_per_page'=> 3, 16 | 'orderby' => 'menu_order', 17 | 'order' => 'ASC', 18 | )); 19 | 20 | /* Wp Query - page + taxonomy + menu order */ 21 | 22 | // $custom_post = new WP_Query( array( 23 | // 'post_type' => 'page', 24 | // 'posts_per_page'=> 3, 25 | // 'orderby' => 'menu_order', 26 | // 'order' => 'ASC', 27 | // 'tax_query' => array( 28 | // array( 29 | // 'taxonomy' => 'home_positions', 30 | // 'field' => 'slug', 31 | // 'terms' => 'top' 32 | // ) 33 | // ) 34 | // )); 35 | 36 | /* Wp Query - post + taxonomy */ 37 | 38 | // $custom_post = new WP_Query( array( 39 | // 'post_type' => 'post', 40 | // 'tax_query' => array( 41 | // array( 42 | // 'taxonomy' => 'people', 43 | // 'field' => 'slug', 44 | // 'terms' => 'bob', 45 | // ) 46 | // ) 47 | // )); 48 | 49 | /* Wp Query - post + category */ 50 | 51 | // $custom_post = new WP_Query( array( 52 | // 'post_type' => 'post', 53 | // 'category_name' => 'news', 54 | // 'posts_per_page'=> 3, 55 | // )); 56 | 57 | 58 | /* Wp Query - sub pages + menu order */ 59 | 60 | // $this_page_id = $wp_query->post->ID; 61 | // $custom_post = new WP_Query( array( 62 | // 'post_type' => 'page', 63 | // 'post_parent' => $this_page_id, 64 | // 'posts_per_page' => 100, 65 | // 'orderby' => 'menu_order', 66 | // 'order' => 'ASC', 67 | // )); 68 | 69 | 70 | 71 | ?> 72 | 73 | have_posts()) : while($custom_post->have_posts()) : $custom_post->the_post(); ?> 74 | 75 | 76 | '','alt' => get_the_title())); ?> 77 | 78 |

79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /loop-pagination.php: -------------------------------------------------------------------------------- 1 | 'page', 13 | 'posts_per_page' => 10, 14 | ); 15 | 16 | // Get current page and append to custom query parameters array 17 | $custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; 18 | 19 | // Instantiate custom query 20 | $custom_query = new WP_Query( $custom_query_args ); 21 | 22 | // Pagination fix 23 | $temp_query = $wp_query; 24 | $wp_query = NULL; 25 | $wp_query = $custom_query; 26 | 27 | // Output custom query loop 28 | if ( $custom_query->have_posts() ) : 29 | while ( $custom_query->have_posts() ) : 30 | $custom_query->the_post(); 31 | // Loop 32 | ?> 33 | 34 | 35 | '','alt' => get_the_title())); ?> 36 | 37 |

38 | 39 | 40 | 41 | 42 | '; 51 | 52 | global $wp_query; 53 | 54 | $big = 999999999; // Need an unlikely integer 55 | 56 | echo paginate_links( array( 57 | 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 58 | 'format' => '?paged=%#%', 59 | 'current' => max( 1, get_query_var('paged') ), 60 | 'total' => $wp_query->max_num_pages 61 | ) ); 62 | 63 | echo '
'; 64 | 65 | // Reset main query object 66 | $wp_query = NULL; 67 | $wp_query = $temp_query; 68 | 69 | ?> 70 | -------------------------------------------------------------------------------- /loop-template-filter.php: -------------------------------------------------------------------------------- 1 | 10 | 'page', 14 | 'no_found_rows' => true, 15 | 'post_status' => 'publish', 16 | 'orderby' => 'menu_order', 17 | 'order' => 'ASC', 18 | 'posts_per_page' => 3, 19 | 'meta_query' => array( 20 | array( 21 | 'key' => '_wp_page_template', 22 | 'value' => 'my-custom-template.php', 23 | ) 24 | ) 25 | ) ); ?> 26 | 27 | have_posts()) : while($template_pages->have_posts()) : $template_pages->the_post(); ?> 28 | 29 | 30 | '','alt' => get_the_title())); ?> 31 | 32 |

33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /loop.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | '','alt' => get_the_title())); ?> 13 | 14 |

15 | 16 | 17 | 18 | 19 | 20 | 34 | 35 | 36 | 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /shortcode.php: -------------------------------------------------------------------------------- 1 | 'post', 25 | 'posts_per_page'=> 6, 26 | )); 27 | 28 | 29 | $buffer = '
'; 30 | $buffer .= '
'; 31 | $buffer .= ''; 48 | $buffer .= '
'; 49 | $buffer .= '
'; 50 | 51 | return $buffer; 52 | 53 | 54 | } 55 | // register shortcode 56 | add_shortcode('slider', 'custom_slider_shortcode'); 57 | 58 | /* Shortcode Custom Loop 59 | ------------------------------------------------------------------------- */ 60 | 61 | function custom_shortcode_inevidenza() { 62 | 63 | $buffer = '
'; 64 | 65 | $custom_loop = new WP_Query( array( 66 | 'post_type' => 'immobile', 67 | 'tax_query' => array( 68 | array( 69 | 'taxonomy' => 'tag-immobile', 70 | 'field' => 'slug', 71 | 'terms' => array( 'in-evidenza'), 72 | )), 73 | 'posts_per_page' => 3, 74 | 'orderby' => 'menu_order', 75 | 'order' => 'ASC', 76 | )); 77 | 78 | if ($custom_loop->have_posts()) : while($custom_loop->have_posts()) : $custom_loop->the_post(); 79 | 80 | $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); 81 | 82 | $buffer .= '
'; 83 | $buffer .= ''; 84 | $buffer .= ''; 85 | $buffer .= ''; 86 | $buffer .= '

'. get_the_title() .'

'; 87 | 88 | 89 | $buffer .= '

'; 90 | //custom terms taxonomy 91 | $terms = get_the_terms( $post->ID ,'numero-bagni'); 92 | if($terms){ 93 | $buffer .= ''; 94 | foreach ( $terms as $term ) { 95 | $buffer .= $term->name; 96 | } 97 | $buffer .= ' bagni'; 98 | } 99 | 100 | // custom fields 101 | if( get_field('metri_quadri') ){ 102 | $buffer .= '   '. get_field('metri_quadri').' m2'; 103 | } 104 | $buffer .= '

'; 105 | $buffer .= '
'; 106 | 107 | wp_reset_postdata(); 108 | endwhile; endif; 109 | $buffer .= '
'; 110 | 111 | return $buffer; 112 | } 113 | add_shortcode('immobili-inevidenza', 'custom_shortcode_inevidenza'); 114 | -------------------------------------------------------------------------------- /taxonomy-menu.php: -------------------------------------------------------------------------------- 1 | 10 | 11 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /taxonomy.php: -------------------------------------------------------------------------------- 1 | __('Categories', 'language-slug'), 20 | 'singular_name' => __('Category', 'language-slug'), 21 | 'add_new_item' => __('Add Category', 'language-slug'), 22 | 'edit_item' => __('Edit Category', 'language-slug'), 23 | 'new_item_name' => __('New Category', 'language-slug'), 24 | 'all_items' => __('All Categories', 'language-slug'), 25 | 'search_items' => __('Search Category', 'language-slug'), 26 | 'update_item' => __('Update Category', 'language-slug'), 27 | ); 28 | 29 | $args = array( 30 | 'labels' => $labels, 31 | 'hierarchical' => true, 32 | 'query_var' => true, 33 | 'rewrite' => array('slug' => 'service-category' , 'hierarchical' => true) //rewrite the tax permalink structure 34 | ); 35 | 36 | register_taxonomy('service_category_tax','my_service_cpt', $args); 37 | } 38 | 39 | 40 | /** 41 | * Show taxonomy in backend list post type 42 | */ 43 | 44 | add_filter( 'manage_taxonomies_for_my_service_cpt_columns', 'my_service_cpt_type_columns' ); 45 | function my_service_cpt_type_columns( $taxonomies ) { 46 | $taxonomies[] = 'service_category_tax'; 47 | return $taxonomies; 48 | } 49 | 50 | ?> 51 | -------------------------------------------------------------------------------- /woocommerce-optimize-enque-scripts-styles.php: -------------------------------------------------------------------------------- 1 | 35 | --------------------------------------------------------------------------------