2011-01-22 35 views
2

我試圖呈現一個100%符合HTML5的WordPress主題,並通過除了一個障礙之外的所有方式來管理我的方式。如何製作WordPress的<?php the_title_attribute(); ?>爲空間渲染%20?

在某些柱的端我顯示一個「資料Tweet」鏈接,它使用下面的源代碼中的主題模板:

在網址
<a href="http://twitter.com/share?text=<?php the_title_attribute(); ?>&amp;via=ianhines&amp;url=<?php echo simple_url_shortener('','service=bit.ly+key&apikey=R_a6dc414291bb882024ddd99690f5eb61&login=ianhines&cache=no'); ?>" title="Share This Article on Twitter">Tweet</a> 

HTML5不允許對具有空間。它們必須呈現爲%20。但是,<?php the_title_attribute; ?>會呈現保留空格的帖子標題的XHTML安全版本。

一個例子URL(使用上面的模板源代碼渲染):

a href="http://twitter.com/share?text=Twitter, Reblog, and Email Comments&via=ianhines&url=http://ihin.es/eCoYN9" title="Share This Article on Twitter">Tweet</a> 

有沒有什麼辦法可以強制WordPress的呈現在此URL字符串爲%20的空間,從而使我的網站完全HTML5兼容?

回答

3

好了,只是包裝的the_title_attribute()urlencode()

/share?text=<?php echo urlencode(the_title_attribute()); ?>&amp;via= 

編輯:好了,由於這一評論,你需要做這樣的事情:

<?php 
ob_start(); 
the_title_attribute(); 
$title = ob_get_clean(); 
?> 
/share?text=<?php echo urlencode($title); ?>&amp;via= 

EDIT2 :看着the docs for the_title_attribute

/share?text=<?php echo urlencode(the_title_attribute('echo=0')); ?>&amp;via= 
+0

`the_title_attribute()`實際上`echo'文本,所以這不起作用。 – fredley 2011-01-22 14:13:42

2
<?php 
    $spaceurl=the_title_attribute('echo=0'); 
    $nonspaceurl=preg_replace('\s','%20',spaceurl); 
?> 

<a href="<?php echo $nonspaceurl; ?>"> 
    my link text 
</a> 

編輯

我加echo=0,而不是返回顯示它的文字,看到the_title_attribute

0

我的建議是使用的the_title_attribute()urlencode()空間回聲PARAM:

<a href="http://twitter.com/share?text=<?php echo urlencode(the_title_attribute('', '', 0)); ?>&amp;via=ianhines&amp;url=<?php echo simple_url_shortener('','service=bit.ly+key&apikey=R_a6dc414291bb882024ddd99690f5eb61&login=ianhines&cache=no'); ?>" title="Share This Article on Twitter">Tweet</a> 
2

傳遞給the_title_attribute() 0值使得其返回,而不是附和它的結果。

<?php 
    $urltitle= str_replace(' ','%20',the_title_attribute('echo=0')); //value of 0 to return rather than echo result 
?> 

<a href="http://twitter.com/share?text=<?php echo $urltitle; ?>&amp;via=ianhines&amp;url=<?php echo simple_url_shortener('','service=bit.ly+key&apikey=R_a6dc414291bb882024ddd99690f5eb61&login=ianhines&cache=no'); ?>" title="Share This Article on Twitter">Tweet</a>