web-dev-qa-db-fra.com

WP_Query n’imprime qu’une publication lorsque posts_per_page est défini sur plusieurs

J'ai créé mon propre type de message personnalisé et le shortcode permettant de générer le type de message personnalisé. Tout fonctionne bien jusqu'à présent, mais une fois que j'ai essayé d'ajouter plus de messages, je n'en ai toujours qu'un sur la page, pas plus. posts_per_page est réglé sur 3 ou plus mais toujours rien.

// Custom Post Type for Use Cases page slider

function create_post_type() {
  register_post_type( 'use_cases',
    array(
      'labels' => array(
        'name' => __( 'Use Cases' ),
        'singular_name' => __( 'Use_Case' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

// Shortcode for Use Cases caption

add_shortcode('Use_Case', 'use_case_shortcode_query');
function use_case_shortcode_query($atts, $content){
    echo "working";
  extract(shortcode_atts(array( // a few default values
   'posts_per_page' => '3',
   'post_type' => 'use_cases',
        )
   , $atts));

  global $post;

  $posts = new WP_Query(array( // a few default values
   'posts_per_page' => '3',
   'post_type' => 'use_cases'
        ));
    //print_r($posts);
  $output = '';
    if ($posts->have_posts()):
        while ($posts->have_posts()):
                    //echo var_dump($posts->the_post());
            $posts->the_post();
            $out = '<div class="use_case_boxes">
                <h4>Name: <a href="'.get_permalink().'" title="' . get_the_title() . '">'.get_the_title() .'</a></h4>
                <p class="Film_desc">'.get_the_content().'</p>';
                // add here more...
            $out .='</div>';
    /* these arguments will be available from inside $content
        get_permalink()
        get_the_content()
        get_the_category_list(', ')
        get_the_title()
        and custom fields
        get_post_meta($post->ID, 'field_name', true);
    */
    endwhile;
  else:
    return; // no posts found
endif;
  wp_reset_query();
  return html_entity_decode($out);
}
1
Limpuls

vous réinitialisez la variable out, c’est pourquoi elle ne présente qu’un seul produit

// Custom Post Type for Use Cases page slider

function create_post_type() {
  register_post_type( 'use_cases',
    array(
      'labels' => array(
        'name' => __( 'Use Cases' ),
        'singular_name' => __( 'Use_Case' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

// Shortcode for Use Cases caption

add_shortcode('Use_Case', 'use_case_shortcode_query');
function use_case_shortcode_query($atts, $content){
    echo "working";
  extract(shortcode_atts(array( // a few default values
   'posts_per_page' => '3',
   'post_type' => 'use_cases',
        )
   , $atts));

  global $post;

  $posts = new WP_Query(array( // a few default values
   'posts_per_page' => '3',
   'post_type' => 'use_cases'
        ));
    //print_r($posts);
  $output = '';
$out = '';
    if ($posts->have_posts()):
        while ($posts->have_posts()):
                    //echo var_dump($posts->the_post());
            $posts->the_post();
            $out .= '<div class="use_case_boxes">
                <h4>Name: <a href="'.get_permalink().'" title="' . get_the_title() . '">'.get_the_title() .'</a></h4>
                <p class="Film_desc">'.get_the_content().'</p>';
                // add here more...
            $out .='</div>';
    /* these arguments will be available from inside $content
        get_permalink()
        get_the_content()
        get_the_category_list(', ')
        get_the_title()
        and custom fields
        get_post_meta($post->ID, 'field_name', true);
    */
    endwhile;
  else:
    return; // no posts found
endif;
  wp_reset_query();
  return html_entity_decode($out);
}
3
Visakh B Sujathan