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;
}
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;
}
Share
Improve this question
edited Apr 6, 2020 at 10:47
omid chahardoli
asked Apr 5, 2020 at 8:41
omid chahardoliomid chahardoli
191 silver badge9 bronze badges
1 Answer
Reset to default 0the_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.