程序随笔 – C/C++ & PHP

2010年03月13日

wordpress二次开发常用函数

类归于: wordpress — admin @ 10:46 上午

      这里只列出最常用的几个。先是重要的bloginfo(),不直接echo数值的函数为get_bloginfo();恩,不少函数都是按照这个规则写的,PS:并不是全部!echo出函数值的函数为xxx()的话,那么不echo出结果值的的函数为get_xxx()。
      该函数范围的是WordPress的配置参数,主要参数和值举例如下,常用的黑体标出:
     admin_email = admin@example
     atom_url = http://example/home/feed/atom
     charset = UTF-8
     comments_atom_url = http://example/home/comments/feed/atom
     comments_rss2_url = http://example/home/comments/feed
     description = Just another WordPress blog
     home = http://example/home这个是网站首页地址
     html_type = text/html
     language = en-US
     name = Testpilot网站名称
     pingback_url = http://example/home/wp/xmlrpc.php
     rdf_url = http://example/home/feed/rdf
     rss2_url = http://example/home/feed
     rss_url = http://example/home/feed/rss
     siteurl = http://example/home也是网站地址
     stylesheet_directory = http://example/home/wp/wp-content/themes/largo
     stylesheet_url = http://example/home/wp/wp-content/themes/largo/style.css主题文件夹下的style.css地址
     template_directory = http://example/home/wp/wp-content/themes/largo主题包地址
     template_url = http://example/home/wp/wp-content/themes/largo
     text_direction = ltr
     url = http://example/home
     version = 2.7
     wpurl = http://example/home/wp
     
     举例,bloginfo(“wpurl “)会输出“http://example/home/wp”。

      下边是主题中的几个常用函数。

<?php get_header();//调用header.php ?>
<?php get_sidebar();//调用sidebar.php ?>
<?php get_footer();//调用footer.php ?>
//下边就比较多了,先来分类的。
<?php wp_list_categories( $args ); ?>
<?php $args = array(
  'show_option_all'    => ,
  'orderby'            => 'name',
  'order'              => 'ASC',
  'show_last_update'   => 0,
  'style'              => 'list',
  'show_count'         => 0,
  'hide_empty'         => 1,
  'use_desc_for_title' => 1,
  'child_of'           => 0,
  'feed'               => ,
  'feed_type'          => ,
  'feed_image'         => ,
  'exclude'            => ,
  'exclude_tree'       => ,
  'include'            => ,
  'current_category'   => 0,
  'hierarchical'       => true,
  'title_li'           => __( 'Categories' ),
  'number'             => NULL,
  'echo'               => 1,
  'depth'              => 0
);
?>
//我估计这个是参数最复杂的一个函数了,改函数的用途自然是输出分类,对了,WordPress最多支持4级分类。
//另外经常用到的就是get_the_category(),例:
<?php
foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';
}
?>
可以在循环读取分类后,输出每个分类的名称,该项全部变量为:
cat_ID
    the category id (also stored as 'term_id') 分类ID
cat_name
    the category name (also stored as 'name') 分类名称
category_nicename
    a slug generated from the category name (also stored as 'slug') slug名称
category_description
    the category description (also stored as 'description') 分类描述
category_parent
    the category id of the current category's parent. '0' for no parents. (also stored as 'parent') 父分类id
category_count
    the number of uses of this category (also stored as 'count') 分类包含的日志数。
//接下去是判断分类的,通常用在sidebar和single里用来判断当前内容的分类信息。
<?php in_category( $category, $_post ) ?>
//例子:
<?php
if (in_category('pachoderms')){
    //分类pachoderms
}elseif(in_category(array('Tropical Birds','small-mammals'))){
    //分类Tropical Birds和small-mammals
}elseif(in_category(array('1','5'))){
    //分类1,5
}else{
    //& c.
}
?>
//page页的,Displays a list of pages in a select (i.e dropdown) box with no submit button. 输入全部page的下拉选择框。
<?php wp_dropdown_pages( $args ); ?>
//$args默认设置,恩,只介绍常用的。
<?php $args = array(
    'depth'            => 0,
    'child_of'         => 0,//父类page的ID
    'selected'         => 0,
    'echo'             => 1,
    'name'             => 'page_id',//page_id
    'show_option_none' => //是否列出空项
    'exclude'          => //不包含的page_id
    'exclude_tree'     => //不包含某个page_id为顶点的page树
);
?>
//Displays a list of WordPress Pages as links.直接输出page页,当然是带链接的。
<?php wp_list_pages( $args ); ?>
<?php $args = array(
    'depth'        => 0,
    'show_date'    => ,
    'date_format'  => get_option('date_format'),
    'child_of'     => 0,
    'exclude'      => ,
    'title_li'     => __('Pages'),//列表前的标题,不要就留空。
    'echo'         => 1,
    'authors'      => ,//作者
    'sort_column'  => 'menu_order, post_title',
    'link_before'  => ,//内容输出前的内容
    'link_after'   => ,//内容输出后的内容,这2个通常用来做custom化。
    'exclude_tree' =>
);
?>
//友情链接部分
<?php wp_list_bookmarks( $args ); ?>
<?php $args = array(
    'orderby'          => 'name',
    'order'            => 'ASC',
    'limit'            => -1,
    'category'         => ,
    'category_name'    => ,
    'hide_invisible'   => 1,
    'show_updated'     => 0,
    'echo'             => 1,
    'categorize'       => 1,
    'title_li'         => __('Bookmarks'),
    'title_before'     => '<h2>',
    'title_after'      => '</h2>',
    'category_orderby' => 'name',
    'category_order'   => 'ASC',
    'class'            => 'linkcat',
    'category_before'  => '<li id=\"%id\" class=\"%class\">',
    'category_after'   => '</li>'
);
//相对来说比较复杂的函数,不过基本不传任何参数,使用默认的就行。
//不怎么用的的wp_list_comments()回复列表函数,参数也不介绍了~
<?php wp_list_comments();?>

接着嘛,是万用数据库查询函数,记住不是query_posts()而是WP_Query()哦!这个函数很强大,具体参照《WordPress-WP_Query》,里边也有讲为何不建议使用query_posts()的原因。

//最后是设计到具体内容后的函数部分,这部分通常是在循环体内,比如:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//code here~
<?php endif; ?>
也有带循环的:
<?php while (have_posts()) : the_post(); ?>
//code here~
<?php endwhile; ?>
the_ID()
the_title() 标题,常用
the_title_attribute() (Version 2.3)
single_post_title()
the_title_rss()
the_content() 内容,常用
the_content_rss()
the_excerpt() 日志中有more标签时输出more前的内容,参数是查看全部内容的链接名,重要
the_excerpt_rss()
next_post_link() 后一个日志的链接,重要
previous_post_link() 前一个日志的链接,重要
next_posts_link() 后xx个日志,基本上用不到这个,而用WP-PageNavi插件实现
previous_posts_link() 同上
posts_nav_link()
sticky_class() (Version 2.7)
the_meta()
the_tags() (Version 2.3) 日志tag,重要
the_time() 时间
the_date() 日期
the_permalink() 固定链接
wp_loginout() 登出
wp_register() 注册用户

评论暂缺 »

还没有任何评论。

这篇文章上的评论 RSS feed TrackBack URL

留下评论

WordPress 所驱动