selftrips
@selftrips

Как код php превратить в шорткод wordpress?

Для этого нужно прописать такую функцию
function my_shortcode_function() {
     return ;
}

Как в эту функцию засунуть сделующий код
$parametri = array(
  'post_type' => 'post', 
  'post_status' => 'publish', 
  'posts_per_page' => -1, 
  'caller_get_posts' => 1
   );
$moi_zapros = null;
$moi_zapros = new WP_Query($parametri);
if ($moi_zapros->have_posts()):
  print '<ol>';
  while ($moi_zapros->have_posts()) : $moi_zapros->the_post(); 
$idt=get_the_ID();
$trip=get_post_meta($idt,"trip", true);?>
<li class="map"><a href="<?php the_permalink() ?>" title="Постоянный линк для:<?php the_title_attribute(); ?>" target="_blank"><?php the_title();   if ($trip) {echo " ", $trip, ". День ",get_post_meta($idt,"day", true), ". ";}?> (<?php print get_comments_number();?>)</a></li>
    <?php
  endwhile;
  print '</ol>';
endif;
wp_reset_query();
  • Вопрос задан
  • 742 просмотра
Решения вопроса 2
deniscopro
@deniscopro Куратор тега WordPress
WordPress-разработчик, denisco.pro
function my_shortcode_function() {
ob_start();

$parametri = array(
  'post_type' => 'post', 
  'post_status' => 'publish', 
  'posts_per_page' => -1, 
  'caller_get_posts' => 1
   );
$moi_zapros = null;
$moi_zapros = new WP_Query($parametri);
if ($moi_zapros->have_posts()):
  print '<ol>';
  while ($moi_zapros->have_posts()) : $moi_zapros->the_post(); 
$idt=get_the_ID();
$trip=get_post_meta($idt,"trip", true);?>
<li class="map"><a href="<?php the_permalink() ?>" title="Постоянный линк для:<?php the_title_attribute(); ?>" target="_blank"><?php the_title();   if ($trip) {echo " ", $trip, ". День ",get_post_meta($idt,"day", true), ". ";}?> (<?php print get_comments_number();?>)</a></li>
    <?php
  endwhile;
  print '</ol>';
endif;
wp_reset_query();

return ob_get_clean();
}
Ответ написан
Комментировать
Gori4ka
@Gori4ka
WordPress Developer
add_ahortcode('shortcode_name','my_shortcode_function');
function my_shortcode_function($attr) {
ob_start();
$parametri = array(
  'post_type' => 'post', 
  'post_status' => 'publish', 
  'posts_per_page' => -1, 
  'caller_get_posts' => 1
   );
$moi_zapros = null;
$moi_zapros = new WP_Query($parametri);
if ($moi_zapros->have_posts()):
  print '<ol>';
  while ($moi_zapros->have_posts()) : $moi_zapros->the_post(); 
$idt=get_the_ID();
$trip=get_post_meta($idt,"trip", true);?>
<li class="map"><a href="<?php the_permalink() ?>" title="Постоянный линк для:<?php the_title_attribute(); ?>" target="_blank"><?php the_title();   if ($trip) {echo " ", $trip, ". День ",get_post_meta($idt,"day", true), ". ";}?> (<?php print get_comments_number();?>)</a></li>
    <?php
  endwhile;
  print '</ol>';
endif;
wp_reset_query();
return ob_get_clean();
}

вот https://wp-kama.ru/function/add_shortcode
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы