我在自定义痕迹导航功能中遇到此PHP错误:
警告:sprintf():第151行/srv/bindings/56058a57d7424f84adac37ba6b03d3b7/code/wp-content/themes/inspire-spine/functions.php中的参数太少
以下是代码:
if ( is_page() && $parent_id ) {
if ($parent_id != $frontpage_id) {
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
if ($parent_id != $frontpage_id) {
$breadcrumbs[] = sprintf($link, get_permalink($page->ID)); //, get_the_title($page->ID));
}
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
echo $breadcrumbs[$i];
if ($i != count($breadcrumbs)-1) echo $delimiter;
}
}
if ($show_current == 1) {
if ($show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id)) echo $delimiter;
echo $before . get_the_title() . $after;
}
}
我在第7行得到错误,我错过了什么?
答案 0 :(得分:1)
您的sprintf字符串有2个占位符,您的填充符。
<?php
// bad
echo sprintf('<a href="%1$s">%2$s</a>', 'http://example.com');
// good
echo sprintf('<a href="%1$s">%2$s</a>', 'http://example.com', 'Link Name');
答案 1 :(得分:0)
就我而言:例如:
$data = call_web_service(); // return array()
foreach ($data as $d)
echo sprintf("<div>our %65 discount is : %s </div>",
$d["discount"]
);
在我的字符串中使用%65
sprintf搜寻每一个%,如果它们的计数不等于您的参数,则会返回此错误
像这样:
$weekday = "sunday";
echo sprintf("<div>our <?php echo $date;?> day is : %s </div>",
$weekday
);