2014-10-26 110 views
0

我想在下面的javascript代碼中生成wordpress帖子標題:我將此代碼置於WordPress內容的內容中。如何在Wordpress中顯示帖子標題?

<script charset="utf-8" type="text/javascript"> 
document.write('\x3Cscript type="text/javascript" charset="utf-8" src="http://adn.ebay.com/cb?programId=1&campId=5337203820&toolId=10026&customId=posttop&keyword=**POST-TITLE**&width=728&height=90&font=1&textColor=000000&linkColor=0000AA&arrowColor=8BBC01&color1=709AEE&color2=[COLORTWO]&format=ImageLink&contentType=TEXT_AND_IMAGE&enableSearch=y&usePopularSearches=n&freeShipping=n&topRatedSeller=y&itemsWithPayPal=n&descriptionSearch=n&showKwCatLink=n&excludeCatId=&excludeKeyword=&catId=177913%2C179767%2C1059&disWithin=200&ctx=n&autoscroll=n&flashEnabled=' + isFlashEnabled + '&pageTitle=' + _epn__pageTitle + '&cachebuster=' + (Math.floor(Math.random() * 10000000)) + '">\x3C/script>'); 

關鍵字=腳本部分是我需要生成wordpress的文章標題。我試圖keyword="<?php single_post_title(); ?>",但它不會生成WordPress的帖子標題...不知道我是否在正確的軌道上。

任何幫助,將不勝感激。請記住我是一個新手。如果我忘記提及某事,請提問。我會很快回復。

非常感謝!

+0

顯示PHP代碼!呈現的HTML根本沒有幫助。你最好在WordPress的StackExchange網站上發佈你的問題:http://wordpress.stackexchange.com – 2014-10-27 00:02:33

+0

顯示PHP代碼?我在那邊問過它... – 2014-10-27 00:30:38

+0

WordPress是PHP。所以我想看看你編寫的代碼來創建OP中的HTML。你只能在PHP文件中使用'<?php/* ... * /?>',所以我認爲有更多的代碼。 – 2014-10-27 00:33:06

回答

1

你的循環內部請參閱if (have_posts())while,使用這裏面的while聲明。

$title= get_the_title(); 

現在變量已設置,您可以稍後使用它,除非它被覆蓋。

在您的JS:

keyword="<?php echo $title; ?>" 
+0

這很好,但我碰到一個輕微的問題。我需要標題中的空格是+(加號),沒有空格。你能幫助我嗎?因此,結果會看起來像keyword =「Capture + the Moment」,而不是關鍵字=「捕捉時刻」。也許過濾器刪除空間,並用+符號替換?非常感謝。這有助於很多! – 2014-10-27 05:17:25

+0

'$ title = str_replace('','+',get_the_title());' – David 2014-10-27 10:54:57

+0

非常感謝您的幫助。你完美地解釋了一切,所以像我這樣的新手可以理解。真的謝謝你! – 2014-10-27 18:48:02

1
//loop start 

foreach($allnews as $news): 
$title=get_the_title($news->ID); 

//following code print your post title 
echo $news->post_title; 
//also you can written 
keyword="<?php echo $title; ?>" 

endforeach; 
//end loop 
+0

謝謝你試圖幫忙。我沒有機會測試你的代碼,因爲第一個答案很完美。謝謝。 – 2014-10-27 18:48:58

0

有一個簡單的方法來做到這一點的博客文章和正常的網頁。這裏是:

<?php the_title(); ?> 
相關問題