|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
/ m" u0 o5 E E5 J: V
. Q9 i% M, u; i5 z' }9 a0 G直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持$ k; T, v: q1 _8 T. r' W
1 @, U# K6 V8 q5 c7 y6 y! v S- J
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了2 j6 d1 N2 ^6 V X0 B5 ^
7 O- h) Q% l7 q# E% v2 r
方便实用 简单 何乐而不用呢- , K. N9 ~, h; F8 ?0 D/ S% R/ Q/ {# V: g
- <?php
0 ?+ G; Y* w& j6 G f - 5 o+ j$ d3 \, g' x5 L
- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ l" T; ^/ p* E" ]0 X - require_once( ABSPATH . 'wp-config.php' );
' A( }* ]: s; W) G+ I - require_once( ABSPATH . 'wp-settings.php' );
0 d8 }3 E; _, z: p" O - require_once( ABSPATH . 'wp-includes/class-wp.php' );
' {' [+ c: E! K1 S1 | - require_once( ABSPATH . 'wp-includes/functions.php' );
9 R( C) a' w- i/ P+ W" E' O - require_once( ABSPATH . 'wp-includes/plugin.php' );& r3 i3 k2 x; Q( H- n/ I
- $title = $_POST["title"];5 ~: q8 n3 U: E% ?, y' Z7 U+ J
- $content = $_POST["content"];4 }8 G7 p' T/ M; e# i$ s: S
- $tags = explode("_",$_POST["tags"]);
" G j- z1 Q1 e% O9 P% g6 c1 C0 O - $cate = $_POST["cate"];
; J+ l% i4 {6 R5 P% o: V0 I - //print_r $tags;8 G; f6 f# d2 ^8 K* C
- //
" q/ P% w* ?; e; k3 M- G% @& s - //" X: P7 ~9 ?* H8 R1 r
- $wp = new WP();$ ? E' d+ o, v# I6 h! ~
- $wp->main();6 \* d* L" k4 O% d: r) J+ [4 P0 C
- //
- g# c6 T' T0 f8 P8 b - $my_post = array();
7 w; s- o2 P8 g# I: V- ]& B - $my_post['post_title'] = $title;9 Q9 o7 ?5 k2 i; b
- $my_post['post_content'] = $content;0 T% l# B! ^, y. D/ z
- $my_post['post_status'] = 'publish';
9 n- l: _" |4 B: T9 O" X - $my_post['post_author'] = 1;
& N- e3 j5 u0 t9 Q' D/ Z3 L2 a - $my_post['post_category'] = array($cate);
7 s e: Y9 Z" T& Z - $my_post['tags_input'] = $tags;& s' R7 G" u( _+ X; W% l
- //$my_post['tags_input'] = array('tag1', 'tag2');# \2 n/ S# E: D& F2 E, }- q' h
- //$my_post['post_status'] = 'future';" V; X y Q9 C0 Y" L# q5 v y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';, H2 L; Q& j6 l, b% F
- / c1 l& b/ r& a2 z
- // Insert the post into the database
. h9 d* a# r0 r - $ret = wp_insert_post( $my_post );
' ?+ f+ W$ ?6 m0 p' |6 ]4 `) ` - echo $ret;
4 P! o; F3 ?# G, ` U _ - ?>
' c# |8 ]9 Z* C7 t- t) u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- c: n5 ]2 a8 V! Y- <?php
1 F) R# q# J: s9 `$ @ - define( 'ABSPATH', dirname(__FILE__) . '/' );* ]7 L( F" W A* K; o
- require_once( ABSPATH . 'wp-config.php' );
9 c: l4 { \5 I) ^& B3 X - require_once( ABSPATH . 'wp-settings.php' );# l2 C7 |, D7 v* M4 P; M. D
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
; ~8 u, R# C6 C+ C5 X# w - require_once( ABSPATH . 'wp-includes/functions.php' );
; P4 e9 m2 M2 J; H - require_once( ABSPATH . 'wp-includes/plugin.php' );5 I* U) S4 l8 n5 b7 Y
- $pic_name = $_POST["pic_name"];
2 V+ ]9 N" [: b% c! u5 z - $pic = $_POST["pic"];9 |: ?9 p0 {8 o% D& ~
- if($pic != "" && $pic_name != "")- S3 B- Z) D6 T
- {
6 _- R5 W# Y, e! x, I - $path = "images/";- ~2 c$ o& H" d" z# h
- $pic_name = $path.$pic_name; z5 z( R$ n2 x. T6 t+ Y
- echo $pic_name;
: W$ k" b5 h" I% C6 I - echo "||";' s0 w; L. T- |3 t6 {( p# S
- error_reporting(0);. |9 r; b! ^! D. G, J# v
- mkdir("images");) E" x& k8 X: W9 `
- $handle = fopen($pic_name, 'w');
7 r4 s$ Z" `& |4 z( T- f" w! l& w/ } - echo fwrite($handle,file_get_contents($pic));8 Q& C+ p# q' x8 `
- }( Z% E/ ~' ]2 e/ }
- , ?5 [2 z0 R1 F( z2 _6 ~
- $title = $_POST["title"];
/ Z Z0 S: N' B2 \0 o K - $content = $_POST["content"];; t, V3 ]9 p; t, F
- $tags = explode("_",$_POST["tags"]);& v+ Y' B+ R: ]$ |
- $cate = $_POST["cate"];
" E# M- g& F8 H6 l5 J - //print_r $tags;
; ?$ j4 L7 G0 O) | - //8 c% Y7 g( g* \% g: _5 |
- //& X7 D# R3 ]" ]0 ~+ M2 g" t
- $wp = new WP();. I& P/ M: K3 Q) h
- $wp->main();# Z, c1 M$ n) p0 ?+ ], R
- //2 w; S% [6 i$ m w! I* ~# l2 i- p
- $my_post = array();7 l, v1 Q, r; C! {: L$ y0 k
- $my_post['post_title'] = $title;; ]: n/ g3 q0 b) @' b7 i
- $my_post['post_content'] = $content;
! g# \& @% n7 Q; d+ |' }5 u - $my_post['post_status'] = 'publish';
$ ]% m: W! t- Y; q! I( j' H& b1 z - $my_post['post_author'] = 1;
, f2 L; c1 i/ D5 H - $my_post['post_category'] = array($cate);
6 G% R2 _0 {7 y& N G3 w - $my_post['tags_input'] = $tags;. Q2 R" g/ b1 h7 s
- //$my_post['tags_input'] = array('tag1', 'tag2');
- S: A/ ?* @8 I: \ - //$my_post['post_status'] = 'future';
! m7 o6 s: C p3 z5 K - ////$my_post['post_date'] = '2010-07-04 16:20:03';1 b$ l: L y* ?7 q K
3 f0 L) _1 J3 l, B' m5 y4 c- // Insert the post into the database
0 n; n S; C* f7 H - $ret = wp_insert_post( $my_post );
. l; R9 O/ w7 T3 Q/ z - echo $ret;) s2 U0 M7 ~( {+ T
- ?>6 M& j5 |, G1 ~/ I+ V
复制代码 我自己写的
& n! X) w0 _+ c插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|