Last active
February 7, 2018 02:22
-
-
Save sxidsvit/12346d20298992421df3a64e6e4d8bcd to your computer and use it in GitHub Desktop.
Фрагменты кода, которые можно использовать для редактирования PHP файлов темы Wordpress
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
| // Меню | |
| /* Вставка меню в код страницы | |
| <div class="wrap"> | |
| <?php $args = array( | |
| 'theme_location' => 'primary', | |
| 'container' => 'nav', | |
| 'container_class' => 'wrap-block', | |
| 'container_id' => '', | |
| 'menu_class' => 'header-ul', | |
| 'menu_id' => 'menu', | |
| ) ; | |
| wp_nav_menu( $args ); | |
| ?> | |
| </div> | |
| /* регистрация меню в functions.php | |
| add_theme_support('menu'); | |
| add_action( 'after_setup_theme', 'theme_register_nav_menu' ); | |
| function theme_register_nav_menu() {register_nav_menu( 'primary', 'Главное меню' );} | |
| // ------------------------------------------------------------------------------------------------------------------ | |
| // Карточка товара: запрос + извлечение и использование метаданных | |
| <?php | |
| $args = array( | |
| 'post_type' => 'product', | |
| 'numberposts' => 0, | |
| 'suppress_filters' => 'true', | |
| 'order' => 'ASC', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'product_cat', | |
| 'field' => 'slug', | |
| 'terms' => $term_slug | |
| ) | |
| ), | |
| ); | |
| $posts = get_posts( $args ); | |
| foreach($posts as $post ) { | |
| setup_postdata($post); | |
| $post_id = $post -> ID; // d($post_id); | |
| $weight = get_metadata('post', $post_id, '_weight', true); //d($weight); | |
| $price = get_metadata('post', $post_id, '_price', true); //d($price); | |
| $regular_price = get_metadata('post', $post_id, '_regular_price', true); //d($regular_price); | |
| ?> | |
| <div class="col-md-3 col-sm-6 col-xs-12"> | |
| <div class="shop-card"> | |
| <?php | |
| if ( has_post_thumbnail() ) { the_post_thumbnail(); } | |
| else { echo "<img src='" .THEMEPATH."/img/example/1.jpg' alt='Заглушка' >"; } | |
| ?> | |
| <h3><?php the_title(); ?> </h3> | |
| <p><?php echo 'Вес: '. $weight .'гр' ?></p> | |
| <span><?php echo $price ?></span> | |
| <b>4<?php echo $regular_price .'руб' ?> .</b> | |
| <button type="submit" class="popup-btn button-small">Оставить заявку</button> | |
| </div> | |
| </div> | |
| <?php } wp_reset_postdata(); ?> | |
| </div> | |
| <?php } ?> | |
| // ------------------------------------------------------------------------------------------------------------------ | |
| // Слайдер . Класс slider_ex используется js-плагином для создания слайдера | |
| <div class="slider_ex"> | |
| <?php | |
| $args = array( | |
| 'post_type' => 'post', | |
| 'numberposts' => 0, | |
| 'suppress_filters' => 'true', | |
| 'order' => 'ASC', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'category', | |
| 'field' => 'slug', | |
| 'terms' => 'reklamnye-bloki' | |
| ) | |
| ), | |
| ); | |
| $posts = get_posts( $args ); | |
| foreach($posts as $post ) { | |
| setup_postdata($post); | |
| if ( has_post_thumbnail() ) { the_post_thumbnail(); } | |
| else { echo "<img src='" .THEMEPATH."/img/example/1.jpg' alt='Заглушка' >"; } | |
| } | |
| wp_reset_postdata(); | |
| ?> | |
| </div> | |
| // ------------------------------------------------------------------------------------------------------------------ | |
| // Получаем свойства конкретного элемента (терма) из указанной таксономии | |
| <?php $ids = array(26, 27, 29, 30, 33); | |
| $i = 0; | |
| foreach($ids as $id ) { | |
| $term = get_term($id, 'product_cat'); | |
| $term_name = $term -> name; | |
| $i = $i + 1; | |
| ?> | |
| // ------------------------------------------------------------------------------------------------------------------ | |
| // Массивы аргументов для функциий wp_query(), get_posts и им подобных | |
| id = napitki | |
| post_type=>'product',tax_query=>array(relation=>'AND',array(taxonomy=>'product_cat',field=>'slug',terms=>'napitki'),array(taxonomy=>'product_tag',field=>'slug',terms=>'akciya1')),order=>'ASC',orderby=>'date',offset=>0,posts_per_page=>4 | |
| post_type=>'product',tax_query=>array(relation=>'AND',array(taxonomy=>'product_cat',field=>'slug',terms=>'napitki'),array(taxonomy=>'product_tag',field=>'slug',terms=>'akciya1')),order=>'ASC',orderby=>'date',offset=>4,posts_per_page=>999 | |
| // ------------------------------------------------------------------------------------------------------------------ | |
| <?php $product = get_product(get_the_ID()); $reg_price = $product->get_regular_price(); | |
| if(!empty( $reg_price )) { ?> <div id="red" class="price"> <?php echo $reg_price; ?> </div> | |
| <?php } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment