function my_render_block_core_latest_posts( $attributes ) {
$args = array(
'posts_per_page' => $attributes['postsToShow'],
'post_status' => 'publish',
'order' => $attributes['order'],
'orderby' => $attributes['orderBy'],
'suppress_filters' => false,
);
if ( isset( $attributes['categories'] ) ) {
$args['category'] = $attributes['categories'];
}
$recent_posts = get_posts( $args );
$list_items_markup = '';
$excerpt_length = $attributes['excerptLength'];
foreach ( $recent_posts as $post ) {
$title = get_the_title( $post );
if ( ! $title ) {
$title = __( '(no title)' );
}
$list_items_markup .= sprintf(
'<li><a href="%1$s">%2$s</a>',
esc_url( get_permalink( $post ) ),
$title
);
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$list_items_markup .= sprintf(
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
esc_attr( get_the_date( 'c', $post ) ),
esc_html( get_the_date( '', $post ) )
);
}
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
$post_excerpt = $post->post_excerpt;
if ( ! ( $post_excerpt ) ) {
$post_excerpt = $post->post_content;
}
$trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $excerpt_length, ' … ' ) );
$list_items_markup .= sprintf(
'<div class="wp-block-latest-posts__post-excerpt">%1$s',
$trimmed_excerpt
);
if ( strpos( $trimmed_excerpt, ' … ' ) !== false ) {
$list_items_markup .= sprintf(
'<a href="%1$s">%2$s</a></div>',
esc_url( get_permalink( $post ) ),
__( 'Read more' )
);
} else {
$list_items_markup .= sprintf(
'</div>'
);
}
}
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
$list_items_markup .= sprintf(
'<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) )
);
}
$list_items_markup .= sprintf(
'<span class="vendor">%1$s</span>',
get_post_meta( $post->ID, 'vendor', true )
);
$list_items_markup .= "</li>\n";
}
$class = 'wp-block-latest-posts wp-block-latest-posts__list';
if ( isset( $attributes['align'] ) ) {
$class .= ' align' . $attributes['align'];
}
if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
$class .= ' is-grid';
}
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
$class .= ' columns-' . $attributes['columns'];
}
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$class .= ' has-dates';
}
if ( isset( $attributes['className'] ) ) {
$class .= ' ' . $attributes['className'];
}
return sprintf(
'<ul class="%1$s">%2$s</ul>',
esc_attr( $class ),
$list_items_markup
);
}
function my_register_block_core_latest_posts() {
register_block_type(
'core/latest-posts',
array(
'attributes' => array(
'align' => array(
'type' => 'string',
'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
),
'className' => array(
'type' => 'string',
),
'categories' => array(
'type' => 'string',
),
'postsToShow' => array(
'type' => 'number',
'default' => 5,
),
'displayPostContent' => array(
'type' => 'boolean',
'default' => false,
),
'displayPostContentRadio' => array(
'type' => 'string',
'default' => 'excerpt',
),
'excerptLength' => array(
'type' => 'number',
'default' => 55,
),
'displayPostDate' => array(
'type' => 'boolean',
'default' => false,
),
'postLayout' => array(
'type' => 'string',
'default' => 'list',
),
'columns' => array(
'type' => 'number',
'default' => 3,
),
'order' => array(
'type' => 'string',
'default' => 'desc',
),
'orderBy' => array(
'type' => 'string',
'default' => 'date',
),
),
'render_callback' => 'my_render_block_core_latest_posts',
)
);
}
remove_action( 'init', 'register_block_core_latest_posts',10 );
add_action( 'init', 'my_register_block_core_latest_posts',10 );
add_filter( 'wp_handle_upload_prefilter', 'wpp_limit_uploads_for_postr' );
function wpp_limit_uploads_for_post( $file ) {
global $wpdb,$post;
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = " . $post->ID );
if ( 10 < $count ) { //количество файлов в пост
$file['error'] = 'Вы исчерпали лимит файлов для загрузки';
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'wpp_limit_uploads_for_user' );
function wpp_limit_uploads_for_user( $file ) {
global $wpdb;
$user = wp_get_current_user();
if ( array_intersect( ['author'], $user->roles ) ) { // Для пользователей с ролью автор
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = " . $user->ID );
if ( 10 < $count ) { //количество файлов пользователю выставлено 10
$file['error'] = 'Вы исчерпали лимит файлов для загрузки';
}
}
return $file;
}
add_filter('gettext', 'wpp_change_product_label_translate');
add_filter('ngettext', 'wpp_change_product_label_translate');
function wpp_change_product_label_translate($translated) {
$translated = str_ireplace('Товары', 'Продукты', $translated);
$translated = str_ireplace('Товар', 'Продукт', $translated);
return $translated;
}
function bla_bla_bla_post_class($classes, $product)
{
$classes[] = 'col-12';
$classes[] = 'col-md-3';
$classes[] = 'col-sm-6';
return $classes;
}
add_filter('woocommerce_post_class', 'bla_bla_bla_post_class', 10, 2);
add_action( 'pre_get_posts', function( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$query->set( 'post__not_in', [1.2.3.4.5.6] ); // id записей которые надо скрыть
$tax_query = [
[
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => [20,30,40,50], // id категорий которые надо скрыть
'operator' => 'NOT IN',
]
];
$query->set( 'tax_query', $tax_query );
}
});
add_filter( 'pre_option_comment_moderation', 'wpp_auto_comment_moderation' );
add_filter( 'pre_option_comment_whitelist', 'wpp_auto_comment_moderation' );
function wpp_auto_comment_moderation( $option ) {
global $post;
$need_id = 55; #ID категории в ноторой надо разрешить автопубликацию комментариев
$ids = wp_get_post_categories( $post->ID );
if ( in_array( $need_id, $ids ) ) {
return 0;
} else {
return $option;
}
}
// добавить в functions.php
/**
* Получение термина верхнего уровня
*
* @param $term_id int ID термина
* @param $taxonomy string таксономия
*
* @return array|int|null|WP_Error|WP_Term
*/
function wpp_get_term_top_level_id( $term_id, $taxonomy ) {
$term_parent = 0;
while ( $term_id ) {
$term = get_term( $term_id, $taxonomy );
$term_id = $term->parent;
$term_parent = $term;
}
return $term_parent;
}
// Start the loop.
while ( have_posts() ) : the_post();
$terms = get_the_terms( get_the_ID(), 'БББ' );
if(!empty($terms)) {
$need_term = wpp_get_term_top_level_id( $terms[0]->term_id, 'БББ' );
get_template_part( 'taxonomy/content-taxonomy/content-taxonomy', $need_term->slug);
} else {
echo 'не установлены термины таксономии БББ';
}