【问题标题】:Wordpress shortcode issueWordpress 简码问题
【发布时间】:2019-03-15 11:50:33
【问题描述】:

我有这个简码代码

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 类别和标签中,则可以完美运行...

请提前帮助和感谢!

【问题讨论】:

标签: php wordpress


【解决方案1】:

无法评论,所以我会发布它作为答案。

请看:

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

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

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

也许这会解决您的问题?

【讨论】:

  • 您是否也在此处使用了 shortcode_atts_postcat 过滤器?如果是这样,可能是过滤器在某处干扰?我还注意到 $id = get_the_ID(get_the_ID());这是故意的吗?
  • 不(关于 shortcode_atts_postcat)和 $id = get_the_ID(get_the_ID());这是一个错误......我现在写对了,但仍然有问题
猜你喜欢
  • 2011-08-21
  • 1970-01-01
  • 2018-12-02
  • 2013-07-25
  • 2016-11-20
  • 2020-01-21
  • 1970-01-01
  • 2017-05-17
  • 2014-09-23
相关资源
最近更新 更多