返回的值
若文章成功加入数据库,返回文章编号。否则返回0.
与上面对应的是修改了,其实都一样:
function wp_update_post( $postarr = array(), $wp_error = false )
get_post_meta() 查询postmeta数据
三个参数很明显了。文章ID,key,$single是否是单一的,意思是key对应的值是否只有一个。False的时候返回的是数组,true的时候返回字符串。 functionget_post_meta($post_id, $key = '', $single = false) { } return get_metadata('post', $post_id, $key, $single);
add_post_meta() 向postmeta表插入数据
不解释,很明白了。 源代码: Functionadd_post_meta($post_id, $meta_key, $meta_value, $unique = false) update_post_meta修改postmeta表
修改
update_post_meta($post_id, $meta_key, $meta_value, $unique = false)
显示文章的函数
在这个文件中定义了很多的函数:wp-includes/post-template.php
看这个文件的时候进入wp-includes/post.php的get_posts()方法,在执行
the_content等方法的时候需要先执行get_post();
通过这个文件里面的函数可以获取wp_posts表中的需要的字段值,下面不一一列出来了。貌似也没啥意思,找出这个一看就知道了.
本人不太喜欢这一块儿内容,都是自己写的sql查询实现功能的,以后有时间再回来补充吧。这里就没有做详细介绍了。
get_posts查询文章列表
functionget_posts($args = null) { $get_posts = new WP_Query; return$get_posts->query($r); $r['ignore_sticky_posts'] = true; $r['no_found_rows'] = true; $r = wp_parse_args( $args, $defaults ); if ( empty( $r['post_status'] ) ) $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? $defaults = array( ); 'numberposts' => 5, 'offset' => 0, 'category' => 0, 'orderby' =>'post_date', 'order' =>'DESC', 'include' =>array(), 'exclude' =>array(), 'meta_key' =>'', 'meta_value' =>'', 'post_type' =>'post', 'suppress_filters' =>true 源代码: 'inherit' : 'publish'; if ( ! empty($r['numberposts']) &&empty($r['posts_per_page']) ) $r['posts_per_page'] = $r['numberposts']; $r['cat'] = $r['category']; $incposts = wp_parse_id_list( $r['include'] ); $r['posts_per_page'] = count($incposts); // only the number of $r['post__in'] = $incposts; $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); if ( ! empty($r['category']) ) if ( ! empty($r['include']) ) { posts included } elseif ( ! empty($r['exclude']) ) } 对参数$args的说明:
10, //需要提取的文章数 'offset' => 0,//以第几篇文章为起始位置 'category' => ,//分类的ID,多个用逗号将分类编号隔开,或传递编号数组。 'orderby' => 'post_date',//排序规则(注1) 'order' => 'DESC',//升序、降序 'ASC' —— 升序(低到高) 'DESC' —— 降序(高到底) 'include' => ,//要显示文章的ID 'exclude' => ,//要排除文章的ID 'meta_key' => ,//自定义字段名称 'meta_value' => ,//自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。 //post(日志)——默认,page(页面), //attachment(附件),any —— (所有) 'post_type' => 'post', 'post_mime_type' => ,//文章的 mime 类型 'post_parent' => ,//要显示文章的父级 ID 'post_status' => 'publish' );//文章状态 ?>
the_content()获取文章内容
显示文章内容,全部 function the_content( $more_link_text = null, $strip_teaser = false) get_the_content也是获取文章内容
存在more的情况,显示一个查看更多内容 function get_the_content( $more_link_text = null, $strip_teaser = false ) is_sticky() 判断文章置顶
看源代码: functionis_sticky( $post_id = 0 ) { } $post_id = absint( $post_id ); if ( ! $post_id ) $post_id = get_the_ID(); $stickies = get_option( 'sticky_posts' ); if ( ! is_array( $stickies ) ) returnfalse; returntrue; if ( in_array( $post_id, $stickies ) ) returnfalse; 可以看出是通过文章ID来判断当前文章是否是置顶文章。 测试代码: id)) echo\置顶------\; ?> 附加说明: get_option('sticky_posts' )
从这句代码,我在数据库options表中通过key为sticky_posts看到这样的一个value的字符串:a:2:{i:0;i:81;i:1;i:94;}这里面的81与94是我两个置顶的文章。找了半天,终于找到wordpress置顶的标识写哪儿了…就是这个sticky_posts对应的value值了。
侧边栏函数
相关推荐: