WordPress简码问题

时间:2018-10-10 14:20:41

标签: php 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");

在后端,当我按“添加新类别”以创建新类别时(duh!)不起作用。已添加类别,但页面未刷新。我很确定这段代码可以解决这个问题,但是我真的不知道该怎么办...如果我不将此文件包含在functions.php类别和标签中,那么效果很好...

请先帮助并感谢!

1 个答案:

答案 0 :(得分:0)

无法发表评论,因此我将其发布为答案。

请查看:

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

我相信,返回后的代码将不会执行。因此将其更改为:

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

也许这可以解决您的问题?

相关问题