|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ; S: l0 s0 w4 |8 q# d( V
( U' Y9 H3 k- X8 ?3 w直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 k! F" K) b7 x2 ]3 P# N
1 J, Q6 t+ ^. A$ X) A, u6 }
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
" e, l7 `' _! b! S# Y; T# [9 w ^, \% _ i" |. ~" \5 j
方便实用 简单 何乐而不用呢
) j# b& k) |) L ]; Y- <?php$ N8 g4 x/ V5 B' z1 j, d3 X: |
+ l5 y+ m! d+ z1 |% l- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 L' [- Q) F+ X: Y - require_once( ABSPATH . 'wp-config.php' );" z+ |5 n' B) ^7 V6 L! A
- require_once( ABSPATH . 'wp-settings.php' );4 I3 h! {2 J* e' \
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 E# c/ H% w, N/ N% N
- require_once( ABSPATH . 'wp-includes/functions.php' );
% z0 j, v, {- ^. B8 @+ w( ?0 I - require_once( ABSPATH . 'wp-includes/plugin.php' );8 ?$ x4 w8 [8 [, _. h
- $title = $_POST["title"];4 {0 P Y/ t9 D' S7 t
- $content = $_POST["content"];
; c2 ?1 G. B! y( B$ y - $tags = explode("_",$_POST["tags"]);2 C0 ~9 K% \2 h, u& f D" I
- $cate = $_POST["cate"];
! M7 x1 Z; X2 r' }! {# R - //print_r $tags;
9 X. G3 A) _/ F, L - //
: i! Q2 [& e$ c" H$ y5 U - //& j* E2 v: I1 j2 Z2 p9 K. B" p; s
- $wp = new WP();; A0 ?$ B$ ~( F
- $wp->main();5 N/ ] t- p' G0 _
- //
2 J1 n2 q) Y! P0 n - $my_post = array();
) A6 }# Q A5 f& u) @ - $my_post['post_title'] = $title;
- g9 b5 J( y) P# F" ]# p - $my_post['post_content'] = $content;# u; w" A3 j+ U4 ]. [- `0 S
- $my_post['post_status'] = 'publish';
D( ^$ `2 _/ y - $my_post['post_author'] = 1;
$ n7 Q6 Y' c3 L2 [: @* ~ - $my_post['post_category'] = array($cate);, w3 J( `% ^: b+ O
- $my_post['tags_input'] = $tags;: D. p+ S8 W# ]. s) [# c3 [ y* O2 |4 G
- //$my_post['tags_input'] = array('tag1', 'tag2');
5 @/ g( v+ Y; h! Z! J - //$my_post['post_status'] = 'future';
% c# U* H; l3 Z" ^; \! \8 x - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( u6 M" v) P7 v) g5 o: \- b! Z
; |, Z* y* K, E* I; I7 L) @; r$ H- // Insert the post into the database
2 w/ H( u) R9 s+ @* | - $ret = wp_insert_post( $my_post );! E2 F# x& m: r
- echo $ret;( {3 n3 |$ D. |# w- `
- ?>6 u; Z0 Y' ^% n- T7 `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 1 c. s1 |8 V, B: X& j
- <?php- t, d% V* S: u- O4 J8 z
- define( 'ABSPATH', dirname(__FILE__) . '/' );% e' b0 A4 Z. E& i
- require_once( ABSPATH . 'wp-config.php' );
0 C0 j1 Q! e2 ?; \ y: n9 d* k - require_once( ABSPATH . 'wp-settings.php' );& g8 s" q7 n: y2 a' | Q
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 \1 @' y( a/ \; M- i - require_once( ABSPATH . 'wp-includes/functions.php' );4 `* W0 R2 z' |+ } o1 @
- require_once( ABSPATH . 'wp-includes/plugin.php' );
) C& S. E5 p U9 E) d5 T - $pic_name = $_POST["pic_name"];
! g' f9 h$ i0 |1 y6 M - $pic = $_POST["pic"];' a- z5 a9 u* h i' Y+ N C
- if($pic != "" && $pic_name != "")
: u- x4 R4 z# [ O2 z& z. Q7 ` - {
1 E1 a% y$ ]- p6 I8 D: J' B4 }$ c3 `# b - $path = "images/";" j# P+ _9 m8 x5 o0 b$ \' ~6 Z
- $pic_name = $path.$pic_name;
. K8 O- K; V3 a# i! n6 S - echo $pic_name;
! x' J: V; Y7 R+ Z9 M# L - echo "||";7 \, W9 I4 _% u! l
- error_reporting(0);! c7 y3 J7 ` Y" G" i: f
- mkdir("images");1 r( W) a7 t5 f5 {; b- t" R
- $handle = fopen($pic_name, 'w');* o. d: w$ y9 L6 O' w& W- d
- echo fwrite($handle,file_get_contents($pic));; t. f* b) n6 b
- }
" T9 z* M; U5 G5 R( W - ; E7 Z( g {* V
- $title = $_POST["title"];2 ?( e# N ^& y- @5 Y9 a
- $content = $_POST["content"];0 {- J9 [) j3 _7 B# p, m
- $tags = explode("_",$_POST["tags"]);( Z4 _3 I6 S0 a1 v% f
- $cate = $_POST["cate"];
; v, U2 I8 n7 z - //print_r $tags;* { @( c2 _: L& O! j# G
- //
: x" a) n! l. B) e* ?5 h1 M - //
; g+ W2 c2 K5 J* { - $wp = new WP();3 c; ^% K3 |8 Z) ^2 z7 Z' C
- $wp->main();
1 y5 q, T. H K1 W! A - //! j n7 O5 v* e
- $my_post = array();) x: D) a$ {6 J! ~& C
- $my_post['post_title'] = $title;3 A& S' G. ]8 _% h Q2 U
- $my_post['post_content'] = $content;4 H8 u- p2 |5 U) m( F: d
- $my_post['post_status'] = 'publish';
0 N( @' Y9 Y: S0 e) S. _9 q& p- P - $my_post['post_author'] = 1;5 Q7 L" D5 y R$ x) E
- $my_post['post_category'] = array($cate);, H7 @ |) q7 m7 W/ G
- $my_post['tags_input'] = $tags;6 w. X' j. \% @% f* h
- //$my_post['tags_input'] = array('tag1', 'tag2');: x+ O( h, \9 Y: \2 f# S+ d
- //$my_post['post_status'] = 'future';7 D& j; D! _$ m( r0 k
- ////$my_post['post_date'] = '2010-07-04 16:20:03';5 {' s! U; L( w9 z
- 7 W6 E1 ^% O. ` {' ^
- // Insert the post into the database" w4 Z' l: b' e3 T. e! x
- $ret = wp_insert_post( $my_post );+ E+ p) k3 l0 m2 W
- echo $ret;# p' z: }' H `% X% K
- ?>- g7 T4 x$ ]8 w9 I
复制代码 我自己写的( \7 Q1 U) n+ S" B' V" B
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|