|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
% J' }! z) f$ I: H( A# Q6 U* E* X W1 _7 f2 b3 F. x. E( M
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
/ g& \: e6 ^& G, I
8 \6 H$ i0 D+ e- `我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了/ t- Z* g1 n8 [' J8 A
4 K2 z0 y5 l# f: Q' w+ \方便实用 简单 何乐而不用呢
$ y+ n5 a! K; n- <?php
; i- I6 g- w4 i* H$ j4 A- K/ `2 K/ _ - 9 r7 j# ~1 }' w
- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 m, W1 G& I+ e; H* [ - require_once( ABSPATH . 'wp-config.php' );
, K5 k8 h7 k# L - require_once( ABSPATH . 'wp-settings.php' );/ O& ^! U6 _& F4 J
- require_once( ABSPATH . 'wp-includes/class-wp.php' );' Z0 S2 }6 s9 j2 O0 ?3 q# z
- require_once( ABSPATH . 'wp-includes/functions.php' );
& F% \4 E+ ?: ~8 i" i. X7 X0 q5 Q. U - require_once( ABSPATH . 'wp-includes/plugin.php' );) b6 e, M- Y* g) X( L$ \' f! ?7 n
- $title = $_POST["title"];1 r2 s3 \1 V" p; ]9 i" g, q! m
- $content = $_POST["content"];
( g; X F* b: v5 y7 k, Y5 J - $tags = explode("_",$_POST["tags"]);5 k5 S- L' E, j. l
- $cate = $_POST["cate"];
]! w/ W5 i# \1 j! i+ E: x [ - //print_r $tags;
7 t6 v: g/ R. Q, @ - //; ]) Y+ f" s3 T3 J
- //
w) Q$ D9 @6 A# Y7 x - $wp = new WP();- m5 \ x5 S# i. V
- $wp->main();9 e5 s7 w, P0 Q) N8 p
- //
t" t# H2 }0 t i9 w5 o: D - $my_post = array();1 W1 W5 ^4 q5 ]/ S
- $my_post['post_title'] = $title;
- w5 X) I4 {/ ^% a7 c& [! j- ~2 {7 k* p - $my_post['post_content'] = $content;
" }6 ]6 P i" \' L6 n9 s7 N - $my_post['post_status'] = 'publish';
( A4 x4 N9 t9 |: ]0 l7 W+ X - $my_post['post_author'] = 1;
! a$ l0 x0 j7 u, s. s7 k, N - $my_post['post_category'] = array($cate);
1 `5 g, ]3 B2 r/ k% j - $my_post['tags_input'] = $tags;
/ O' h' y& ~( b( `. d - //$my_post['tags_input'] = array('tag1', 'tag2');/ x& M. ^$ B. F5 S' N$ N
- //$my_post['post_status'] = 'future';+ m% [, \* Z( }% H- f. V8 y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 ?2 O v6 ], x( }+ S
$ a }7 D- S' K- // Insert the post into the database
. h& Z |/ [$ B! {7 u/ x - $ret = wp_insert_post( $my_post );) C9 Q5 I1 v; \. k7 h7 [
- echo $ret;
) n4 O6 u+ ~6 M% W: @% X - ?>, s: d3 K' O; S! Q0 i0 ]1 |# U5 d7 w8 x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - V9 y7 W0 ?8 h+ U4 ?& e
- <?php
) X, s9 v8 z9 S; d5 y - define( 'ABSPATH', dirname(__FILE__) . '/' );
8 M) c2 _7 @( Y) Y, ^! x5 O - require_once( ABSPATH . 'wp-config.php' );
- L9 Y3 f F; z& s - require_once( ABSPATH . 'wp-settings.php' );
2 G B( Q) e! d2 s8 r; o( P - require_once( ABSPATH . 'wp-includes/class-wp.php' );$ z* r0 q5 ]4 D* q
- require_once( ABSPATH . 'wp-includes/functions.php' );9 c. @, {1 Y- D5 l/ E2 B
- require_once( ABSPATH . 'wp-includes/plugin.php' );
& V% H' a: a# K. Y$ U9 L - $pic_name = $_POST["pic_name"];, C3 f0 I4 }8 V- j, h* L
- $pic = $_POST["pic"];! j; L! \6 C+ v
- if($pic != "" && $pic_name != "")% A' k( n9 ^, Z! k
- {
. y, P4 M R& D: v% u - $path = "images/";/ k% C. a u8 ]" g3 z+ ^1 @& }# O
- $pic_name = $path.$pic_name;4 a' ~* w9 c0 h3 Y% W5 Z' M
- echo $pic_name;
9 ^8 C& d8 M! _% O" q: t - echo "||";
- |8 x' v$ s, m/ O% Z$ F - error_reporting(0);
0 c! R% _1 B: ^* _7 X5 |% d2 ~+ N- J9 k3 K - mkdir("images");6 K5 ~+ ^8 Y* b( Y
- $handle = fopen($pic_name, 'w');
5 u: c. L$ g, F+ d0 R1 }4 a - echo fwrite($handle,file_get_contents($pic));
5 H% _; U% K& x1 v% N' p - }
; T. I4 t5 ~) v6 T: f- ] - + _3 e. P" [' Q: f- a
- $title = $_POST["title"];/ @6 z3 g s$ c$ @ g
- $content = $_POST["content"];
) ~4 k- @( p) I4 j0 G - $tags = explode("_",$_POST["tags"]);4 J% {( |+ {3 g' s; B1 G& |0 o5 |9 O
- $cate = $_POST["cate"];- Y& K$ E C$ R. w! _8 F
- //print_r $tags;
6 P: ^% i8 F' f/ o - /// a7 ~; |* y U$ @) {6 ]
- //
0 d( S8 a! r4 f' Q' u7 S7 I0 N - $wp = new WP();
7 T! R+ A2 j8 z, j - $wp->main();- O& C6 g9 G$ e: x: Q# _
- //
3 @7 [$ X/ A1 R; j- }8 v1 x) | - $my_post = array();
9 C# \6 L- [( P5 S' ~/ k, X - $my_post['post_title'] = $title;; t- }7 x% X7 O* b* I% D: P
- $my_post['post_content'] = $content;- P; ^: N8 `* @" o
- $my_post['post_status'] = 'publish';
& A- L& a% ?1 o. b - $my_post['post_author'] = 1;
0 F" M$ d# q& E - $my_post['post_category'] = array($cate);
0 ~, G6 T3 I1 a+ I: l% X/ u& A - $my_post['tags_input'] = $tags;
8 t8 [* w- u2 [# S( V3 S9 E* q - //$my_post['tags_input'] = array('tag1', 'tag2');
7 R; t0 S1 y: b. {/ J% f7 \0 l - //$my_post['post_status'] = 'future';& M4 Z3 E6 y# _& i Y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 a1 r: a- g) l y. }, U
3 o4 s- {% g h& Y- // Insert the post into the database
+ v/ i1 p7 y" b$ T - $ret = wp_insert_post( $my_post );
" `) ?' O" E1 R& i - echo $ret;" Z) w+ {& b6 p8 U, L
- ?>1 u4 R( l+ h$ R( A8 H* C
复制代码 我自己写的
: K+ O5 X' T1 l插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|