2014-09-23 62 views
1

我想獲取所有帖子並插入到自定義表格中,但我無法獲得url(修復)的帖子,但它始終爲空或出現錯誤。無法從Wordpress數據庫獲得永久鏈接

$posts = get_posts(array( "showposts" => 50)); 
global $wpdb; 
foreach($posts as $post) 
{ 
     $wpdb->insert('wp_employee', array('pottitle'=>$post->post_title, 'postid'=>$post->ID, , 'postid'=>$post->permalink), array('%s', '%s', '%s')); 
} 

回答

2

試試這個,讓我知道如果任何問題;)

function getp($s) 
    { 
     $result = post_permalink($s); 
     return $result; 
    } 

調用函數

$posts = get_posts(array( "showposts" => 50)); 
global $wpdb; 
foreach($posts as $post) 
{ 
     $wpdb->insert('wp_employee', array('pottitle'=>$post->post_title, 'postid'=>$post->ID, 'postid'=>$post->getp($post->ID)), array('%s', '%s', '%s')); 
} 

您也可以直接來電與使用post_permalink

'lastname'=>post_permalink($post->ID)) 
+1

是的它的工作謝謝:) – vivek 2014-09-23 11:59:59

0

get_posts()不返回像永久

嘗試值來檢查http://codex.wordpress.org/Template_Tags/get_posts

所以你需要手動進行與帖子ID永久鏈接或使用get_permalink()the_permalink()

+0

是的,這是可能的wiwh postid我嘗試它,但我得到錯誤:( get_permalink($ post-> ID) – vivek 2014-09-23 11:48:04

+0

嘗試the_permalink()? – 2014-09-23 11:48:53

+1

嘗試post_permalink($ post-> ID) – 2014-09-23 12:01:36

0

您可以使用下面的行在foreach循環內部

$permalink = get_permalink($post->ID); 
無論你在foreach循環想要

和使用$永久