简体   繁体   English

WordPress的wp_insert_post(); 在一个脚本中转到不同的安装

[英]Wordpress wp_insert_post(); to different installations in one Script

Imagine i have a big set of preformatted data and want to create wordpress posts out of it. 想象一下,我有大量的预格式化数据,并想从中创建wordpress帖子。

What i need is just some little snippet how to: 我需要的只是一些小片段:

  1. check if there is a wordpress connection (weather wp-blog-header.php is included or not) 检查是否存在wordpress连接(是否包含天气wp-blog-header.php)
  2. close wordpress database connection 关闭wordpress数据库连接
  3. include("wp-blog-header"); 包括( “WP-博客报头”);
  4. insert post 插入帖子

i would do like so... is this a way i can go? 我想这样做...这是我可以走的路吗?

$data = array(
                    "path/to/wordpress-instance/wp-blog-header.php" => array(
                        "post_title" => "bla",
                        "post_content" => "blubb"
                        ...
                    ),
                    "path/to/wordpress-instance/wp-blog-header.php" => array(
                        "post_title" => "bla",
                        "post_content" => "blubb"
                            ...
                    ),
                    "path/to/wordpress-instance/wp-blog-header.php" => array(
                        "post_title" => "bla",
                        "post_content" => "blubb"
                            ...
                    )
                );



                foreach($data as $path => $dataItem){

                    if($wpdb){
                        @mysql_close( $wpdb->dbh );//close connection
                    }

                    include($path);//new wp-blog-header

                    wp_insert_post($dataItem);


                }

that sounds like a roundabout way of doing things. 这听起来像是回旋处。 why bother shutting down the db connection in the first place? 为什么首先要打扰数据库连接?

instead, just create a page in wp. 相反,只需在wp中创建一个页面即可。 then create a page template that runs php code for that page. 然后创建一个页面模板,为该页面运行php代码。

instead of printing any data from the database for that page, just loop through and print whatever data you want. 而不是从该页面的数据库中打印任何数据,只需遍历并打印所需的任何数据即可。 (so you don't need any sort of wp_query loop). (因此您不需要任何wp_query循环)。

if you want to pass parameters to determine which page to show, you can setup your urls like: 如果您想传递参数以确定要显示的页面,则可以设置以下网址:

http://example.com/the_page/some_parameter

then use php to parse the url, get the parameter and display the correct page. 然后使用php解析网址,获取参数并显示正确的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM