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");
在后端,当我按“添加新类别”来创建一个新类别时(呃!)不起作用。它添加的类别但页面不刷新。我很确定它与这段代码有关,但我真的不知道是什么......如果我不将这个文件包含在functions.php类别和标签中,效果很好......
请提前帮助和感谢!
解决方案
无法评论,所以我将其发布为答案。
请看:
return $output;
wp_reset_query();
wp_reset_postdata();
我相信返回后的代码不会被执行。所以改成:
wp_reset_query();
wp_reset_postdata();
return $output;
也许这会解决你的问题?
推荐阅读
- spring - 使用 Apache Beam 的 Spring
- mysql - 从其他属性或 Java VM 选项调用 Spring Boot 应用程序属性
- javascript - 从 javascript 一致地访问 gridview 行数据
- google-apps-script - 在对象 Nom.Candidat 中找不到函数 getValue
- c# - 使用 EF Core 插入对象时,“无法为表中的标识列插入显式值”
- amazon-web-services - 使用 cloudformation 模板授予对 s3 存储桶对象的公共读取和查看访问权限
- reporting-services - 如何在ssrs中获取多个月的求和数据的平均值
- java - 如何遍历页面、查找标题文本并使用 Selenium 验证文本?
- visual-studio-code - 有没有办法改变折叠树的外观?
- java - 如何解决第二种方法的“非法开始表达”错误?