1. wordpress的get
wordpress可以设置自定义字段,方便扩展功能,wordpress利用巧妙的数据库表设计达到这一目的,posts表存放文章,页面和附件等,与之对应的postmeta表用来存储自定义的字段,采用post_id,key,value这样的设计来存放自定义字段的值。
get_post_meta函数用法:
get_post_meta($post_id, $key, $single);
该函数有3个基本参数:
$post_id —— 所检索数据的文章的ID,使用 $post->ID 来获取文章的ID。
$key —— 要检索的自定义字段名称
$single —— 这是一个布尔值,如果设置为 true ,将直接以字符串的形式返回字段的值;一个自定义字段可以填写多个值,如果设置为 false,将返回一个数组 array 来显示这多个值。
此函数定义在wordpress的post.php中:
2. wordpress怎么在后台自定义设置
add_post_meta 函数是 WordPress 中用来给文章或页面添加自定义字段值的一个函数, 其用法与在编写文章时在文章编写界面中利用自定义栏目面板为文章添加自定义字段值的效果是一样的。
add_post_meta函数描述 为文章添加自定义字段。 常见的使用有:文章浏览次数、喜欢按钮、seo插件等常用插件就是使用的自定义字段功能。
参数详解 add_post_meta($post_id, $meta_key, $meta_value,$unique); $post_id 要添加自定义字段的文章或页面的ID值 $meta_key 自定义字段的键值(名字) $meta_value 自定义字段的值 $unique 如果已经有相同名字的自定义字段,是否重复添加重名的自定义字段,true为不允许,false为允许 函数使用实例 //为ID为1的文章添加_postviews自定义字段,值为99 add_post_meta(1, "_postviews", "99"); var_dump(get_post_meta(1));echo""; //为ID为1的文章添加_postviews自定义字段,值为999,并允许重复自定义字段名称 add_post_meta(1, "_postviews", 999,false); var_dump(get_post_meta(1));echo""; 演示效果: array(1) { ["_postviews"]=> array(1) { [0]=> string(2) "99" } } array(1) { ["_postviews"]=> array(2) { [0]=> string(2) "99" [1]=> string(3) "999" } } //不允许重复自定义字段的代码 add_post_meta(1, "_postviews", "996",true); var_dump(get_post_meta(1));echo""; add_post_meta(1, "_postviews", "997",true); var_dump(get_post_meta(1));echo""; array(1) { ["_postviews"]=> array(1) { [0]=> string(3) "996" } } array(1) { ["_postviews"]=> array(1) { [0]=> string(3) "996" } } add_meta_box add_meta_box 是 WordPress 进阶使用的一个函数,能用到这个函数那说明你已经比一个普通的博主更了解这个世界瞩目的博客程序了,至少你在它身上已经花了不少功夫了。能用到它,说明你现在正在折腾一个你自己的主题、插件,甚至是在折腾 WordPress 后台了。
好像已经赘述的够多了,下面我们以一个进阶的角度去说明一下这个函数怎么用。 add_meta_box 函数说明 add_meta_box 函数是被用来在文章编辑等页面添加一个设置的区域的函数。
参数说明 $id HTML 代码中设置区域中id属性的值 $title 区域中的标题名称 $callback 添加的设置区域的显示函数(回调函数) $post_type 在 post 还是 page 的编辑页面中显示 $context 设置区域的显示位置,主编辑区、边栏、其他 $priority 设置区域显示的优先级 $callback_args 回调函数接受的附加参数 使用实例 function add_xz_box (){//添加设置区域的函数 add_meta_box('xz_box_1', 'add_meta_box 测试', 'xz_box_1','post','side','high',array('str1','str2')); }; //在'add_meta_boxes'挂载 add_xz_box 函数 add_action('add_meta_boxes','add_xz_box'); function xz_box_1($post,$boxargs){//显示设置区域的回调函数 echo"add_meta_box 测试"; };。
3. wordpress首页如何调用最新文章
第一种方法
WordPress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:
(显示10篇最新更新文章)
或
后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)
补充: 通过WP的query_posts()函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。
第二种方法
调用最新文章:(直接在想要呈现的位置放上以下代码即可)
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit=7 . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
ID)) : ?>
第三种方法调用最新文章:
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
第四种方法调用最新文章:
get_results(“SELECT ID,post_title FROM $wpdb->posts where post_status='publish' and post_type='post' ORDER BY ID DESC LIMIT 0 , 10″);
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
?>
第四种方法是自己写的,用get_results()函数调用比较快,官网的很多方法都是基于get_results()函数实现的.
以上代码亲测可以使用,如果有问题联系我。
转载请注明:wordpress调用最新文章的四种方法无插件哦 - 前端开发
4. 有人帮忙写个wordpress的post吗
/* Define the custom box,适用WP 3.0以后的版本 */add_action( 'add_meta_boxes', 'ludou_add_custom_box' );// 如果是WP 3.0之前的版本,使用以下一行代码// add_action( 'admin_init', 'ludou_add_custom_box', 1 );/* Do something with the data entered */add_action( 'save_post', 'ludou_save_postdata' );/* Adds a box to the main column on the Post and Page edit screens */function ludou_add_custom_box() { add_meta_box( 'ludou_sectionid', 'SEO', // 可自行修改标题文字 'ludou_inner_custom_box', 'post' );}/* Prints the box content */function ludou_inner_custom_box( $post ) { global $wpdb; // Use nonce for verification wp_nonce_field( plugin_basename( __FILE__ ), 'ludou_noncename' ); // 获取固定字段keywords和description的值,用于显示之前保存的值 // 此处wp_posts新添加的字段为keywords和description,多个用半角逗号隔开 $date = $wpdb->get_row( $wpdb->prepare( "SELECT keywords, description FROM $wpdb->posts WHERE ID = %d", $post->ID) ); // Keywords 字段输入框的HTML代码 echo ' '; echo ''; // description 字段输入框的HTML代码,即复制以上两行代码,并将keywords该成description echo ' '; echo ''; // 多个字段依此类推}/* 文章提交更新后,保存固定字段的值 */function ludou_save_postdata( $post_id ) { // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST['ludou_noncename'], plugin_basename( __FILE__ ) ) ) return; // 权限验证 if ( 'post' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_post', $post_id ) ) return; } // 获取编写文章时填写的固定字段的值,多个字段依此类推 $keywords = $_POST['keywords_new_field']; $description = $_POST['description_new_field']; // 更新数据库,此处wp_posts新添加的字段为keywords和description,多个根据你的情况修改 global $wpdb; $wpdb->update( "$wpdb->posts", // 以下一行代码,多个字段的话参照下面的写法,单引号中是字段名,右边是变量值。
半角逗号隔开 array( 'keywords' => $keywords, 'description' => $description ), array( 'ID' => $post_id ), // 添加了多少个新字段就写多少个%s,半角逗号隔开 array( '%s', '%s' ), array( '%d' ) );}。
5. wordpress获取指定形式的文章
先确定查询 参数:
$args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' )
)
)
);再去查询:
$asides = get_posts( $args );
if ( count($asides) ) {
foreach ( $asides as $aside ) {
// 在这里循环输出
}
}其它文章格式的查询参照上述示例代码。
当然了,你也可以其它查询函数,但是参数却类似上面列出的。
不了解再问。
祝愉快!
6. 【大牛速来】【wordpress】怎样获取文章页首段文字为摘要,或者获
if ( have_posts() ):
while(have_posts()) : the_post();
global $post;
preg_match_all( '|<p>([^<]+)</p>|', wpautop( $post->post_content ), $matches );
// var_dump($matches);
echo $matches[0][0];
endwhile;
endif;根据$matches输出的内容,直接echo $matches[0][0];就是获取第一个P标签的内容
转载请注明出处51数据库 » wordpressgetpost
来斤单反尝尝