Доброго времени суток.
Есть пользовательские типы записей, решил сделать простой шорткод чтобы можно было выводить последние записи (ПТЗ) в различных местах с помощью шорткода (из админки, а не в файле шаблона).
Я не программист, вордпресс мое хобби, поэтому код собрал опираясь на кодекс вордпресс и плагины с похожим функционалом.
Код рабочий, ПТЗ выводится, все работает. Но поскольку я не профессионал, если найдется время
посмотрите на предмет ошибок, тех. стандартов и т.п.
Насколько правильно/не правильно, безопасно и т.п.
/**
* Shortcodes to use with the Custom Post Type
* Get the latest Custom Post Type
* Usage: [cpt_newest number=3]
*/
// Add Shortcode
function cptNewest($atts) {
// Attributes
extract(shortcode_atts(
array(
'number' => '4',
),
$atts)
);
// Code
$output = '<div class="masonry masonry--h">';
$the_query = new WP_Query(array('post_type' => ' services ', 'posts_per_page' => $number));
if ($the_query->have_posts()):
while ($the_query->have_posts()):
$the_query->the_post();
$output .= cpt_shortcode_output();
endwhile;
endif;
wp_reset_query();
return $output . '</div>';
}
add_shortcode('cpt_newest', 'cptNewest');
function cpt_shortcode_output() {
$output = '';
$output .= '<div class="masonry-brick masonry-brick--h">'
. '<h2 class="ms-meta"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>'
. '<div class="arch-thumb">';
$output .= '<a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a> ';
$output .= '</div></div>';
return $output;
}
Спасибо