|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ' q0 F! j/ ?+ r* N6 C+ E, y% J7 a
" [# g( _ x9 G" N直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持8 x f9 b6 e8 G2 Z$ @( `, p2 F
# M/ {8 e! t9 e我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了* ?& v' V& j6 W4 S$ S+ [( u; |
" L' k! D, K3 ?方便实用 简单 何乐而不用呢
+ K0 B" Q0 q8 b' M1 t3 ~) R- <?php
) O1 [# }* e8 v3 B g) i) ?+ {
8 C1 \8 e2 j/ v, a! W6 S8 o6 @- define( 'ABSPATH', dirname(__FILE__) . '/' );
' J' ^: ]/ F6 ~4 } - require_once( ABSPATH . 'wp-config.php' );3 u( e2 C# |5 q( p [
- require_once( ABSPATH . 'wp-settings.php' );6 _" [4 F. Z3 k9 G+ ?" |
- require_once( ABSPATH . 'wp-includes/class-wp.php' );5 q% |4 ~+ V5 b: T) R+ }% \- Y
- require_once( ABSPATH . 'wp-includes/functions.php' );: U. \, {& h1 s% P) D, F h
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% _+ k3 ]3 B! E& B4 x - $title = $_POST["title"];
8 c* r4 `& `6 q) P2 x# S5 d6 m - $content = $_POST["content"];) Y9 l6 q& D, ] @! x8 f
- $tags = explode("_",$_POST["tags"]);5 t5 V3 Z* X) z
- $cate = $_POST["cate"];
; s# M$ D; ?0 c& t - //print_r $tags;/ {+ n0 f' Z. ~, U4 l" [. g- P
- //
* I* T+ n% \+ w$ w. B - //4 i0 H5 i: B$ s
- $wp = new WP();8 H9 |# v: Q1 n0 q( k
- $wp->main();4 J1 F2 n( |4 U4 t% A. y7 y
- //
/ N$ n* G; Z! N4 i2 y' a" b - $my_post = array();( s7 e( [( p4 a. s s) J- ~
- $my_post['post_title'] = $title;
h0 ^8 ~- p+ G - $my_post['post_content'] = $content;
( d6 P4 K2 ]+ Q# y - $my_post['post_status'] = 'publish';& S6 K& W) ?- Y2 k
- $my_post['post_author'] = 1;
7 T% T6 i, x# R# n - $my_post['post_category'] = array($cate);& B+ R' Z) R( B% O) V. p
- $my_post['tags_input'] = $tags;+ J8 [' r% {) t& u X# Y3 m9 _
- //$my_post['tags_input'] = array('tag1', 'tag2');
* f1 x: a5 ?6 g& N4 z8 f" k7 Y - //$my_post['post_status'] = 'future';
6 g& ]6 p+ X- a- r. N. L* }- Q1 O - ////$my_post['post_date'] = '2010-07-04 16:20:03';3 n$ a. Q7 a+ |) _: |# m2 ~
- 1 h) J/ \4 a& h
- // Insert the post into the database# t, n- W$ A! l* F. T
- $ret = wp_insert_post( $my_post );5 D6 V) u" ]. N8 j) v; T3 X+ P7 c# J
- echo $ret;' m1 M- {+ x# {7 ~
- ?>
- T6 R: J5 b; b7 z0 e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 5 n7 O; C. E! H, B
- <?php
7 t/ n0 r6 N: O6 Z( w - define( 'ABSPATH', dirname(__FILE__) . '/' );6 {( O0 w T9 W5 u
- require_once( ABSPATH . 'wp-config.php' );
& q1 y4 f; I1 ], W: M/ f1 a; i n4 p - require_once( ABSPATH . 'wp-settings.php' );7 {, o( Y, Z2 t4 S9 ]5 M
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 u K, G4 K2 ^3 ~6 p( a/ D7 P+ A - require_once( ABSPATH . 'wp-includes/functions.php' );+ r. r. `3 D7 W. j( h
- require_once( ABSPATH . 'wp-includes/plugin.php' );$ x7 _) |9 x, k% \7 \/ n0 z
- $pic_name = $_POST["pic_name"];
5 Y2 z5 ~- U1 N: M- D0 z7 e - $pic = $_POST["pic"];9 m3 b0 X4 C- A, ~; Q. v6 ?
- if($pic != "" && $pic_name != "")
8 ?3 r1 _% A% m4 J; Z/ R2 E% d- m - {1 R9 m% o0 u5 p1 O
- $path = "images/";
' @1 O- b; S7 n- w: h. h - $pic_name = $path.$pic_name;
+ F' n. P! Y- t; E H - echo $pic_name;
( z2 n3 ^6 _9 b: M, N - echo "||";. \2 B g1 ?4 H. ?2 N0 J2 Z
- error_reporting(0);' F- A- o5 l/ M; k9 N
- mkdir("images");
" M/ j0 J$ X4 h2 q - $handle = fopen($pic_name, 'w');- Z5 _( ?" U9 m/ O; ^
- echo fwrite($handle,file_get_contents($pic));4 l* y6 _; t' R k+ ^! a, F
- }
. J- o6 T' N% C
( a2 I! k5 k! [/ v- $title = $_POST["title"];
6 @8 {& b N4 h) _8 ? - $content = $_POST["content"];
/ o, @2 n& ?2 }9 {. R - $tags = explode("_",$_POST["tags"]);
2 [5 S. l) h6 y: E2 L+ [ - $cate = $_POST["cate"];
V+ Q+ i$ @" s, R: e - //print_r $tags;$ A9 L2 T6 u: ]$ G
- //% P/ o) I& [! O& b2 Y/ k' ?
- //8 j; ]0 m0 }! h+ i; R7 e/ t1 N
- $wp = new WP();* D' P7 T ~/ x
- $wp->main();
T3 L k, Q$ R; H5 Q# T& { - //6 I M' O: C9 U2 U" y* u9 k
- $my_post = array();; Z* h6 {6 K. I2 U7 e U
- $my_post['post_title'] = $title;
0 D" E( {1 r, o% Y5 ? - $my_post['post_content'] = $content;3 }# a! R5 B( `% c0 x
- $my_post['post_status'] = 'publish';) H* Q2 Y9 F3 `
- $my_post['post_author'] = 1;9 b& E i2 G3 w- u& o4 Q
- $my_post['post_category'] = array($cate);7 K4 X# u- z8 j3 R$ S X+ W
- $my_post['tags_input'] = $tags;- V) C7 @, I* Z5 c; L) [
- //$my_post['tags_input'] = array('tag1', 'tag2');$ n# q: o' R' r& Y% P
- //$my_post['post_status'] = 'future';7 u; \! Z* _+ V8 t
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 H0 j5 `, \. h
- , n8 {5 a& u ?0 f
- // Insert the post into the database8 T9 Z( g! @$ c3 ^ D* u# ~
- $ret = wp_insert_post( $my_post );
4 O) p, g+ [) x. j$ K( Q - echo $ret;
7 z* O+ ^7 P5 } q - ?>5 c0 H0 J' I" h Z) s- G
复制代码 我自己写的
* v- Q6 R% T0 J9 j7 c, [1 ?2 t# h( y插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|