Reputation: 481
I am using a theme using sprintf which I am very new to. I cannot get custom support from the developer on this so I am trying to figure it out on my own.
Not understanding how sprintf works, I've Googled loads of pages trying to find a fix to what I thought would have been simple.
The theme uses %s to load a text string, in this case the title of the page. I just want the title to link to the page! That's it! No less, no more. I was able to come up with something close below:
// Featured columns
case 'columns':
$count = Website::getThemeOption('front_page/columns/count');
$classes = array('one', 'two', 'three', 'four');
$columns = array();
for ($i = 0; $i < $count; $i++) {
extract(Website::getThemeOption('front_page/columns/column/'.$i, true)->toArray());
$text = DroneFunc::stringToHTML($text, false);
if ($more && $link) {
$text .= sprintf(' <a href="%s" class="more">%s</a>', $link, $more);
}
$columns[] = sprintf(
'<li class="column">'.
'<img src="%s/data/img/icons/32/%s" alt="" class="icon">'.
'<h1><a href="%1$s">%s</a></h1><p>%s</p>'.
'</li>',
Website::get('template_uri'), $icon, $title, $text
);
}
?>
<section class="columns <?php echo $classes[$count-1]; ?> clear">
<ul>
<?php echo implode('', $columns); ?>
</ul>
</section>
<?php
break;
Now originally, there was no hyperlink reference... I added that in. What this does now is make the h1 title clickable but it just goes to the root of the theme folder, not the title's page.
Any help in understanding this and making it work would be greatly appreciated.
Upvotes: 0
Views: 2676
Reputation: 41934
Sprintf
basixSprintf
adds the parameters to the first parameter at the place of the placeholders (which begins with %
).
For instance:
$name = 'Rob';
$str = sprintf('Hello, I am %s', $name); // become: Hello, I am Rob
The letter after %
is the first letter of the type of the parameter. A string is %s
, an decimal is %d
. For instance:
$name = 'Rob';
$age = 26;
$str = sprintf('Hello, I am %s and I am %d years old.', $name, $age);
// become: Hello, I am Rob and I am 26 years old.
Sprintf
use the order of the parameters to determine where to place it in the string. The first placeholder gets the second parameter, the second placeholder the thirth, ect.
If you want to change this, you need to specify this between %
and the type. You do this by <place>$
where <place>
is the place nummer.
$name = 'Rob';
$age = 26;
$str = sprintf('Hello, I am %2$s and I am %1$d years old.', $age, $name);
// become: Hello, I am Rob and I am 26 years old.
You do this:
'<h1><a href="%1$s">%s</a></h1><p>%s</p>'
The %1$s
is the first parameter, which is the template_uri
. I think this isn't the url which you want to link to? You want to link to the uri in $link
. Just place that in the parameters and refer to it:
sprintf(
'...'.
'<h1><a href="%s">%s</a></h1><p>%s</p>',
Website::get('template_uri'), $icon, $link, $title, $text
);
Upvotes: 3