php - WP 简码忽略传递的值
问题描述
短代码中的 WP_Query 忽略传递的值
一直试图让它工作几个小时。无论我做什么,WP 都使用默认值并忽略传递给函数的值。
// Add Shortcode
function ima_featured_member( $atts ) {
// Attributes
$attributes = shortcode_atts(
array(
'numb' => '1',
),
$atts,
'featured'
);
$dirloop = new WP_Query( array(
'post_type' => 'member',
'post_status' => 'publish',
'posts_per_page' => $attributes['numb'],
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'elc_member_featured',
'value' => '1',
'compare' => '=='
)
)
));
if ($dirloop->have_posts())
{
$output = "\n";
while ( $dirloop->have_posts() ) : $dirloop->the_post();
$output .= ''.get_the_title().'';
endwhile;
$output .= "\n";
}
else
{
$output = "nothing";
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'featured', 'ima_featured_member' );
[featured = '2'] 没有通过。没有错误,没有警告,只是不起作用。非常感谢任何帮助或现场。
解决方案
为了工作,您需要对此进行一些更改。
更改以下代码:
$attributes = shortcode_atts(
array(
'numb' => '1',
),
$atts,
'featured'
);
至
$attributes = shortcode_atts(
array(
'numb' => '1',
),
$attributes,
'featured'
);
同样对于获取 2 个帖子的简码,您应该编写如下简码:
[featured numb='2']
这应该适合你。
推荐阅读
- sql - 如果满足条件,SQL 触发器将数据从一个表插入到另一个表
- data-science - 有没有办法在 Nim 中处理 NetCDF 文件?
- firebase - 未处理的异常:PlatformException(执行获取时出错,PERMISSION_DENIED:缺少权限或权限不足。,null)
- java - renameField 后出现错误 crashlytics firebase
- sequelize.js - 关联模型 1:M SequelizeDatabaseError: column [Model].id 不存在
- android - LiveData 测试:如何跟踪值变化的顺序?
- python - 在真实世界地图上绘制 Networkx 图
- python - Turtle.goto 函数仅适用于两个正坐标?
- sql - SQL - 需要以选择字段为条件的字段之间的计算
- javascript - 如何将 ajax 与 jquery 一起使用以获取 json 文件