Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active December 3, 2022 06:52
Show Gist options
  • Save eto4detak/8d9d8c9381f5a0625203d057f0f5a9a1 to your computer and use it in GitHub Desktop.
Save eto4detak/8d9d8c9381f5a0625203d057f0f5a9a1 to your computer and use it in GitHub Desktop.
wp php tax
<?php
$cur_queried_object = get_queried_object();
/*========================================================
* получить родительскую по названию и родительской term_id
========================================================*/
$terms = get_terms( array(
'taxonomy' => array( 'category', ), // название таксономии с WP 4.5
'child_of' => $department_term->term_id,
'name__like' => $cat_lvl2->subdivision,
) );
/*========================================================
* поиск родительской id (без родителей)
========================================================*/
$term_list = wp_get_post_terms( $post->ID, 'category', array('fields' => 'all') );
$cat_id = 0;
foreach ($term_list as $key => $term) {
if($term->parent == 0){
$cat_id = $term->term_id;
break;
}
}
/*========================================================
* терм
========================================================*/
$term = get_term( 11, 'product_cat' );
/*========================================================
* вывести термины поста
========================================================*/
$term_list = wp_get_post_terms( $post->ID, 'property_category', array('fields' => 'names') );
$property_categ = '';
foreach ($term_list as $key => $value) {
$property_categ .= '<span>'.$value.'</span>';
}
/*========================================================
* поиск родительской категории
========================================================*/
$term_children = wp_get_post_terms( $product->get_id(), 'product_cat', array( "fields" => "ids" ) );
$term_id = art_find_product_cat_parent($term_children[0]);
$term = get_term( $term_id, 'product_cat' );
$term_name = $term->name ? $term->name : '';
$term_url = get_term_link( $term_id, 'product_cat' ) ? get_term_link( $term_id, 'product_cat' ) : '';
function art_find_product_cat_parent($term_children='')
{
if(!empty($term_children)){
$termchildren = wp_get_term_taxonomy_parent_id( $term_children, 'product_cat' );
if(!empty($termchildren)){
$term_children = art_find_product_cat_parent($termchildren);
}
}
return $term_children;
}
/*========================================================
* изменить такономию, добавить поле в категорию
========================================================*/
$taxname = 'product_cat';
// Поля при добавлении элемента таксономии
add_action("{$taxname}_add_form_fields", 'art_add_new_custom_fields');
// Поля при редактировании элемента таксономии
add_action("{$taxname}_edit_form_fields", 'art_edit_new_custom_fields');
// Сохранение при добавлении элемента таксономии
add_action("create_{$taxname}", 'art_save_custom_taxonomy_meta');
// Сохранение при редактировании элемента таксономии
add_action("edited_{$taxname}", 'art_save_custom_taxonomy_meta');
function art_edit_new_custom_fields( $term ) {
?>
<tr class="form-field">
<th scope="row" valign="top"><label>Стикеры</label></th>
<td>
<input type="text" name="art_theme[stickers]" value="<?php echo esc_attr( get_term_meta( $term->term_id, 'stickers', 1 ) ) ?>"><br />
<span class="description">Стикеры для страницы категории</span>
</td>
</tr>
<?php
}
function art_add_new_custom_fields( $taxonomy_slug ){
?>
<div class="form-field">
<label for="tag-stickers">Заголовок</label>
<input name="art_theme[stickers]" id="tag-stickers" type="text" value="" />
<p>Стикеры для страницы категории</p>
</div>
<?php
}
function art_save_custom_taxonomy_meta( $term_id ) {
if ( ! isset($_POST['art_theme']) ) return;
if ( ! current_user_can('edit_term', $term_id) ) return;
if (
! wp_verify_nonce( $_POST['_wpnonce'], "update-tag_$term_id" ) && // wp_nonce_field( 'update-tag_' . $tag_ID );
! wp_verify_nonce( $_POST['_wpnonce_add-tag'], "add-tag" ) // wp_nonce_field('add-tag', '_wpnonce_add-tag');
) return;
// Все ОК! Теперь, нужно сохранить/удалить данные
$extra = wp_unslash($_POST['art_theme']);
foreach( $extra as $key => $val ){
// проверка ключа
$_key = sanitize_key( $key );
if( $_key !== $key ) wp_die( 'bad key'. esc_html($key) );
// очистка
if( $_key === 'tag_posts_shortcode_links' )
$val = sanitize_textarea_field( strip_tags($val) );
else
$val = sanitize_text_field( $val );
// сохранение
if( ! $val )
delete_term_meta( $term_id, $_key );
else
update_term_meta( $term_id, $_key, $val );
}
return $term_id;
}
/*========================================================
* сортировка в виджете категории woocommerce, убрать дочерние элементы не активные
========================================================*/
add_filter( 'woocommerce_product_categories_widget_args', 'divie_woo_cat_widget_args' );
function divie_woo_cat_widget_args($list_args=''){
$cats = get_categories(array('taxonomy' => 'product_cat', 'parent' => 0));
$mass_parent= [];
$mass_parent_cat = [];
foreach ($cats as $key => $value) {
$mass_parent[] = $value->term_id;
}
$arr_parent_car = [];
$arr_parent_car = get_ancestors( $list_args['current_category'], 'product_cat' );
if(empty($arr_parent_car)){
$arr_parent_car[] = $list_args['current_category'];
}
$exclude_cat = array_diff($mass_parent, $arr_parent_car);
foreach ($exclude_cat as $key => $cat_id) {
$child_cats = get_term_children( $cat_id, 'product_cat' );
foreach ($child_cats as $key => $id) {
$mass_parent_cat[] = $id;
}
}
$str_exclude_cat = implode(",", $mass_parent_cat);
$list_args['exclude'] = $str_exclude_cat;
return $list_args;
}
/*========================================================
*
========================================================*/
global $post;
$the_id= $post->ID;
$terms = get_terms( 'my_taxonomy' );
$booking_status = get_the_term_list( $the_id, 'status_booking', '', ', ', '');
//page tax
$cur_queried_object = get_queried_object();
$cur_term_id = $cur_queried_object->term_id;
$tax_nam = 'vopros_otvet';
$fiz_ID = get_term_by('slug', 'fizicheskie_lica', $tax_nam)->term_id;
$fiz_ch_terms = get_term_children( $fiz_ID, $tax_nam );
//с проверкой
$cur_term_id = '';
if($cur_queried_object = get_queried_object()){
$cur_term_id = $cur_queried_object->term_id;
}
$taxon = 'category';
$term_id = '';
if($term = get_term_by('slug', 'blog', $taxon)){
$term_id = $term->term_id;
}
if(!empty($term_id) && !empty($cur_term_id) && $cur_term_id === $term_id){
}
//наследники
foreach ($fiz_ch_terms as $child) {
$term = get_term_by( 'id', $child, $tax_nam );
if($fiz_ID !== $term->parent) continue;
?>
<div class="item">
<div class="h2"><div class="img"><img src="/wp-content/themes/ask-me/assets/img/pravo/ctg-list-ico1.png" alt=""></div><?php echo $term->name ?></div>
<?php
foreach ($fiz_ch_terms as $ch3) {
$term3 = get_term_by( 'id', $ch3, $tax_nam );
if($term->term_id !== $term3->parent) continue;
?>
<div class="h3"><?php echo $term3->name; ?></div>
<ul>
<?php
foreach ($fiz_ch_terms as $ch4) {
$term4 = get_term_by( 'id', $ch4, $tax_nam );
if($term3->term_id !== $term4->parent) continue;
?>
<li><a href="<?php echo get_term_link( $term4->term_id, $term4->taxonomy ) ?>"><?php echo $term4->name ?></a></li>
<?php
}
?>
</ul>
<?php
}
?>
</div>
<?php
}
get_the_terms( $post->ID, 'my_tax' ); // термин поста
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment