我如何让这个 wp_query 在短代码中工作?

How do I get this wp_query working in a shortcode?

我无法让 wp_query 使用简码。我想我根据 wp codex 是正确的,但它一直破坏我的网站 - 500 错误。它位于创世纪自定义主题的外部文件中。

文件在子文件夹中,我有 included_once 文件并将 add_shortcode 函数添加到 functions.php 文件。当我注释掉 include_once 时,该网站很好,所以我猜我在函数中遗漏了一些东西。

<?php
function exp_post_slider_shortcode( $atts ) {

$a = shortcode_atts( array(
    'cat' => '15',
    'posts_per_page' => '3',
), $atts ); 

$output = '';   
 $args = array(

            'cat' => $a['cat'],
            'posts_per_page' => $a['posts_per_page'],
);
    $post_slider = new WP_Query( $args );

if ( $post_slider->have_posts() ) {
    // The Loop
     $output .=  '<div class="exp-post-slider-container">'
     $output .= '<div class="owl-carousel owl-theme exp-post-slider">'
    while ( $post_slider->have_posts() ) {
        $post_slider->the_post();
        $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        $output .= '<div class="exp-cat-slide" style="background-image:url('.$feat_image_url.'); background-size:cover; background-repeat:no-repeat;">';
        $output .= '<div class="exp-slide-post-info">';
        $output .= '<h2>' . get_the_title() . '</h2>';
        $output .= '<p>' . get_the_author() . ' | ' . get_the_date() . '</p>';
        $output .= '<p><a href="' . get_permalink() . '" class="exp-post-link-btn">View Post</a>';
        $output .= '</div></div>';
    }

    wp_reset_postdata();
} else { 

        $output .= '<div class="exp-cat-slide" style="background-image:url(https://webclient.co/explore/wp-content/uploads/2019/04/looking-out-no-posts.jpg); background-size:cover; background-repeat:no-repeat;">';
        $output .= '<div class="exp-slide-post-info">';
        $output .= '<h2>No Adventures Posted Here Yet</h2>';
        $output .= '<p>Check Back Soon!</p>';
        $output .= '<p><a href="https://webclient.co/explore/blog/" class="exp-post-link-btn">Check Out Our Blog</a>';
        $output .= '</div></div>';
}


$output .= '</div>'
$output .= '</div>'

return $output;
} ?>

我正在尝试将 to 输出到 owl 滑块。将它作为主题挂钩中的函数发送到 运行 没有问题,但我需要它作为具有类别和 post 参数数量的简码工作。

您需要 add_shortcode 函数才能将您的函数转换为简码。来自 WP 的非常好的文档:https://codex.wordpress.org/Shortcode_API

可能看起来像这样:

add_shortcode( 'exp_post_slider', 'exp_post_slider_shortcode' );

然后在编辑器中,您可以在您的内容中使用它来触发您的 exp_post_slider_shortcode 函数并生成输出:

[exp_post_slider whatever_args="whatever..."]

我刚刚将您提供的代码移至 Wordpress 测试环境,很明显您在使用 $output 变量时在行尾缺少一些 ;

使用下面的代码我可以输出你的简码:

add_shortcode('test','exp_post_slider_shortcode');

function exp_post_slider_shortcode( $atts ) {

$a = shortcode_atts( array(
    'cat' => '15',
    'posts_per_page' => '3',
), $atts ); 

$output = '';   
 $args = array(

            'cat' => $a['cat'],
            'posts_per_page' => $a['posts_per_page'],
);
    $post_slider = new WP_Query( $args );

if ( $post_slider->have_posts() ) {
    // The Loop
     $output .=  '<div class="exp-post-slider-container">';
     $output .= '<div class="owl-carousel owl-theme exp-post-slider">';
    while ( $post_slider->have_posts() ) {
        $post_slider->the_post();
        $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        $output .= '<div class="exp-cat-slide" style="background-image:url('.$feat_image_url.'); background-size:cover; background-repeat:no-repeat;">';
        $output .= '<div class="exp-slide-post-info">';
        $output .= '<h2>' . get_the_title() . '</h2>';
        $output .= '<p>' . get_the_author() . ' | ' . get_the_date() . '</p>';
        $output .= '<p><a href="' . get_permalink() . '" class="exp-post-link-btn">View Post</a>';
        $output .= '</div></div>';
    }

    wp_reset_postdata();
} else { 

        $output .= '<div class="exp-cat-slide" style="background-image:url(https://webclient.co/explore/wp-content/uploads/2019/04/looking-out-no-posts.jpg); background-size:cover; background-repeat:no-repeat;">';
        $output .= '<div class="exp-slide-post-info">';
        $output .= '<h2>No Adventures Posted Here Yet</h2>';
        $output .= '<p>Check Back Soon!</p>';
        $output .= '<p><a href="https://webclient.co/explore/blog/" class="exp-post-link-btn">Check Out Our Blog</a>';
        $output .= '</div></div>';
}


$output .= '</div>';
$output .= '</div>';

return $output;
}

更多旁注:请注意输出缓冲区。如果您遇到短代码内容没有放在您期望的位置,请查看 ob_get_clean() 函数。