Warning: sprintf(): Too few arguments in functions

2020-02-16 03:14发布

I'm getting this PHP error in my custom breadcrumb function:

Warning: sprintf(): Too few arguments in /srv/bindings/56058a57d7424f84adac37ba6b03d3b7/code/wp-content/themes/inspire-spine/functions.php on line 151

Here is the code:

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;
    }

}

I get the error in line 7, what am I missing?

2条回答
We Are One
2楼-- · 2020-02-16 03:31
  1. dont use % sign in your string when use sprintf method

in my case: for example:

$data = call_web_service(); // return array()
foreach ($data as $d)
echo sprintf("<div>our %65 discount is : %s </div>",
$d["discount"]
);

use %65 in my string

sprintf seek every % and if counts of them is not equal to your parameters return this error

  1. dont use < ?php or ? > in your string

like this :

$weekday = "sunday";
echo sprintf("<div>our <?php echo $date;?> day is : %s </div>",
$weekday
);
查看更多
祖国的老花朵
3楼-- · 2020-02-16 03:45

Your sprintf string has 2 placeholders, your filling one.

<?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');

https://3v4l.org/nu8Ht

查看更多
登录 后发表回答