開始使用 WordPress 簡碼和示例片段

已發表: 2021-02-18

WordPress 短代碼是在 2.5 中引入的,可幫助您創建宏代碼以在您的內容中使用。 如果您考慮一下,這是在帖子中創建動態廣告位或號召性用語按鈕之類的好方法。

如果我們使用號召性用語示例,您可以在博客文章中添加類似的內容以顯示按鈕,然後在您的模板 functions.php 文件中編輯輸出,我們將在一分鐘內完成。

 [button]

要自定義按鈕,我們可以簡單地添加如下內容:

 [button type="twitter"]

或者為了讓它更好,我們可以使用封閉的短代碼:

 [button type="twitter"]Follow me on Twitter![/button]

有了一些想像力,您很快就會意識到短代碼的潛力以及可以用它們做什麼。 在本文中,我將向您展示如何創建和使用這三種不同類型的簡碼,然後我將展示一些可在您自己的 WordPress 網站上使用的即用型簡碼。

創建一個自動關閉的簡碼

最簡單的簡碼是自動關閉的。 我們將創建一個指向我們的 Twitter 帳戶的簡單鏈接,然後將其添加到博客文章中。

所有代碼都在位於/wp-content/themes/your-theme/functions.php中。 如果您沒有,只需創建它並將代碼放入其中。

 <?php function button_shortcode() { return '<a href="http://twitter.com/filipstefansson" class="twitter-button">Follow me on Twitter!</a>"'; } add_shortcode('button', 'button_shortcode'); ?>

用法:

 [button]

只需使用add_shortcode()函數,我們就可以將任何 PHP 函數鏈接到我們的簡碼。 在這個簡單的示例中,我們所做的只是返回一個指向我們的 Twitter 帳戶的鏈接,但讓我們更進一步並添加一些參數。

使用參數創建一個自動關閉的簡碼

簡碼支持參數,這讓我們可以自定義輸出。 在這個例子中,我們有兩個不同的按鈕,所以我們必須定義我們想要顯示的按鈕。

 <?php function button_shortcode($type) { extract(shortcode_atts(array( 'type' => 'type' ), $type)); // check what type user entered switch ($type) { case 'twitter': return '<a href="http://twitter.com/filipstefansson" class="twitter-button">Follw me on Twitter!</a>'; break; case 'rss': return '<a href="http://example.com/rss" class="rss-button">Subscribe to the feed!</a>' break; } } add_shortcode('button', 'button_shortcode'); ?>

現在,您可以通過在簡碼中定義type來選擇要顯示的按鈕。

 [button type="twitter"] [button type="rss"]

這很棒。 但是如果我們想改變文本呢? 我們可以繼續添加短代碼類型,如[button type="twitter-2"]等等,但這不是很動態,是嗎? 讓我們看看如何以正確的方式做到這一點。

創建封閉的簡碼

封閉的簡碼允許您在簡碼中嵌入內容,就像您曾經使用過的 BBCode 一樣。

 <?php function button_shortcode( $attr, $content = null ) { return '<a href="http://twitter.com/filipstefansson" class="twitter-button">' . $content . '</a>'; } add_shortcode('button', 'button_shortcode'); ?>

要使用此簡碼,您可以像這樣嵌入要使用的文本:

 [button]Follow me on Twitter![/button]

為了使這個按鈕更好,我們可以像前面的例子一樣添加參數。 這次我們添加兩個參數,一個是 Twitter 用戶名,一個是按鈕樣式。 然後我們可以有不同類型的按鈕,並選擇我們想要將按鈕鏈接到的 Twitter 帳戶。

 <?php function button_shortcode( $atts, $content = null ) { extract( shortcode_atts( array( 'account' => 'account', 'style' => 'style' ), $atts ) ); return '<a href="http://twitter.com/' . esc_attr($account) . '" class="twitter-button ' . esc_attr($style) . '">' . $content . '</a>'; } add_shortcode('button', 'button_shortcode'); ?>

用法:

 [button account="filipstefansson" style="simple"]Follow me on Twitter![/button] // Result: &lt;a href="http://twitter.com/filipstefansson" class="twitter-button simple">Follow me on Twitter!&lt;/a>

現在我們有一個可自定義的按鈕,我們可以鏈接到任何 Twitter 帳戶。 我相信你明白,你可以創建比這更高級的簡碼,但這是一個好的開始。

小部件和模板文件中的簡碼

現在,當您看到短代碼的強大功能時,您可能想知道為什麼不能在小部件和模板文件中使用它們。 好吧,事實證明你可以。

要在您的小部件中激活短代碼,只需將以下代碼放入 functions.php 中:

 add_filter('widget_text', 'do_shortcode')

要在模板文件中使用簡碼,您可以通過以下方式訪問它們:

 do_shortcode("[button]");

即用型短路器

這裡有一些很酷的短代碼,您可以立即實施。

帖子中的代碼

如果您經營一個專注於編程的博客,您可能希望在您的帖子中顯示代碼。

 function code_shortcode( $attr, $content = null ) { $content = clean_pre($content); // Clean pre-tags return '<pre"><code>' . str_replace('<', '<', $content) . // Escape < chars '</code></pre>'; } add_shortcode('code', 'code_shortcode');

用法:

 [code]&lt;?php echo 'Hello World!'; ?>

在帖子的任何位置嵌入 Adsense

使用此簡碼,您只需使用[adsense]即可在帖子的任何位置添加 Google 廣告。

 function showads() { return '<script type="text/javascript"><!-- google_ad_client = "pub-3637220125174754"; google_ad_slot = "4668915978"; google_ad_width = 468; google_ad_height = 60; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> '; } add_shortcode('adsense', 'showads');

嵌入 YouTube 視頻

此簡碼可讓您將 YouTube 視頻嵌入到您的博客文章中。

 function youtube($atts) { extract(shortcode_atts(array( "value" => 'http://', "width" => '475', "height" => '350', "name"=> 'movie', "allowFullScreen" => 'true', "allowScriptAccess"=>'always', "controls"=> '1', ), $atts)); return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$value.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"><param name="allowScriptAccess" value="'.$allowScriptAccess.'"><embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></object>'; } add_shortcode("youtube", "youtube");

用法:

 // Optional attributes: width, height, name, allowFullScreen, allowScriptAccess, controls [youtube value="http://www.youtube.com/watch?v=1aBSPn2P9bg"]

貝寶捐贈簡碼

這是一個簡碼,可幫助您創建指向您的 Paypal 帳戶的捐贈鏈接。

 function donate_shortcode( $atts, $content = null) { global $post;extract(shortcode_atts(array( 'account' => 'your-paypal-email-address', 'for' => $post->post_title, 'onHover' => '', ), $atts)); if(empty($content)) { $content='Make A Donation'; } return '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation for '.$for.'" title="'.$onHover.'">'.$content.'</a>'; } add_shortcode('donate', 'donate_shortcode');

用法:

 [donate] [donate]Donate Now[/donate] [donate account="[email protected]" onHover="Thanks" for="Title"] [donate account="[email protected]" onHover="Thanks" for="Title"]Donate Now[/donate]

給作者的私人筆記

最後一個很聰明。 使用此簡碼,您可以在帖子中創建只有作者才能看到的註釋。

 function sc_note( $atts, $content = null ) { if ( current_user_can( 'publish_posts' ) ) return '<div class="note">'.$content.'</div>'; return ''; } add_shortcode( 'note', 'sc_note' );

結論

閱讀本文後,我希望您和我一樣喜歡 WordPress 短代碼,並希望您開始在自己的博客中實現它們。