有条件地在wp_ins_post中设置post_content

时间:2014-02-22 作者:Richard Mišenčík

是否可以在wp\\U insert\\U post中有条件地设置post\\U内容?Im使用foreach一次添加多个页面:

foreach ($create_pages as $new_page) {
    $add_pages = array(
        \'post_title\' => $new_page,
        \'post_content\' => \'\', // lets say, if the page_title is \'Home\', set the post_content to \'Default home page content\'
        \'post_status\' => \'publish\',
        \'post_type\' => \'page\'
    );
    $page_id = wp_insert_post($add_pages);
}
是否也可以为php文件中的所有页面设置默认内容?

3 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

我忍不住想我错过了什么,但似乎最明显的答案是改变你的$create_pages 阵列:

$create_pages = array(
  \'one\' => \'content for one\',
  \'two\' => \'content for two\',
  \'three\' => \'content for three\'
);

foreach ($create_pages as $title => $content) {
    $add_pages = array(
        \'post_title\' => $title,
        \'post_content\' => $content, // lets say, if the page_title is \'Home\', set the post_content to \'Default home page content\'
        \'post_status\' => \'publish\',
        \'post_type\' => \'page\'
    );
    var_dump($add_pages); // debugging
//     $page_id = wp_insert_post($add_pages); 
}
可以使用第二个帖子内容数组并将其与标题匹配,但根据您对问题的描述,我不明白您为什么需要这种复杂性。

另一种选择是使用嵌套数组,如下所示:

$create_pages = array(
  array (
    \'title\' => \'one\', 
    \'content\' => \'content for one\'
  ),
  array (
    \'title\' => \'two\', 
    \'content\' => \'content for two\'
  ),
  array (
    \'title\' => \'three\', 
    \'content\' => \'content for three\'
  ),
);

foreach ($create_pages as $page) {
    $add_pages = array(
        \'post_title\' => $page[\'title\'],
        \'post_content\' => $page[\'content\'], // lets say, if the page_title is \'Home\', set the post_content to \'Default home page content\'
        \'post_status\' => \'publish\',
        \'post_type\' => \'page\'
    );
    var_dump($add_pages);
//     $page_id = wp_insert_post($add_pages);
}
我很确定这两个版本都可以正常工作(参见thisthis), 尽管如果你有长标题,后者可能会更具可读性。

如果该数组太大,可以将其放入另一个文件中include 信息技术

SO网友:Shazzad

是的,你可以试试这个-

foreach( $create_pages as $new_page ){
    $content = "Default ". strtolower($new_page) ." page content";
    $add_pages = array(
        \'post_title\' => $new_page,
        \'post_content\' => $content,
        \'post_status\' => \'publish\',
        \'post_type\' => \'page\'
    );
    $page_id = wp_insert_post($add_pages);
}
编辑:关于外部逻辑,您可以尝试以下操作。

foreach( $create_pages as $new_page ){

    $content = "Default ". strtolower($new_page) ." page content";
    // allow external filter to the page content.
    // filter names become available appending the page name with \'external_page_content_\'
    $content = apply_filters( \'external_page_content_\' . sanitize_title_with_dashes($new_page), $content );

    $add_pages = array(
        \'post_title\' => $new_page,
        \'post_content\' => $content,
        \'post_status\' => \'publish\',
        \'post_type\' => \'page\'
    );
    $page_id = wp_insert_post($add_pages);
}
现在,要为页面添加内容,例如:关于我们

add_filter(\'external_page_content_about_us\', \'my_about_us_page_content\');
function my_about_us_page_content($content){
    // here you can define your about us page content
    return $content;
}
参考号:
  1. sanitize_title_with_dashes - 这将使用破折号和非字母数字字符分隔空格,并使所有字母字符的大小写都变为小写
  2. apply_filters - 这用于离开回调,就像我们对call_user_func

SO网友:Graeme

取决于您希望它以什么为条件。E、 g。

<?php
foreach( $create_pages as $new_page ){
 if ($new_page == \'Home\') {
   $content = "Home is where the heart is";
} else {
   $content = "$new_page is going to be a great page.";
}

$add_page = array(
        \'post_title\' => $new_page,
        \'post_content\' => $content,
        \'post_status\' => \'publish\',
        \'post_type\' => \'page\'
    );
    $page_id = wp_insert_post($add_page);
}
?>

结束