Wordpress
Замена URL в базе данных сайта на Wordpress
Через SQL-запрос к базе данных:
Внутренний счетчик цикла в ACF Repeater и Flexible Content
<?php $_i = acf_get_loop( 'active', 'i' ); ?>
Шаблон архива с AJAX-подгрузкой постов в Wordrpess
<?php if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' && get_query_var( 'paged' ) ) : ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php if ( $next = get_next_posts_link( '' ) ) : ?>
<a class="load-more" href="<?php echo next_posts(); ?>"><?php _e( 'Load more', 'base' ); ?></a>
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
<?php get_header(); ?>
<div id="content">
<?php if ( have_posts() ) : ?>
<div class="load-more-holder">
<div class="title">
<?php the_archive_title( '<h1>', '</h1>' ); ?>
</div>
<div class="new-content-target">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
</div>
<?php if ( $next = get_next_posts_link( '' ) ) : ?>
<a class="load-more" href="<?php echo next_posts(); ?>"><?php _e( 'Load more', 'base' ); ?></a>
<?php endif; ?>
</div>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php if ( $next = get_next_posts_link( '' ) ) : ?>
<a class="load-more" href="<?php echo next_posts(); ?>"><?php _e( 'Load more', 'base' ); ?></a>
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
<?php get_header(); ?>
<div id="content">
<?php if ( have_posts() ) : ?>
<div class="load-more-holder">
<div class="title">
<?php the_archive_title( '<h1>', '</h1>' ); ?>
</div>
<div class="new-content-target">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
</div>
<?php if ( $next = get_next_posts_link( '' ) ) : ?>
<a class="load-more" href="<?php echo next_posts(); ?>"><?php _e( 'Load more', 'base' ); ?></a>
<?php endif; ?>
</div>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
<?php endif; ?>
Проект на GitHub - https://github.com/sergeybelyaev/wp_ajax_load_more_archive.
Редирект анонимных посетителей на главную страницу в Wordpress
add_action( 'get_header', 'redirect_anonymous_to_front_page', 100 );
function redirect_anonymous_to_front_page() {
if ( ! is_user_logged_in() && ! is_front_page() ) {
wp_redirect( home_url() );
exit;
}
}
Кастомный переключатель языков WPML
<?php if ( function_exists( 'icl_get_languages' ) ) : ?>
<?php $languages = icl_get_languages(); ?>
<ul>
<?php foreach ( $languages as $lang ) : ?>
<li<?php if ( $lang['active'] ) : ?> class="active"<?php endif; ?>>
<a href="<?php echo $lang['url']; ?>"><?php echo $lang['native_name']; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php $languages = icl_get_languages(); ?>
<ul>
<?php foreach ( $languages as $lang ) : ?>
<li<?php if ( $lang['active'] ) : ?> class="active"<?php endif; ?>>
<a href="<?php echo $lang['url']; ?>"><?php echo $lang['native_name']; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Сниппеты для работы с ACF Pro
<?php // ACF Repeater ?>
Получение всех значений кастомфилда из определенного типа постов в Вордпресс
function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) {
global $wpdb;
if ( empty( $key ) ) return;
$r = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $r;
}
global $wpdb;
if ( empty( $key ) ) return;
$r = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $r;
}