|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 , V, l/ l/ M) a* L/ \% p( W4 `
0 E2 F! _; @+ b+ s. B直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
* V& U7 u4 Q2 n: }5 m9 K% y6 `" T6 M3 W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了+ p: A( l' L8 m8 w3 f
+ R/ I4 b+ Q* I. A
方便实用 简单 何乐而不用呢
+ s2 {* E/ g( g2 y- <?php" P1 a' c: R8 `1 m2 a, V/ Q( x
- ' J! t( [7 P% h. [! g" E$ G
- define( 'ABSPATH', dirname(__FILE__) . '/' );. ]" N/ |& r" |0 l# |
- require_once( ABSPATH . 'wp-config.php' );* X, f8 \& _, a1 c% a6 A/ j
- require_once( ABSPATH . 'wp-settings.php' );
4 p# U$ s5 I4 n( M8 A# D: Z - require_once( ABSPATH . 'wp-includes/class-wp.php' );% J5 R( H3 m) n
- require_once( ABSPATH . 'wp-includes/functions.php' );
1 j5 ?; r0 H5 s; [( O, ` - require_once( ABSPATH . 'wp-includes/plugin.php' );# U2 T! b1 q l8 b: }
- $title = $_POST["title"];. E( f+ I" p: S, d$ |6 ?3 N8 V9 y" ]
- $content = $_POST["content"]; j( v3 o* q, i" G) M. e
- $tags = explode("_",$_POST["tags"]);7 p, L- G+ V% R2 Q
- $cate = $_POST["cate"];: I: o9 G! P/ c8 p
- //print_r $tags;
7 O* Z7 Q8 }- U9 X5 Q9 x - //+ s) q3 |8 C8 I: G! n2 P5 c2 G
- //
1 A7 L+ S% p% F2 Y! k - $wp = new WP();2 x. D: y# L6 D) m
- $wp->main();
4 @3 t8 s& d# \* K - //
& I3 ~5 N0 B- \, C8 I8 v - $my_post = array();. n9 b- v6 |/ o" l. f# R
- $my_post['post_title'] = $title;
8 A% ?" I3 M8 | S( A" U- Q# N - $my_post['post_content'] = $content;
! w& t Z# J( K6 r. ^ - $my_post['post_status'] = 'publish';
1 Z0 W+ f: r/ @9 u" O' s0 j - $my_post['post_author'] = 1;9 S% @/ _6 ~: ?7 _
- $my_post['post_category'] = array($cate);
# G; m1 v7 F; h: i - $my_post['tags_input'] = $tags;
9 @7 X1 b H6 y4 V U O( @ - //$my_post['tags_input'] = array('tag1', 'tag2');
, I; i) k5 H& Y3 v: w7 V# J - //$my_post['post_status'] = 'future';
' K, P& K- Y- e/ ?9 L# x3 | - ////$my_post['post_date'] = '2010-07-04 16:20:03';
6 V$ h( \4 F! `: O1 b$ S! V* {
' h* L3 `7 N4 I; U. P- // Insert the post into the database- Y2 q! d2 K$ B3 X1 f' R& P
- $ret = wp_insert_post( $my_post );. _4 J+ W# s* [- K' j! n( l
- echo $ret;- r( b$ G. e0 y( U, D1 S5 s
- ?>8 U' t0 q) }+ p) z, Z ~
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& `2 b( S# Y3 E8 \- <?php
5 |* y5 H5 K* ?, P) @2 s" Q - define( 'ABSPATH', dirname(__FILE__) . '/' );
7 M$ \5 h; b+ J( _1 ^ - require_once( ABSPATH . 'wp-config.php' );; z* t2 l9 F# A4 {, x& j7 z% {
- require_once( ABSPATH . 'wp-settings.php' );
! x: N2 h% q% ~, _7 g$ t - require_once( ABSPATH . 'wp-includes/class-wp.php' );, \- V5 \" v5 k5 }7 j
- require_once( ABSPATH . 'wp-includes/functions.php' );
4 a" z6 N# r+ j: R3 o& ?; y4 | - require_once( ABSPATH . 'wp-includes/plugin.php' );1 [$ O$ C% ?' N
- $pic_name = $_POST["pic_name"];3 g' V \$ u$ M0 e }& ?
- $pic = $_POST["pic"];
2 t- ?0 F5 x% L8 V9 n: I - if($pic != "" && $pic_name != "")
- ]- D0 g. u7 r/ f - {
D; M1 U0 ?' ?+ @* m$ l - $path = "images/";
3 ], r6 s' N* G1 S2 p# X' n2 W - $pic_name = $path.$pic_name;8 t2 @% D: ~9 v/ [! y
- echo $pic_name;
; o: x$ H& N, X. F/ B7 L0 {! F - echo "||";7 h% U/ L$ V9 |0 h3 V
- error_reporting(0);
! ]! D" E \$ M* ^) S/ p- a - mkdir("images");
0 C' G; N L/ s& z' m - $handle = fopen($pic_name, 'w');; g) q1 f2 l5 y* r
- echo fwrite($handle,file_get_contents($pic));
& O: w9 j) v1 Z2 Y: ]- M: o! j - }% x) S; }8 C; ~) D
- 5 P( M1 j6 N* v+ F- ]8 G
- $title = $_POST["title"];
% w9 L8 C3 y: _' T6 Q5 q - $content = $_POST["content"];
8 v/ f- w4 C7 f1 y; R# Y - $tags = explode("_",$_POST["tags"]);( c& W$ m* v* x- ]* ~3 D6 I
- $cate = $_POST["cate"];
& F O% t4 Q/ H - //print_r $tags;
" |6 U; L* N' ?. v9 a6 p# E - //
2 o& e0 p' {# U- W - //" L6 Q! W! f& M5 _, i5 [
- $wp = new WP();+ ]$ R7 k8 U$ F/ L G) I
- $wp->main();, i% Y/ P8 S! b2 \: N
- //+ s+ k: n. t/ A |7 s9 G
- $my_post = array();
- e7 w2 a4 d8 l2 W - $my_post['post_title'] = $title;
6 f6 W c: I; q! `/ G: I6 |0 ~ - $my_post['post_content'] = $content;' X+ Y) D5 ~( V# |! Q {7 m) C+ V
- $my_post['post_status'] = 'publish';
: Q: {. O9 M: ?* R* E' `. T$ J1 ~ - $my_post['post_author'] = 1;4 Q3 q1 q4 v5 u1 |
- $my_post['post_category'] = array($cate);
( H+ j* o( w+ y! h8 d" u - $my_post['tags_input'] = $tags;
. E6 K* l, u' G - //$my_post['tags_input'] = array('tag1', 'tag2');
3 _% [9 C8 `% |9 t1 ?. ?, b6 i - //$my_post['post_status'] = 'future';/ K8 E0 ~' V5 M) Y M; J( H
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
- H7 |2 Z. b5 [( y! H
$ q' v. K+ W. _# \+ C- // Insert the post into the database, ^5 B e0 r5 J, g5 B* |) V1 B
- $ret = wp_insert_post( $my_post );/ i& V5 M& V) Q, N; P) v
- echo $ret;. y j8 ^3 t# H4 Y8 o, @
- ?>" D6 r3 s' i3 b
复制代码 我自己写的* ?/ `/ P- b* s8 a6 T3 [" `6 ^
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|