|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . a4 y! Q8 a# z2 |1 l
; o' v5 h: @8 R5 O3 O9 O
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& R/ B5 w9 ?& z; B; ~ C# Y2 J
* ^$ J2 U& d) F' w& I我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' ^, ~$ t& A. Y, }# s5 y# c
+ T9 p# O0 |: x( V, y
方便实用 简单 何乐而不用呢
! [ Y, O! j5 s' k( F# Q- <?php1 D2 f! H9 D2 ~, L
- - e0 a( r7 A ~9 Y# L0 I
- define( 'ABSPATH', dirname(__FILE__) . '/' );
; {8 l, g9 }: o# c+ {) e9 x - require_once( ABSPATH . 'wp-config.php' );. a* t+ Z. D8 w3 }; g4 ]: C
- require_once( ABSPATH . 'wp-settings.php' );
o m0 n% {# e" [4 `; W - require_once( ABSPATH . 'wp-includes/class-wp.php' );
* k& @, f6 {) {* b$ N4 X# S) I: o$ E - require_once( ABSPATH . 'wp-includes/functions.php' );+ `; u7 L# i8 f# }
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 Y. c/ k8 t7 i8 F/ B; f7 F
- $title = $_POST["title"];
4 u( b# k. R; I- m* e - $content = $_POST["content"];% f$ Z# ?5 c; v4 b
- $tags = explode("_",$_POST["tags"]);2 Q: _( t9 F( |- r; T5 C7 v4 J# P
- $cate = $_POST["cate"];( w9 _9 S% M! d: b% M
- //print_r $tags; e9 j. n; `; m) a: i
- //1 `; G* g! s1 q5 Y3 W0 U
- //0 s' L, e j% G) M& X" J" D1 O2 U( a
- $wp = new WP();4 I/ l1 v- R9 e: f3 B: Q3 Z' w( B) y: H
- $wp->main();# r8 c3 O M5 ~% D8 E1 Z
- //( i3 i& h. v% K1 P" B9 V( r
- $my_post = array();
, K' I' W$ N% {# n: t2 z - $my_post['post_title'] = $title;% z: L- k; W6 N
- $my_post['post_content'] = $content;3 m7 V) R8 r8 [# [' m; D
- $my_post['post_status'] = 'publish';9 M7 I4 x6 H: d2 w5 x
- $my_post['post_author'] = 1;
# M! t6 v% f1 V9 J! C' h5 i - $my_post['post_category'] = array($cate);8 n( ?; Y/ G/ M, P3 |/ A, z
- $my_post['tags_input'] = $tags;9 \2 {' ]' n* T' ]" K' d
- //$my_post['tags_input'] = array('tag1', 'tag2');
0 H+ ~# E- F5 x1 H - //$my_post['post_status'] = 'future';7 {1 ~& P& ?3 R% I; z4 i
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
, Z" j& A5 m+ ?8 g( e - 2 p+ @- i9 q* @# }
- // Insert the post into the database
0 O+ x. y- w, L2 A& G4 B a/ G) U - $ret = wp_insert_post( $my_post );
" d: Y, A- o; `+ a/ o" S# W# v - echo $ret;
& Q6 [! U4 ? s z - ?>
3 O& c8 ^; w8 W! w! @8 j
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - I& B- _; Q& z0 c' c8 o& G/ |
- <?php
1 ?; O6 W5 l! C& Y9 V - define( 'ABSPATH', dirname(__FILE__) . '/' ); U. h+ t3 v+ d, Z
- require_once( ABSPATH . 'wp-config.php' );
% a* \* B; _/ ]5 a" B5 K8 [! ~ - require_once( ABSPATH . 'wp-settings.php' );
. _0 |9 t* ^& O2 O' y6 a7 m - require_once( ABSPATH . 'wp-includes/class-wp.php' );
q x! X7 y# `, S) v - require_once( ABSPATH . 'wp-includes/functions.php' );
; _1 j- M. j, p$ p' P V# t1 i7 A - require_once( ABSPATH . 'wp-includes/plugin.php' );
6 R- T# } E. |. u, G' @7 Z0 M - $pic_name = $_POST["pic_name"];' s5 q9 ~' \# N& f& k( g
- $pic = $_POST["pic"];
9 M9 p( z3 C* w0 u - if($pic != "" && $pic_name != "")% F, d( u* }3 J3 }: [& O
- {* _3 Q( ]& d( ~1 D
- $path = "images/";
# Z6 j% H# T( [" y5 j - $pic_name = $path.$pic_name;% G* P h7 P# G: O; a
- echo $pic_name;; S) [* ^# Q2 `' ~7 [
- echo "||";
0 |7 P/ w: p9 D - error_reporting(0);
4 B3 o2 J; f, v8 L2 V - mkdir("images");2 x' F0 @ W: y" i' T& e9 H
- $handle = fopen($pic_name, 'w');8 `0 M8 Q O# O$ G
- echo fwrite($handle,file_get_contents($pic));% y" `/ F+ T1 W0 j1 }6 X+ S! R! @
- }4 M' T9 i6 v; [2 [* e2 P
0 |" j% N0 f* N( `- $title = $_POST["title"];' r! H) M1 ?, P
- $content = $_POST["content"];
4 I7 ?: g5 r( M2 x - $tags = explode("_",$_POST["tags"]);
& M5 t# m' a3 ?+ y- N, d - $cate = $_POST["cate"];
/ y( i" u$ n1 @: T" V - //print_r $tags;0 n1 m$ O1 W, k$ F& k6 S" z( c
- //1 |9 G( K i; s+ G) g
- //+ E# i, [# F# k9 \9 Y2 a- H
- $wp = new WP();
; S% a) [- U, {! e - $wp->main();
t2 D1 U1 ^+ ~ - //1 V' |5 H) p& B5 g8 m9 |
- $my_post = array();
, b8 c6 M0 k# b3 m - $my_post['post_title'] = $title;
: f, P/ J; f4 G# ^5 O$ G T/ l - $my_post['post_content'] = $content;& _+ u: s: }' X; h
- $my_post['post_status'] = 'publish';
. y; `( l8 N4 a" ~5 W6 @ k0 b+ ] - $my_post['post_author'] = 1;. ~1 S, q8 R- J8 S& W; @6 K" I- [
- $my_post['post_category'] = array($cate);
! z. p6 b# I0 Q" I0 R; l5 a - $my_post['tags_input'] = $tags;% h' p+ }7 Z( h" B& K5 ?
- //$my_post['tags_input'] = array('tag1', 'tag2');& w t' J4 i# E: z/ W# M9 e
- //$my_post['post_status'] = 'future';
- Z( g$ S7 S) _; H a' g - ////$my_post['post_date'] = '2010-07-04 16:20:03';$ W9 r0 u) u. _9 A8 o, r4 k$ Y4 \8 y
7 L0 q+ X/ d6 \( `8 w+ w- // Insert the post into the database1 E8 ?$ d$ ^* }- ]+ y7 B5 F
- $ret = wp_insert_post( $my_post );
8 W- V9 X- U8 R' w% m" q, l& Z - echo $ret;+ R% s: ]3 b# Y0 i
- ?>
0 E9 R' E% d0 b. F6 r
复制代码 我自己写的
1 {- T* _/ _6 b6 C9 V( s插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|