分类目录归档:Wordpress

WordPress rewrite

Apache

.htaccess


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Nginx


location / {
        try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

 

WordPress 短标签shortcode的使用

短标签更多是处理数据的调用,可灵活的使用在多个地方上面。当有需对某些数据进行调用时,即可考虑使用短标签功能。

形式

[mycode]
[mycode foo=”bar” id=”123″ color=”red” something=”data”] 标签带有属性的
[mycode]Some Content[/mycode] 标签还有内容
[mycode]<p><a href=”http://example.com/”>HTML Content</a<>/p>[/mycode] 标签内容可为html代码
[mycode]Content [another-shotcode] more content[/mycode] 标签嵌套
[mycode foo=”bar” id=”123″]Some Content[/mycode] 标签有属性及内容

标签的形式基本为以上的形式。

应用场景

标签用在以下场景

  • 直接插入到内容中使用   [my_shortcode]
  • 主题直接调用 <?php echo do_shortcode(“[my_shortcode]”); ?>
  • 侧边栏widget中使用。function.php 中增加 add_filter(‘widget_text’, ‘do_shortcode’); 然后添加一个文本widget,内容填入标签即可。
  • 文章的摘要中使用。function.php 中增加 add_filter( ‘the_excerpt’, ‘do_shortcode’);然后在文章摘要中填入标签即可。
  • 评论内容中使用。function.php增加 add_filter( ‘comment_text’, ‘do_shortcode’ ); 然后评论的内容中填入标签即可。

 

示例

实现调用最新文章数据的功能,定义标签为 [latest-posts showposts=”5″]

在function.php 增加


function latest_posts_function($attr,$content=null,$tagname) {
//对标签属性处理,属性只有showposts,且默认值为1,当使用标签时没有属性,则使用默认属性值。
$params = shortcode_atts(array('showposts'=>5),$attr);
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $params['showposts']));
$return_string = '<ul>';
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
add_shortcode('latest-posts', 'latest_posts_function');

标签功能添加完。

在需要使用的地方增加标签。如发布文章时,在内容 填入[latest-posts],这样就完成了对短标签的调用。其他形式的调用参考上面提到的调用方式。