I have created a CPT with 3 custom fields (custom fields are done in ACF plugin). I have a shortcode that displays the custom post in my theme. So far, so good.

My problem is when I have multiple CPT's, I want to have a shortcode attribute that let's the user specify which custom post they want to display.

Currently, depending on what I have posts_per_page set to dictates which post is displayed. It is completely ignoring the attributes.

Here is what I have:

// create custom post for jumbotron
function jumbotron_post_type()
{
    $labels = array(
        'name' => 'Jumbotrons',
        'singular_name' => 'jumbotron');
    $supports = array(
        'title'        
    );
    $args=array(
    'labels' => $labels,
    'supports' => $supports,
    'public' => true
    );
    register_post_type('Jumbotrons', $args);
}
add_action('init', 'jumbotron_post_type');

// create shortcode for display

function show_jumbotron($atts, $content = null )
{
   extract(shortcode_atts(array(
      'id' => 'new-jumbo',
      'per_page' => -1,
  ), $atts));

    $args = array(
        'posts_per_page' => $per_page, 
        'title' => $id,
        'post_type' =>'Jumbotrons'        
    );
    $results = new WP_Query($args);

    while ($results->have_posts()):
        $results->the_post();
    ob_start(); ?>
            <section class="jumbotron">
                <div class="jumbotron--container">
                    <h1><?php the_field('jumbotron_title'); ?></h1>
                    <p class="info"><?php the_field('jumbotron_body'); ?></p>
                    <h2 class="u--text__right"><?php the_field('jumbotron_end_heading'); ?></h2>
                </div>
            </section>
            <?php
        $content = ob_get_clean();
    endwhile;
    wp_reset_postdata();
    return $content;
};

add_shortcode('jumbotron', 'show_jumbotron');

In the theme, I have:

<?php echo do_shortcode('[jumbotron id="Homepage Jumbotron"]');?>   

If I set posts_per_page to -1, it shows the oldest custom post. If I set to 1 then it shows the most recent.

The 2 CPT titles are:

  • New Jumbo
  • Homepage Jumbotron

My intention would be for [jumbotron title="Homepage Jumbotron"] to display the correct custom post based on the title entered with the default being "New Jumbo".

2 Answers

0
megler On

I found the error. The ID needed to be set to an empty string

'id' => ''

The working code is:

// create shortcode for display

function show_jumbotron($atts, $content = null )
{
    extract(shortcode_atts(array(
      'id' => '',
      'per_page' => -1,
  ), $atts));

    $args = array(
        'post_type' =>'Jumbotrons',
        'posts_per_page' => $per_page, 
        'title' => $id
    );
    $results = new WP_Query($args);

    while ($results->have_posts()):
        $results->the_post();
    ob_start(); ?>
            <section class="jumbotron">
                <div class="jumbotron--container">
                    <h1><?php the_field('jumbotron_title'); ?></h1>
                    <p class="info"><?php the_field('jumbotron_body'); ?></p>
                    <h2 class="u--text__right"><?php the_field('jumbotron_end_heading'); ?></h2>
                </div>
            </section>
            <?php
        $content = ob_get_clean();
    endwhile;
    wp_reset_postdata();
    return $content;
};

add_shortcode('jumbotron', 'show_jumbotron');
0
Community On
Hey megler use my below code. I hope it should work.

function show_jumbotron($atts, $content = null )
{
   extract(shortcode_atts(array(
      'title' => 'New Jumbo',
      'per_page' => 1,
  ), $atts));

    $args = array(
        'posts_per_page' => $per_page, 
        'title' => $title,
        'post_type' =>'Jumbotrons'        
    );
    $results = new WP_Query($args);

    while ($results->have_posts()):
        $results->the_post();
    ob_start(); ?>
            <section class="jumbotron">
                <div class="jumbotron--container">
                    <h1><?php the_field('jumbotron_title'); ?></h1>
                    <p class="info"><?php the_field('jumbotron_body'); ?></p>
                    <h2 class="u--text__right"><?php the_field('jumbotron_end_heading'); ?></h2>
                </div>
            </section>
            <?php
        $content = ob_get_clean();
    endwhile;
    wp_reset_postdata();
    return $content;
};

add_shortcode('jumbotron', 'show_jumbotron');

Then add shortcode like this:

<?php echo do_shortcode('[jumbotron title="Homepage Jumbotron"]');?>   
<?php echo do_shortcode('[jumbotron title="New Jumbo"]');?>   

I hope this will work. 

Thank you.