我如何使这个wp_query以简码工作?

时间:2019-04-04 14:08:47

标签: php wordpress wordpress-shortcode

我无法获得以简码工作的wp_query。我想我根据wp Codex正确了,但它不断破坏我的网站-500错误。它位于创世纪自定义主题的外部文件中。

该文件位于子文件夹中,我已经包含文件一次,并将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;
} ?>

我正试图将其输出到owl滑块。在主题挂钩中使其作为功能运行时没问题,但我需要它作为具有类别和后置参数数量的简码来工作。

2 个答案:

答案 0 :(得分:1)

我刚刚将您提供的代码移到了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()函数。

答案 1 :(得分:0)

您需要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..."]