0

I have this PHP code that shows a related posts element created using advanced custom fields plugin. I want to create a shortcode inside functions.php with the code and then use the shortcode in a text element of a page builder. Would someone kindly assist me with the modified code to put inside functions.php? Thanks

<?php
$posts = get_field('related_posts', false, false);
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3, 'post__in' => $posts, 'post_status' => 'publish', 'orderby' => 'post__in', 'order' => 'ASC' ));

if($loop->have_posts()) { ?>
    <div class="rel-posts">

    <?php while ($loop->have_posts()) : $loop->the_post(); ?>

        <div class="related-post">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('td_218x150'); ?></a>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        </div>

    <?php endwhile; ?>

</div>
<?php } wp_reset_query(); ?> 

2 Answers 2

0

If your code works fine then do this in function.php

    <?php
    add_shortcode( 'custom_name', 'footag_func' );
    function footag_func( $atts ) {
    $html= '';
    $posts = get_field('related_posts', false, false);
    $loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3, 'post__in' => $posts, 'post_status' => 'publish', 'orderby' => 'post__in', 'order' => 'ASC' ));

    if($loop->have_posts()) { 
       $html .= '<div class="rel-posts">';

        while ($loop->have_posts()) : $loop->the_post(); 

           $html .= '<div class="related-post">';
            $html .='<a href="'.the_permalink().'">'.the_post_thumbnail('td_218x150').'</a>';
              $html .=  '<h3><a href="'.the_permalink().'">'.the_title().'</a></h3>';
           $html .=  '</div>';

        endwhile; 

   $html .= '</div>';
   } wp_reset_query(); 
    }
    return $html;
    ?> 

your shortcode [custom_name]

4
  • Thank you for this. I have seen that the related posts appear at the top of the page created with page builder rather than at the location where I placed the shortcode (at the bottom). Is that a normal behaviour? Commented Dec 29, 2018 at 15:49
  • 1
    Shortcodes must return their contents, not directly print or echo. Commented Dec 29, 2018 at 16:27
  • @Milo I am a php newbie. How do I make the code above as you specified? Or could you kindly provide the edited code as per your suggestion? Commented Dec 30, 2018 at 8:12
  • @Mato edited the code Commented Dec 31, 2018 at 6:49
0

add_shortcode('location_start_your_application_group', 'start_your_application_group');

function start_your_application_group() {
    $start_your_application_group = '';
    $start_your_application_group .= '<section class="start-your-application">';
     if ( have_rows( 'start_your_application_group', 'option' ) ) : 
         while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row(); 
           $heading = get_sub_field( 'heading' );
             $content = get_sub_field( 'content' );

             if ( $heading !== '' ) {
                    $start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
             }
             if ( $content !== '' ) {
                $start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
             }

             $image = get_sub_field( 'image' );
             if ( $image ) { 
                $start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . $image['url'] .'" alt="' . $image['alt'] . '" /></div>';
            } 
        endwhile;
    endif;
    $start_your_application_group .= '</section>';

    return $start_your_application_group;
}
1
  • When posting an answer always try to include a bit of an explanation so that people an learn the process rather than just obtain an answer or pre-written segment of code. Commented May 2, 2020 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.