|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
/ r9 u% N9 _, }9 f2 H/ V: s' L9 o# x ^* ] f; _" J: `' D2 p
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. m5 m: |: [# d2 C x1 q# Z* h
, s( w N7 y5 P% x
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 {8 C/ c: ]0 F4 q% S; D; x0 h
* k+ ?6 Y. l( [, i方便实用 简单 何乐而不用呢
- |% j1 V' f- n) A; V& U7 I+ \- <?php
0 r" d& r) S/ j1 U0 l- r5 Y
- ]) {" j3 D- D1 g; ]* K- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ n! D* o9 @. f. v; V - require_once( ABSPATH . 'wp-config.php' );, |3 |% r+ U, P) N# w
- require_once( ABSPATH . 'wp-settings.php' );8 |# W% i0 O* X" F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );3 @$ c' q M" L, l
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ x1 Z/ F4 j% Z; S c - require_once( ABSPATH . 'wp-includes/plugin.php' );
" e7 T# z- E3 q+ @# B- A# N/ d - $title = $_POST["title"];
/ H- } J. P; o" y# q3 s - $content = $_POST["content"];
; F0 H3 H8 s. u/ N0 C/ C - $tags = explode("_",$_POST["tags"]);/ o0 n+ L9 Y/ d% `6 L& i! R
- $cate = $_POST["cate"];/ j& z! o* U6 D7 o) t' m2 x. V, v3 M
- //print_r $tags;; g+ n2 _; t7 J9 B. s& q& r
- //
; z8 [& @" O, K5 B, a# K+ G$ [ - //
n: A M2 B: j% S" r3 H - $wp = new WP();) d6 d( a1 I" c6 B7 f" _ z
- $wp->main();
; M0 I. ?( v: T7 a7 m - //: P: g0 i7 J9 s$ }2 v
- $my_post = array();
- Z, i- W+ x$ n' j7 t - $my_post['post_title'] = $title;0 B* m4 \- I# {) C1 i
- $my_post['post_content'] = $content;
" F9 w, i* X) V - $my_post['post_status'] = 'publish';
# [1 y2 q$ p* q8 e - $my_post['post_author'] = 1;
0 s1 c# F3 L! z T% J - $my_post['post_category'] = array($cate);' O1 R0 U. S8 u+ V
- $my_post['tags_input'] = $tags;
" n, U( H' p% p% m6 C% K - //$my_post['tags_input'] = array('tag1', 'tag2');
* u1 Y* s3 Q7 q5 `2 o7 z+ S7 B - //$my_post['post_status'] = 'future';& q9 r* p& V9 {" r/ n
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
. S$ _$ f1 y; B5 W' F; a5 D
7 }2 ^% L/ n. F4 L6 G3 a- // Insert the post into the database
2 q* b) v" ^ B* f* o - $ret = wp_insert_post( $my_post );, E I! g/ _3 i
- echo $ret;6 Z1 l! E/ h1 S# G
- ?>, u; @4 W7 @$ p+ P9 s( l; x/ L' W
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
4 I8 H7 B1 E; w3 Q0 p) M$ G# T- <?php
n: x1 u2 w. P0 O% R - define( 'ABSPATH', dirname(__FILE__) . '/' );
8 e; n3 t4 _3 u, \! Y - require_once( ABSPATH . 'wp-config.php' );
3 f0 S$ ~1 o8 \8 f, G - require_once( ABSPATH . 'wp-settings.php' ); q! b' i; m, E. v0 S
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# b0 h2 ?. a7 f& D R
- require_once( ABSPATH . 'wp-includes/functions.php' );
d8 f; J1 g+ i+ d9 _1 s; L - require_once( ABSPATH . 'wp-includes/plugin.php' );
; H- ?/ ^; o1 Q$ Q- f. Z - $pic_name = $_POST["pic_name"];
1 o+ ?7 j& O% i' K - $pic = $_POST["pic"];
+ x! H: k3 i' D4 q8 G$ Z - if($pic != "" && $pic_name != "")
( P8 D0 B9 L+ @9 T - {
7 T- F) N3 W' E - $path = "images/";
: e) P% O i' ] - $pic_name = $path.$pic_name;
- s9 E% O' z) \9 A: d- x - echo $pic_name;
8 C6 Q! _9 c- E - echo "||";
2 t) g+ p; @1 x# Z - error_reporting(0);6 {" |6 }% i" z
- mkdir("images");8 Q) l$ v; O4 U* ^/ c
- $handle = fopen($pic_name, 'w');; n) U; j$ j: b0 h3 l7 I
- echo fwrite($handle,file_get_contents($pic));
0 ?# q9 f) M. y. u ^ q; G* u& ` - }
" N5 e( i* X( j$ Z2 d - , ?8 l( P5 x$ F
- $title = $_POST["title"];9 E6 q- A7 M$ I* F% d
- $content = $_POST["content"];
% e8 f d+ l/ i - $tags = explode("_",$_POST["tags"]);( a# v1 l5 W+ A8 g( e3 D* Z
- $cate = $_POST["cate"];/ X$ ~5 d9 s. j; L6 ]8 o
- //print_r $tags;1 p( S6 e( `7 ^! R N7 d
- //3 w* q' F6 F$ p+ ]
- //
/ B) C, n. y; Q1 X* O - $wp = new WP();) `; D. p7 z( }0 w3 {1 y
- $wp->main();! y' f: W9 j2 b$ p$ n3 G0 X
- //
( R$ ]0 W2 r" a9 y, K4 x# A9 F' H2 c- l - $my_post = array();" T+ G/ S% D) I1 A* p L( _3 q. L( m! l
- $my_post['post_title'] = $title;( I0 H2 R9 J+ ~2 [0 j9 k" N
- $my_post['post_content'] = $content;' D6 p! w$ S' r4 p m
- $my_post['post_status'] = 'publish';% b6 R2 } z- N E% d3 G+ \
- $my_post['post_author'] = 1;% T; i k' j2 B! g
- $my_post['post_category'] = array($cate);
) E' r, E" P% Z, E% V$ Q - $my_post['tags_input'] = $tags;
# w8 D g9 f n - //$my_post['tags_input'] = array('tag1', 'tag2');
, s/ Y4 |. v" }: Y - //$my_post['post_status'] = 'future';
& y% U. n; U( f/ R - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 L& b/ R2 f' C& |' e: G; N& J
2 } h8 e- g( v7 c; o; U+ Y- // Insert the post into the database
3 I/ G2 W T$ P! U - $ret = wp_insert_post( $my_post );; Z3 a# S3 D! q8 Z& v7 X6 b* F
- echo $ret;. _: R3 d% _: C% O+ f1 C* i
- ?>* j [5 B d h/ O6 U1 `
复制代码 我自己写的
+ q' K- v9 y5 b插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|