Простой пример создания шорткода
function price_block_shortcode() {
ob_start(); ?>
<?php $tables = get_field('tables', 248); ?>
<?php if( !empty($tables) ): ?>
<div class="section-price__tables">
<?php echo $tables; ?>
</div>
<?php endif ?>
<?php
$myvariable = ob_get_clean();
return $myvariable;
}
add_shortcode( 'price', 'price_block_shortcode' );
Код добавляется в файл functions.php
Шорткод с параметрами
Параметров будет два: field и id— поле custom-fields и id странцы(записи) соответственно. Как будет выглядеть сам шорткод: [price field ="tables" id="248"]
. Если параметр не будет указан в самом шорткоде, то будет использоваться его значение по умолчанию, заданное в функции.
function price_block_shortcode($atts) {
$params = shortcode_atts( [ // в массиве указываются значения параметров по умолчанию
'field' => 'tables',
'id' => '248'
], $atts );
ob_start(); ?>
<?php $tables = get_field($params['field'], $params['id']); ?>
<?php if( !empty($tables) ): ?>
<div class="section-price__tables">
<?php echo $tables; ?>
</div>
<?php endif ?>
<?php
$myvariable = ob_get_clean();
return $myvariable;
}
add_shortcode( 'price', 'price_block_shortcode' );