0

i use ACF plugin and i want put this query in shortcode. but i have a problem. (not output anything)

    add_shortcode('pishnahadi' , 'kn_inline_related');
global $post;
function kn_inline_related( $atts ) {
$html= '';
$posts = get_field('pishnahadi', false, false);
$loop = new WP_Query(array('post_type' => 'news', '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 .=  '<h3><a href="' .the_permalink get_permalink() . '">' . the_title( '', '', false ) . '</a></h3>';
             $html .=  '</div>';

        endwhile;

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

update: (The below code is correct and worked)

add_shortcode('pishnahadi' , 'kn_inline_related');
global $post;
function kn_inline_related( $atts ) {
$html= '';
$posts = get_field('pishnahadi', false, false);
$loop = new WP_Query(array('post_type' => 'news', '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 .=  '<h3><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></h3>';
             $html .=  '</div>';

        endwhile;

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

1 Answer 1

0

the_permalink() echoes the output, and so does the_title() by default. So you should use get_permalink() and get_the_title():

$html .=  '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';

Or use the_title() with the third parameter set to false to return and not echo the output:

$html .=  '<h3><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></h3>';

Update: The return $html; (in your original non-edited question) was actually outside the function and that would result the shortcode to give you no output! So make sure that it's inside the function in your actual code. :) And for secondary/custom WP_Query instances like your $loop variable, you just need to call wp_reset_postdata() and not wp_reset_query() since secondary queries don't touch the (global) $wp_query variable. Unless of course, in your code, you modified that variable. But why would you do that.

6
  • this not output anything Commented Apr 5, 2020 at 9:06
  • Try adding global $post; at the top of your function. Commented Apr 5, 2020 at 9:07
  • There is still a problem. Even with the exception of permalink and title, the Div are not displayed in the output Commented Apr 5, 2020 at 9:13
  • 1
    Edit the question and add your current code, but in the question, the return $html; is outside the function. And I'm no longer getting any issues after using the non-echoing functions. And you can try clearing your site/browser caches. Commented Apr 5, 2020 at 9:20
  • 1
    TNX , worked. return $html; was outside the function. Commented Apr 5, 2020 at 9:35

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.