У меня есть шаблон с дочерней темой, и я редактирую дочерний шаблон function.php
Цель - создать сокращенный код, который будет иметь параметры и часть файла шаблона.
моя функция выглядит так:
function my_shortcode($atts = array() ) {
extract(shortcode_atts(array(
'catid' => '5'
), $atts));
ob_start();
include(get_template_part('mynews-temp'));
return ob_get_clean();
}
add_shortcode('mynews', 'my_shortcode');
Вывод короткого кода [mynews catid = "5"] должен быть новостным запросом с идентификатором кошки 5.
это часть шаблона с запросом
<?phpif ( ! defined( 'ABSPATH' ) ) {exit( 'Direct script access denied.' );}
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'mynews',
'tax_query' => array(
array(
'taxonomy' => 'newstypes',
'field' => 'tag_ID',
'terms' => array('$catid')
),),));
if ( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );?>
<a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a><
<?php endforeach; ?>
<?php endif; wp_reset_postdata(); ?>
Не работает, не могу найти ошибку
P.S. ведьма верна, когда я использую дочерний шаблон:
include(get_template_part('mynews-temp'));
or include(locate_template('mynews-temp'));






Вы можете включить такие части шаблона: get_template_part('mynews-temp'). Не нужен include, он ничего не делает.
Остальная часть вашего кода работает отлично. Единственное, что я мог бы порекомендовать, - это упростить ваш код и просто получить все обычные post, чтобы увидеть, не в tax_query ли проблема:
<?php
if ( ! defined( 'ABSPATH' ) ) {exit( 'Direct script access denied.' );}
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
));
// 'tax_query' => array(
// array(
// 'taxonomy' => 'newstypes',
// 'field' => 'tag_ID',
// 'terms' => array('$catid')
// ),),));
if ( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );?>
<a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a><
<?php endforeach; ?>
<?php endif; wp_reset_postdata(); ?>