如何使用此PHP脚本生成多个WordPress页面?

时间:2019-08-29 15:20:19

标签: php wordpress

我正在尝试找到一种方法,使我的代码循环遍历所有PostCreator函数,而不是一次只添加一页。最初,我尝试仅在第一个之后添加一个新的PostCreator,但是它没有用。最好的方法是什么?我是否使用forEach?任何帮助将不胜感激!

PostCreator( 'Name of Page', 'page', 'Hello Word - Content','','','1','publish');
PostCreator( 'Name of Page 2', 'page', 'Hello Word 2 - Content','','','1','publish');

这是完整的脚本。

if ( ! function_exists( 'PostCreator' ) ) {

    function PostCreator(
        $name      = 'AUTO POST',
        $type      = 'post',
        $content   = 'DUMMY CONTENT',
        $category  = array(1,2),
        $template  = NULL,
        $author_id = '1',
        $status    = 'publish'
    ) {

        define( POST_NAME, $name );
        define( POST_TYPE, $type );
        define( POST_CONTENT, $content );
        define( POST_CATEGORY, $category );
        define( POST_TEMPLATE, '' );
        define( POST_AUTH_ID, $author_id );
        define( POST_STATUS, $status );

        if ( $type == 'page' ) {
            $post      = get_page_by_title( POST_NAME, 'OBJECT', $type );
            $post_id   = $post->ID;
            $post_data = get_page( $post_id );
            define( POST_TEMPLATE, $template );
        } else {
            $post      = get_page_by_title( POST_NAME, 'OBJECT', $type );
            $post_id   = $post->ID;
            $post_data = get_post( $post_id );
        }

        function hbt_create_post() {
            $post_data = array(
                'post_title'    => wp_strip_all_tags( POST_NAME ),
                'post_content'  => POST_CONTENT,
                'post_status'   => POST_STATUS,
                'post_type'     => POST_TYPE,
                'post_author'   => POST_AUTH_ID,
                'post_category' => POST_CATEGORY,
                'page_template' => POST_TEMPLATE
            );
            wp_insert_post( $post_data, $error_obj );
        }

        if ( ! isset( $post ) ) {
            add_action( 'admin_init', 'hbt_create_post' );
            return $error_obj;
        }

    }
}
/* All available options for PostCreator()
PostCreator( 'TITLE' , 'POST TYPE' , 'POST CONTENT' , 'POST CATEGORY' , 'TEMPLATE FILE NAME' , 'AUTHOR ID NUMBER' , 'POST STATUS');
TITLE - HTML Stripped Out. Simple String.
POST TYPE - Post type slug. Eg 'post' or 'page'. Custom Post Types are supported.
POST CONTENT - Content of the Post/Page. HTML allowed.
POST CATEGORY - An array of the integer ID's of the category/categories you want to link to your post
TEMPLATE FILE NAME - File name of the template. Only for Pages. In the format 'file_name.php'.
AUTHOR ID NUMBER - Integer value. Default is 1.
POST STATUS - Available options; [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ]
If successful, PostCreator() returns nothing.
If there is an error PostCreator() returns a WP_error object.
*/
PostCreator( 'Name of Page', 'page', 'Hello Word - Content','','','1','publish');


0 个答案:

没有答案