2014-09-23 63 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循环想要

和使用$永久