user5896012
user5896012

Reputation:

How To insert str_replace in single_post_title?

How do I insert the function str_replace in single_post_title ?

<a class="tbutton large" href="http://domain.com/mp3/<?php single_post_title($str_replace =' ','+',''); ?>.html"><span> DOWNLOAD </span></a>

But it is not working

Example :

Post Title Input Value:

WIZ KHALIFA – COWBOY (OFFICIAL AUDIO)

With str_replace, I wish to create a url like this:

http://domain.com/mp3/wiz-khalifa-cowboy-official-audio.html

Thank you.

Upvotes: 1

Views: 471

Answers (2)

farooq
farooq

Reputation: 489

You cannot use str_replace() on single_post_title() because it echoes the value instead of returning it.

You should use get_the_title() to get the title and use sanitize_title() to convert it into the format you want.

<?php

$title = get_the_title();
$download = "http://domain.com/mp3/" . sanitize_title($title) . ".html";

?>

<a href="<?php echo $download; ?>">Download</a>    

Upvotes: 2

ACKA
ACKA

Reputation: 27

Remove the $ sign from the start of str_replace to begin with

$input = "WIZ KHALIFA – COWBOY (OFFICIAL AUDIO";

<a class="tbutton large" href="http://domain.com/mp3/<?php single_post_title(str_replace(" ", "-", "$input")); ?>.html"><span> DOWNLOAD </span></a>

See the below for more help:

str_replace

Upvotes: 0

Related Questions