wp query - create custom shortcode wp and put php code in

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

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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744593304a4582739.html

相关推荐

  • wp query - create custom shortcode wp and put php code in

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

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信