首页 > 解决方案 > Wordpress 简码问题

问题描述

我有这个简码代码

function custom_query_shortcode( $atts ) {

// EXAMPLE USAGE:
// [postcat name="Lorem"]

// Defaults
$atts = shortcode_atts(array(
    'name' => ''
), $atts, 'postcat');


// Reset and setup variables
$output     = '';
$title      = '';
$link       = '';
$featImg    = '';

// the loop
$argsSC = array(
    'post_type'     => 'post',
    'orderby'       => 'date',
    'order'         => 'DESC',
    'posts_per_page'=> -1,
    'category_name' => $atts['name']
); 
$querySC = new WP_Query( $argsSC );

if ( $querySC->have_posts() ) {

    $output .= '<div class="row" style="margin-top: 30px;">';

    while ( $querySC->have_posts() ) {

        $querySC->the_post();

        $title      = get_the_title(get_the_ID());
        $link       = get_permalink(get_the_ID());
        $featImg    = ( has_post_thumbnail() ? get_the_post_thumbnail_url(get_the_ID(), 'large') : get_template_directory_uri() . '/imgs/No_Image_Available.gif' );
        $id         = get_the_ID(get_the_ID());
        $chartL     = get_field( 'chart' );
        $storyL     = get_field( 'story' );

        // output all findings - CUSTOMIZE TO YOUR LIKING
        //$output .= "<div><img src='$featImg'><a href='$link'>$title</a></div>";
        $output .= '<article id="post-'.$id.'" class="csseco-tssf-t col-12 col-md-6">';
        $output .= '    <div class="standard-featured bg-img-el" style="background-image: url('.$featImg.')">';
        $output .= '        <a class="standard-featured-link" href="'.$link.'"></a>';
        $output .= '        <div class="cat-tssf">';
        if ( !empty($chartL['url']) ) {
            $output .= '        <a href="'.$chartL['url'].'" target="'.$chartL['target'].'">Charts</a>';
        }
        if ( !empty($storyL['url']) ) {
            $output .= '        <a href="'.$storyL['url'].'" target="'.$storyL['target'].'">The Story</a>';
        }
        $output .= '        </div>';
        $output .= '        <h3 class="ttl-tssf">';
        $output .= '            <span class="dtable">';
        $output .= '                <span class="dtable-cell">';
        $output .= '                    <a href="'.$link.'" rel="bookmark">';
        $output .=                          $title;
        $output .= '                    </a>';
        $output .= '                </span>';
        $output .= '            </span>';
        $output .= '        </h3>';
        $output .= '    </div>';
        $output .= '</article>';

    }

    $output .= '</div>';

} else {

    $output .= "nothing found.";

}
return $output;
wp_reset_query();
wp_reset_postdata();


}
add_shortcode("postcat", "custom_query_shortcode");

在后端,当我按“添加新类别”来创建一个新类别时(呃!)不起作用。它添加的类别但页面不刷新。我很确定它与这段代码有关,但我真的不知道是什么......如果我不将这个文件包含在functions.php类别和标签中,效果很好......

请提前帮助和感谢!

标签: phpwordpress

解决方案


无法评论,所以我将其发布为答案。

请看:

return $output;
wp_reset_query();
wp_reset_postdata();

我相信返回后的代码不会被执行。所以改成:

wp_reset_query();
wp_reset_postdata();
return $output;

也许这会解决你的问题?


推荐阅读