Products
GG网络技术分享 2025-03-18 16:07 2
上周我介绍了有效利用简码的三个技巧。 如果你不能使用它,那么拥有所有这些知识又有什么意义呢? 现在是时候开始使用一些可以在 WordPress 博客上使用的简码了。
您的专业水平(或缺乏)无关紧要 - 任何知道如何复制和粘贴的人都可以成功实施简码!
下面的每一个短代码都已经过测试,并且可以与 WordPress 一起使用:
别担心——我并不是要建议一个真正有用的短代码是一个允许你显示“Hello World!”的短代码。 但是包含了这个超级简单的短代码,因为它展示了您只需一点 HTML 知识即可完成的工作。
潜在的应用程序是无穷无尽的——只要您希望在多个页面上显示某些内容(例如社交共享按钮、时事通讯注册表单或简单的号召性用语),您就可以使用这样的简码。 无需每次都输入相同的代码,只需引用短代码即可。
function HelloWorldShortcode() {
return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');
如您所见,更改要显示的消息非常简单,您可以包含完整的 HTML 和 CSS。
每当您想包含存储在短代码中的内容时,只需将其输入到您的 WordPress 编辑器中:
[helloworld]
来源:https://www.sitepoint.com/wordpress-shortcodes-tutorial/
能够显示您最近的帖子是可以在众多应用程序中使用的“库存”功能之一。 例如,我最近为一家企业建立了一个网站,该企业希望他们最近的博客文章显示在他们网站的页脚中。 我使用这个简码来实现它。
function wptuts_recentpost($atts, $content=null){
$getpost = get_posts( array('number' => 1) );
$getpost = $getpost[0];
$return = $getpost->post_title . "<br />" . $getpost->post_excerpt . "…";
$return .= "<br /><a href="https://wpmudev.com/blog/10-awesome-shortcodes-for-your-wordpress-blog/" . get_permalink($getpost->ID) . ""><em>read more →</em></a>";
return $return;
}
add_shortcode('newestpost', 'wptuts_recentpost');
毫无疑问,您可以看到此短代码包括帖子标题、摘录和“阅读更多”链接作为标准。 每当您想显示最新帖子时,只需输入此简码:
[newestpost]
来源:https://code.tutsplus.com/articles/getting-started-with-wordpress-shortcodes–wp-21197
在每个博客文章页面上都有一个相关的文章部分是提高用户参与度的好方法。 如果有人喜欢您阅读的内容,他们可能会愿意阅读更多内容,但您需要将其放在他们面前。
虽然有很多相关的帖子插件,但这也可以完成这项工作:
function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
'limit' => '5',
), $atts));
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval="
<ul>";
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(',', $tagsarray);
// Do the query
$q = "
SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
WHERE tt.taxonomy ='post_tag'
AND tt.term_taxonomy_id = tr.term_taxonomy_id
AND tr.object_id = p.ID
AND tt.term_id IN ($tagslist)
AND p.ID != $post->ID
AND p.post_status="publish"
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";
$related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
';
}
} else {
$retval .= '
<li>No related posts found</li>
';
}
$retval .= '</ul>
';
return $retval;
}
return;
}
add_shortcode('related_posts', 'related_posts_shortcode');
这个短代码将在一个简单的列表中显示帖子,但是通过一些 CSS 技巧,你可以把它变成更有趣的东西。
帖子是否相关取决于您对帖子标签的使用。 每当您想显示相关帖子时,只需输入以下内容:
[related_posts]
来源:http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/
这是插件中提供的另一个功能,但可以使用简单的短代码轻松复制。
在您的网站上显示 AdSense 可能会很痛苦——您当然不想在每个帖子页面上手动插入广告。 因此,请改用此短代码:
function showads() {
return '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%3C!--%0Agoogle_ad_client%20%3D%20%22xxx%22%3B%0Agoogle_ad_slot%20%3D%20%22xxx%22%3B%0Agoogle_ad_width%20%3D%20xxx%3B%0Agoogle_ad_height%20%3D%20xxx%3B%0A%2F%2F--%3E%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%0Asrc%3D%22http%3A%2F%2Fpagead2.googlesyndication.com%2Fpagead%2Fshow_ads.js%22%3E%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
';
}
add_shortcode('adsense', 'showads');
显然,您需要将自己的 AdSense 帐户和广告详细信息放入相关位置,但是一旦您这样做了,只需在您希望广告出现的位置输入:
[adsense]
资料来源:http://www.wprecipes.com/how-to-embed-adsense-anywhere-on-your-posts
如果您有一个会员网站,或者只是想向在您的网站上注册为用户的人显示特定内容,这个简单的短代码将允许您定制显示给谁的内容。
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
这实际上是两个短代码合二为一。 第一个将显示内容给那些 不是 注册,第二个将显示内容给那些谁 是 挂号的。 只需将这些短代码放在适当的位置:
[visitor]Displayed to visitors[/visitors]
[member]Displayed to members[/member]
资料来源:http://justintadlock.com/archives/2009/05/09/using-shortcodes-to-show-members-only-content
您可以使用此短代码显示您喜欢的任何博客的 RSS 提要。
include_once(ABSPATH.WPINC.'/rss.php');
function cwc_readRss($atts) {
extract(shortcode_atts(array(
"feed" => 'http://',
"num" => '1',
), $atts));
return wp_rss($feed, $num);
}
您可能会注意到,我们需要定义几个变量才能使此短代码起作用。 只需在您希望 Feed 出现的位置输入以下内容:
[rss feed="feed-url" num="number-of-posts-to-display"]
资料来源:http://www.wprecipes.com/how-to-display-any-rss-feed-on-your-wordpress-blog
对于任何因 WordPress 的自动格式化而陷入垃圾(这是技术术语)的人来说,这很可能是您祈祷的答案:
function my_formatter($content) {
$new_content="";
$pattern_full="{([raw].*?[/raw])}is";
$pattern_contents="{[raw](.*?)[/raw]}is";
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
只需将您想要保持不变的文本包含在以下短代码中,您就可以了:
[raw]This is gonna be raw, yo'[/raw]
来源:https://www.ideasandpixels.com/articles/disable-wordpress-auto-formatting-short-code
您通常会从您的站点链接到 PDF,但如果您可以将它们包含在您的页面内容中或发布本身,那不是很好吗? 这样用户就不必离开您的网站来阅读它。
function viewpdf($attr, $url) {
return '<iframe src="http://docs.google.com/viewer?url=" . $url . "&embedded=true" style="width:' .$attr['width']. '; height:' .$attr['height']. ';" frameborder="0">Your browser should support iFrame to view this PDF document</iframe>';
}
add_shortcode('embedpdf', 'viewpdf');
除了尺寸之外,您还需要定义 PDF 的来源:
[embedpdf width="xxxpx" height="xxxpx"]http://www.yoursite.com/your.pdf[/embedpdf]
获得更多 RSS 订阅者是增加对您的博客的重复访问的好方法,而增加上述订阅者的一种方法是仅通过您的 RSS 提要内容提供独家内容(甚至可能是独家竞争)。
使用此简码,您可以轻松显示仅 RSS 提要的内容:
function feedonly_shortcode( $atts, $content = null) {
if (!is_feed()) return "";
return $content;
}
add_shortcode('feedonly', 'feedonly_shortcode');
当您只想为您的 RSS 阅读器包含文本时,只需使用以下短代码:
[feedonly]Only my most loyal readers can see this text.[/feedonly]
资料来源:https://konstantin.blog/2011/snippet-a-feed-only-shortcode-for-wordpress/
你们中的鹰眼可能已经发现我只包含了 9 个短代码。 如果我在网上搜索短代码时发现了一件事,那就是那里不乏创意。
这就是为什么我现在求助于你,谦虚的 WPMU 读者,你是所有 WordPress 方面的专家。
知识共享 照片由 pvera 提供
标签:Demand feedback