2016年3月1日星期二

wordpress 必学函数

add_action();
Hooks a function on to a specific action. 
钩一个指定的动作功能函数;
例:To email some friends whenever an entry is posted on your blog:
当你在你的博客发表一篇文章的时候email告诉你的朋友;
function email_friends( $post_ID )  
{
$friends = 'bob@example.org, susie@example.org';
wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );

return $post_ID;
}
add_action( 'publish_post', 'email_friends' );

register_post_type();
Create or modify a post type. register_post_type should only be invoked through the 'init' action. It won't work at all if called before 'init', and aspects of the new post type will work incorrectly if called later.
新建或修改文章类型,register_post_type函数只能通过 ‘init'被调用; ‘init'之前不会起作用;
示例:
  1. function codex_custom_init() {
  2. $labels = array(
  3. 'name' => 'Books',
  4. 'singular_name' => 'Book',
  5. 'add_new' => 'Add New',
  6. 'add_new_item' => 'Add New Book',
  7. 'edit_item' => 'Edit Book',
  8. 'new_item' => 'New Book',
  9. 'all_items' => 'All Books',
  10. 'view_item' => 'View Book',
  11. 'search_items' => 'Search Books',
  12. 'not_found' => 'No books found',
  13. 'not_found_in_trash' => 'No books found in Trash',
  14. 'parent_item_colon' => '',
  15. 'menu_name' => 'Books'
  16. );
  17. $args = array(
  18. 'labels' => $labels,
  19. 'public' => true,
  20. 'publicly_queryable' => true,
  21. 'show_ui' => true,
  22. 'show_in_menu' => true,
  23. 'query_var' => true,
  24. 'rewrite' => array( 'slug' => 'book' ),
  25. 'capability_type' => 'post',
  26. 'has_archive' => true,
  27. 'hierarchical' => false,
  28. 'menu_position' => null,
  29. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  30. );
  31. register_post_type( 'book', $args );
  32. }
  33. add_action( 'init', 'codex_custom_init' );

register_taxonomy();
This function adds or overwrites a taxonomy.添加或修改分类 ;
Taxonomy is one of those words that most people never hear or use. 
Basically, a taxonomy is a way to group things together.
基本来说,taxonomy就是一个分组的方法;
To register a taxonomy, you use the register_taxonomy() function.
例如:
  1. function people_init() {
  2. // create a new taxonomy
  3. register_taxonomy(
  4. 'people',
  5. 'post',
  6. array(
  7. 'label' => __( 'People' ),
  8. 'rewrite' => array( 'slug' => 'person' ),
  9. 'capabilities' => array(
  10. 'assign_terms' => 'edit_guides',
  11. 'edit_terms' => 'publish_guides'
  12. )
  13. )
  14. );
  15. }
  16. add_action( 'init', 'people_init' );



//自定义字段;
在wordpress中,自定义字段存储在数据库的_postmeta表中
add_post_meta();
update_post_meta();
delete_post_meta();
get_post_meta();



get_terms();
get_term_by();
get_query_var();

wp_reset_query();
query_posts();
WP_Query();





没有评论:

发表评论