1

I am new to wordpress and coding and I need some help. I have a custom post type called 'endorsement' that would like to create a shortcode for that will output a list of posts sorted by a custom field 'last name' Here is the code I am using in my themes functions.php. I am using the Avada theme

function shortcode_endorsements($atts) {
    // global $wp_query,
    //     $post;

    $atts = shortcode_atts( array(
        'line' => ''
    ), $atts );

    $loop = new WP_Query( array(
      'post_type'         => 'endorsement',
      'post_status'       => 'publish',
      'posts_per_page'    =>  100,
      'meta_key'                => 'last_name',
        'orderby'                 => 'meta_value',
        'order'                   => 'ASC'
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }
    $results = '';
    if( $loop->have_posts() ) {
      $results .= 
      '<div id="endorsments">';
        while ($loop->have_posts()) : $loop->the_post();
          $results .=
          '<div class="fusion-layout-column fusion_builder_column fusion_builder_column_1_4 fusion-one-fourth 1_4"><div class="endorsment-name">' . the_title() . '</div></div>';
        endwhile;
      $results .= 
      '</div>';
    }

    wp_reset_postdata();
    return $results;
}
add_shortcode('endorsements', 'shortcode_endorsements');

This code is really just a mashup of examples I have found here. The shortcode outputs the post titles to the page in the correct order but without the HTML markup and at the top of the page no matter where I embed the shortcode. Ideas?

  • 1
    the_title() != get_the_title() – kero May 8 '18 at 20:29
  • Shoot, I should have asked earlier. I'll keep this post up in case anyone wants to provide feedback on other aspects of my code. THANK YOU! onward – whathappened May 8 '18 at 20:33
  • That fixed it @kero – whathappened May 8 '18 at 20:34
  • You're welcome, I've been developing a theme all day long today and made similar mistakes ;) This site is rather for questions&answers about specific problems, if you want feedback, you could try CodeReview (I don't know how many WordPress users are active there). Actually I flagged to close your question as it is just a minor mistake/typo and I'm sure there are already answers about this here, there is no shame in deleting it – kero May 8 '18 at 20:39
  • 1
    Possible duplicate of Shortcode outputs at the top of the_content – Michael May 8 '18 at 23:50

Your Answer

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

Browse other questions tagged or ask your own question.