找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 129|回复: 0

[cms教程] Wordpress 短代码(简码)收藏

[复制链接]

该用户从未签到

发表于 2018-5-19 13:33:00 | 显示全部楼层 |阅读模式

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Wordpress 短代码(简码)收藏
我知道wordpress有神奇的自定义函数,这个自定义函数基本上可以代替大部分的插件了;我也知道wordpress有神奇的自定义域,虽然用
起来比较麻烦,但是不能不说它真的很强大;今天我才发现,原来wordpress还有更神奇的短代码(简码)的功能!当然,还是得借助
function.php这个文件来实现。
什么是短代码(简码)?就是你在写日志的时候,在编辑器里随便插入几个英文字符,当出现在文章里的时候,它就是千变万化的其他内容了。你说神奇 不?!
一、超链接用[url]
1. 打开主题中的functions.php文件。粘贴以下函数到其中:

            function myUrl($atts, $content = null) {
extract(shortcode_atts(array(
"href" => 'http://'
)] $atts));
return '[url='$href.'">'.$content.'[/url]';
}
add_shortcode("url", "myUrl");//把函数转化成简码
            
2. 简码创建成功,现在就可在日志和页面上使用了。
[url href=“http://www.wordpress.com”]WordPress recipes[/url]
日志保存后,简码会显示名为“WordPress recipes”的链接,并指向http://www.wordpress.com。
代码注释:若要正常运行,简码必须处理两个参数:$atts 和  
$content。$atts是简码属性,上例中,属性为href,且包括了URL链接。$content是简码内容,位于域名和子目录之间(即
www.example.com和“/subdirectory”之间)。正如以上显示,我们给$atts 和 $content都设置了默认值。
二、创建“发送到 twitter” 的简码

function twitt() {
            return

'';
            }
            add_shortcode
(
'twitter',

'twitt'
);
然后只要在你文章需要的地方插入[twitter]此简码,“发送到Twitter”链接就会只出现放置简码的位置。
三、创建“RSS订阅”简码

function subscribeRss()  {
            return

'Enjoyed
this  post? Subscribe to my RSS feeds!';
            }
            add_shortcode
(
'subscribe',

'subscribeRss'
);
同样用[subscribe]就可以显示了,当然加点css修饰一下就更好了。
四、定制Google AdSense位置

function showads() {
            return

'
            //google adsense code here
            ';
            }
            add_shortcode
(
'adsense',

'showads'
);
使用[adsense]在你需要的位置调用google ad,记得给外包的那个div设置css样式。
五、嵌入RSS阅读器

//This  file is needed to be able to use the wp_rss() function.
            include_once(ABSPATH.WPINC.'/rss.php');
            function readRss($atts) {
            extract(shortcode_atts(array(
            "feed" => 'http://',
            "num" => '1',
            )] $atts));
            return wp_rss($feed, $num);
            }
            add_shortcode
(
'rss',

'readRss'
);
使用简码的时候输入:[rss feed=“http://feed.happyet.org” num=“5”]
feed属性(attribute)即是要嵌入的feed URL,num即是要显示的条目数量。
六、使用简码从WordPress数据库中提取文章

            function sc_liste($atts, $content = null)  {
            extract(shortcode_atts(array(
            "num" =>  '5',
            "cat"  => ''
            )]  $atts));
            global $post;
            $myposts  = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
            $retour='';
            foreach($myposts as $post) :
            setup_postdata($post);
            $retour.='[url='get_permalink().'">'.the_title("","",false).'';
            endforeach;
            $retour.='
';
            return $retour;
            }
            add_shortcode("list", "sc_liste");
            
在WordPress编辑器中使用以下简码:[liste num=“3” cat=“1”],系统将从ID为1的类别中提取3篇文章。
代码注释:系统提取参数并创建全局变量$posts后,sc_liste()函数使用了 get_posts(),numberposts,  
order, orderby和category参数以从类别Y中获取X篇最新日志。完成后,系统就会以无序的HTML列表形式显示日志。
七、获取日志中的最新图像

            function sc_postimage($atts, $content = null) {
            extract(shortcode_atts(array(
            "size" => 'thumbnail',
            "float" => 'none'
            )] $atts));
            $images =& get_children(  'post_type=attachment&post_mime_type=image&post_parent='  . get_the_id() );
            foreach(  $images as $imageID  => $imagePost )
            $fullimage  = wp_get_attachment_image($imageID, $size] false);
            $imagedata = wp_get_attachment_image_src($imageID, $size] false);
            $width = ($imagedata[1,+2);
            $height = ($imagedata[2,+2);
            return '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'';
            }
            add_shortcode("postimage", "sc_postimage");
            
使用代码:[postimage size=“” float=“left”]
代码注释:sc_postimage()函数首先提取了简码属性,然后它使用get_children(),  
wp_get_attachment_image()  
和wp_get_attachment_image_src()这些WordPress函数检索图像。完成后,系统就会返回图像并插入到文章内容中。
八、在侧边栏微件中添加简码
add_filter('widget_text', 'do_shortcode');
好是好,function.php文件得爆炸了……不知道会不会对速度有影响。
回复

使用道具 举报

网站地图|页面地图|文字地图|Archiver|手机版|小黑屋|找资源 |网站地图

GMT+8, 2024-9-30 03:24

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表