1

I made my own custom post type and the shortcode to output the custom post type. It all works fine so far, but once I tried adding more posts I still get only one on the page and not more. posts_per_page is set to 3 or more but still nothing.

// 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);
}
3

you are re-initializing the out variable that is why it shows only one product

// 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);
}
  • Can you explain a little bit more? I don't see any changes in your code and it's still not working? – Limpuls Mar 12 '18 at 12:41
  • Ops, my bad it's working. Still don't get it with re-initializing – Limpuls Mar 12 '18 at 12:44
  • in your code $out = '<div class="use_case_boxes"> i made it $out .= '<div class="use_case_boxes"> you are defining the variable again so the old values get replaced – Visakh B Sujathan Mar 13 '18 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.