Показывать сообщения по идентификатору с помощью шорткода - PullRequest
0 голосов
/ 24 сентября 2018
add_shortcode('testimonials', 'testimonial_query');
function testimonial_query($atts, $content){
extract(shortcode_atts(array( // a few default values

'id' => null,
'posts_per_page' => '1',
'caller_get_posts' => 1)
, $atts));
$args = array(
'post_type' => 'testimonial',
'numberposts' => -1
);
global $post;
$posts = new WP_Query($args);
$output = '';
if ($posts->have_posts( $attributes['id']))
while ($posts->have_posts( $attributes['id'])):
$posts->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
$out = '<div class="testimonial-img">
<img src="'.$url.'" />
</div>
<div class="testimonial_content">
<img src="'.get_field('logo').'" alt="icon">
<p class="testimonial_desc">'.get_the_content().'</p>
<div class="author-details">
<img src="'.get_field('author_image').'" alt="image">
<p>'.get_field('author_name').' <span>'.get_field('author_designation').'</span></p>
</div>';
// add here more...
$out .='</div>';
endwhile;
else
return; // no posts found

wp_reset_query();
return html_entity_decode($out);
}

Я пытаюсь получить сообщения по идентификатору с помощью шорткода [testimonials id="272"] в functions.php файле.Я использовал приведенный ниже код, он работает, но не может получать сообщения по идентификатору.Может ли кто-нибудь помочь мне с этой проблемой, если я сделал неправильно?Заранее спасибо.

1 Ответ

0 голосов
/ 24 сентября 2018

Попробуйте этот код

add_shortcode('testimonials', 'testimonial_query');

function testimonial_query($atts, $content){

    extract(shortcode_atts(array( // a few default values
    'id' => null,
    'posts_per_page' => '1',
    'caller_get_posts' => 1)
    , $atts));


    $args = array(
    'post_type' => 'testimonial',
    'numberposts' => -1
    );

    if($atts['id']){
     $args['p'] = $atts['id'];
    }

    global $post;
    $posts = new WP_Query($args);
    $output = '';


    if ($posts->have_posts())
        while ($posts->have_posts()):
            $posts->the_post();
            $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
            $out = '<div class="testimonial-img">
            <img src="'.$url.'" />
            </div>
            <div class="testimonial_content">
            <img src="'.get_field('logo').'" alt="icon">
            <p class="testimonial_desc">'.get_the_content().'</p>
            <div class="author-details">
            <img src="'.get_field('author_image').'" alt="image">
            <p>'.get_field('author_name').' <span>'.get_field('author_designation').'</span></p>
            </div>';
            // add here more...
            $out .='</div>';
        endwhile;
    else
    return; // no posts found
    wp_reset_query();
    return html_entity_decode($out);
}
...