Памятка по произвольным полям и ACF

1) Использовать только get_post_meta() (https://wp-kama.ru/function/get_post_meta)

$meta_values = get_post_meta( $post_id, $key, $single );

$post_id(число) (обязательный)
ID поста, произвольные поля которого нужно получить.
По умолчанию: нет
$key(строка) 
Название произвольного поля, значение которого нужно получить. Если оставить поле пустым, будут получены все произвольные поля поста.
По умолчанию: ''
$single(логический)
true - вернет значение метаполя (если полей несколько то вернет значение первого метаполя).
false - вернет массив всех значений мета полей с указанным ключом.

Если в значении произвольного поля находится сериализованный массив, то значение true вернет, нормальный массив, а если указать false, то вернется массив в элементе "[0]" которого будет все тот же сериализованный массив.

По умолчанию: false

 

2) Отключать ACF на фронтенде

<?php
/**
 * Plugin Name: Disable ACF on Frontend
 * Description: Provides a performance boost if ACF frontend functions aren't being used
 * Version:     1.0
 * Author:      Bill Erickson
 * Author URI:  http://www.billerickson.net https://www.billerickson.net/code/disable-acf-frontend/
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */
 
/**
 * Disable ACF on Frontend
 *
 */
function ea_disable_acf_on_frontend( $plugins ) {

    if( is_admin() )
        return $plugins;

    foreach( $plugins as $i => $plugin )
        if( 'advanced-custom-fields-pro/acf.php' == $plugin )
            unset( $plugins[$i] );
    return $plugins;
}
add_filter( 'option_active_plugins', 'ea_disable_acf_on_frontend' );

3) Use Local JSON (or Export to PHP). Local JSON is a new feature added in version 5 which saves field group and field settings as .json files within your theme. The idea is similar to caching, and both dramatically speeds up ACF and allows for version control over your field settings!

To start using the local JSON feature, simply create a new folder in your theme and name it acf-json. This folder must have permissions for the server to read and write (in most cases 755 will work well).

Once this folder exists, each time you save a field group a JSON file will be created (or updated) with the field group and field settings. The JSON file will be named using the field group’s unique key.

Now that the JSON file exists, ACF will load the relevant field group and field settings from this file which reduces the number of database calls during your page load!

If you wish to hide your field group or field settings from the public, simply add an empty index.php file to your acf-json folder.

INDEX.PHP
<?php
// Silence is golden.
?>

 

Проверить есть ли значения в поле, если есть — вывести

	 if ( $daate = get_post_meta( get_the_ID(), 'date_start', true ) ) : 			
	 		 echo '<div class="date-event">  <img class="ev-cal" src="/wp-content/uploads/calendar1.png">' .date('d-m-Y', strtotime(  $daate )). '</div>'; 
			 endif;

 

Разные приемы

$current_user = wp_get_current_user();

if( $current_user->ID ){
    // Авторизован.  
    
    if( ($current_user->user_email=='@yandex.ua') ||  ($current_user->user_email=='@rambler.ru')     )
    {
                wp_redirect( '/limit/', 303 ); exit;  
        //echo $current_user->user_email;
    }
     
}
else {
    // Не авторизован.
    echo ' ';
}

 

 

 

Заказать сайт

Оставьте комментарий

Ваш адрес email не будет опубликован.

Прокрутить вверх