|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
) z* |, Q! B' S. R, n. o$ F4 S# X' ]& a# J& T; Z/ A. g- j( [% Y! H
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
+ r3 J+ b7 a$ B9 X
1 ^. L, q' Q, ]1 P' ]8 j我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了; V3 J" @" D! m' o& v
4 k) m& n' V# o& Q7 C/ i方便实用 简单 何乐而不用呢- 1 _+ @% K# E8 y
- <?php
' H3 E( X) V @5 x5 s - " i. Z; k6 J. K8 I; a3 w
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# Q1 C- e) ]" T* @ - require_once( ABSPATH . 'wp-config.php' );
1 K( g t9 w, V+ B* j- ~ - require_once( ABSPATH . 'wp-settings.php' );
5 P( M5 R( |7 o2 i* C4 V - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) l) K7 h5 R6 t/ U$ J; |. ] - require_once( ABSPATH . 'wp-includes/functions.php' );3 [; \ Q- ~9 n9 O) U: N9 o% d& E
- require_once( ABSPATH . 'wp-includes/plugin.php' );
) |% K. r- O7 h8 |+ } - $title = $_POST["title"];( a' M+ E: l1 M' t! k: N7 f
- $content = $_POST["content"];/ V$ _! f* l( [, r! w
- $tags = explode("_",$_POST["tags"]);
% m- i+ B1 O6 C" F - $cate = $_POST["cate"];
0 [5 S: H/ i& a) U) b/ A - //print_r $tags;
$ \5 i5 Y, s& d$ r, ~. \3 p - //
, q& F/ [" S9 M9 w$ S - //
6 N) S$ a8 N, L3 S - $wp = new WP();
7 q# v( A# u2 d4 F+ [9 ?6 E - $wp->main();/ |0 e. [. v" Q6 t% }( a
- //& N+ d$ A/ x7 ?
- $my_post = array();
, a! l4 P. A$ Z% s - $my_post['post_title'] = $title;( f& k3 f1 z0 t
- $my_post['post_content'] = $content;
) C3 \# n' Z5 { y9 ~ - $my_post['post_status'] = 'publish';, G2 }7 D3 c( }* U& x8 Q
- $my_post['post_author'] = 1;" a+ N5 y; g1 b
- $my_post['post_category'] = array($cate);: J! k# b6 ~' h, `
- $my_post['tags_input'] = $tags;
S! R/ y: f1 [3 A% S2 U - //$my_post['tags_input'] = array('tag1', 'tag2');, T3 ?7 ?) L+ h4 q) c
- //$my_post['post_status'] = 'future';3 q9 v0 J' h: r* ^" u- N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
) I6 |% G P$ D% E% v+ v - 6 Z5 f. M8 N! z3 r5 }
- // Insert the post into the database: u# f( w5 G4 b/ T
- $ret = wp_insert_post( $my_post );
% F8 Q6 I/ {: U8 I - echo $ret;
% a; r" h5 B& l, k& S - ?>* j! Y. E. C- V' V$ L e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- , @$ a- z0 H7 x) s' S& [% _# A, y
- <?php: M3 j2 V5 K2 c( Q0 k3 J
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# D" C8 Y r7 Y3 a# O7 ? - require_once( ABSPATH . 'wp-config.php' );. q) l4 c. o, |9 I7 ^% V: Z
- require_once( ABSPATH . 'wp-settings.php' );" L9 G; N4 }% }2 k
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& \6 V [+ b' a* j p0 R9 [ - require_once( ABSPATH . 'wp-includes/functions.php' );$ f* g* S$ E5 _
- require_once( ABSPATH . 'wp-includes/plugin.php' );7 h7 r k4 t( v
- $pic_name = $_POST["pic_name"];* O0 F4 `& \' o) p) B- f
- $pic = $_POST["pic"];
5 H% O% G5 Q& d - if($pic != "" && $pic_name != "")/ f+ \' {. k& t
- {
; ?1 P1 {( c T1 c; V+ t, ] - $path = "images/";' ^- N6 }. z' G& s) R7 v6 O; Y
- $pic_name = $path.$pic_name;( L/ D) ~6 v: D5 x. f1 i
- echo $pic_name;
" @4 V- v4 Z) [4 v7 x - echo "||";8 X) n) b+ s; V6 E8 ?8 U5 K
- error_reporting(0);
6 `% k4 }( |. f! B1 [, J1 c3 p - mkdir("images");
6 i1 c& t. f: \* [/ n* U - $handle = fopen($pic_name, 'w');
8 f) ^2 Q) }/ e3 u3 U, |! q: q3 M - echo fwrite($handle,file_get_contents($pic));
" s6 k. ~9 q7 s: g; Z - }
1 W, M5 w, F2 i$ z - * w3 {4 i) B- l, f; o# J9 P; d4 C, [
- $title = $_POST["title"];; U" J: E2 c3 i5 ]; `# q
- $content = $_POST["content"];
: x4 i; Z' G1 a4 u1 x - $tags = explode("_",$_POST["tags"]);
: F! y7 I2 Q7 R$ { - $cate = $_POST["cate"];: D- s3 H. `0 X! c- |9 D3 e" y$ k7 K
- //print_r $tags;
% }4 O9 g' B+ C" c$ g - /// O& H* t$ H2 l, j1 V- S
- //
2 n0 e" M3 X/ F( a, p& [) y" k - $wp = new WP();
( w3 n R& [4 m - $wp->main();% R: D( } m4 b: {6 b% R
- //- A( }, `1 J& K1 r& m" E
- $my_post = array();
( j+ ^0 T$ @. F1 z: g4 r3 } - $my_post['post_title'] = $title;
) a ?0 V r1 `" @$ H6 `; r - $my_post['post_content'] = $content;
1 a* B7 c4 m8 |# M/ N( o) l! l - $my_post['post_status'] = 'publish';
# ?' Q: J& Y7 N3 n/ j8 o+ M - $my_post['post_author'] = 1;
3 \& a- v9 o7 c8 g4 a/ h! Y0 ` - $my_post['post_category'] = array($cate);* k" }' S% g9 q( [* n; @, E* x
- $my_post['tags_input'] = $tags;! _4 A0 L$ [4 F7 ?$ _
- //$my_post['tags_input'] = array('tag1', 'tag2');
5 F. i1 o2 b) a# p8 r7 h' P; u, @ - //$my_post['post_status'] = 'future';" i; k, v, K. r# M% t- \ t' ?
- ////$my_post['post_date'] = '2010-07-04 16:20:03';0 v$ `# m* L% m# c# j7 \
- : L2 R0 p) F! a0 \) U6 J
- // Insert the post into the database* \4 Q2 u$ u" k" H6 F9 c
- $ret = wp_insert_post( $my_post );
+ k, j7 _0 _2 _ - echo $ret;
4 C4 n+ K" ~/ p( V, n, G( V - ?>- K: }9 x$ }* e2 j- K* H. |# C, V
复制代码 我自己写的% a' `, h8 P/ v( U2 n$ n# ~
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|