wordpress 文章的插入,修改,读取,删除

来源:互联网 发布:服装出口数据 编辑:程序博客网 时间:2024/05/16 01:41


前提:wp-content\themes\twentysixteen

目录只有一个index.php,复制即可

一.获取文章信息

英文官方文档: https://codex.wordpress.org/Template_Tags/get_posts

中文帮助文档:http://javatest.blog.163.com/blog/static/208651064201331962612311/

 

获取标题

<?php

 

$id=2;//这里是文章的ID

 echo $id;

$title =get_post($id)->post_title;

 

echo$title;//输出文章的标题

 

?>

获取文章所有内容

<ul>

<?php

global$post;

$args =array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );

$myposts= get_posts( $args );

foreach ($myposts as $post ) :

  setup_postdata( $post ); ?>

         <li><a href="<?phpthe_permalink(); ?>"><?php the_date(); ?></a></li>

<?phpendforeach;

wp_reset_postdata();?>

</ul>

 

2.创建文章

英文帮助文档:https://developer.wordpress.org/reference/functions/wp_insert_post/

中文帮助文档 : http://www.cnblogs.com/xbdeng/p/5545180.html

 

 

 

 

<?php

 

 

$my_post = array(

 

'post_title' => 'My post',

 

'post_content' => 'This is my post.',

 

'post_status' => 'publish',

 

'post_author' => 1,

 

'post_category' => array(8,39)

 

);

 

 

//入库

 

$a = wp_insert_post( $my_post );

echo $a;

?>

 

 

3.修改文章

英文文档:https://codex.wordpress.org/Function_Reference/wp_update_post

 

<?php

 $my_post = array(

     'ID'           => 2,

     'post_title'   => 'This is thepost title.',

     'post_content' => 'This is the updated content.',

  );

 

// Update the post into the database

 wp_update_post( $my_post );

 

?>

 

 

4.删除文章

英文文档:https://codex.wordpress.org/Function_Reference/wp_delete_post

 

<?php $a = wp_delete_post(6);

?>

 

 

0 0
原创粉丝点击