首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress wp-insert-post函数创建任意无标题页面

WordPress wp-insert-post函数创建任意无标题页面
EN

Stack Overflow用户
提问于 2021-04-08 04:30:10
回答 1查看 29关注 0票数 1

我正在使用wp-insert post函数从我正在工作的网站动态生成页面。但是,每次我刷新页面以及代码生成的实际页面时,我都会收到这些没有标题的帖子。

我试图通过在页面创建过程中添加一个钩子来解决这个问题,但是这似乎没有任何作用。我想知道如何才能只创建我需要的页面,而不显示这些标题帖子。

我的代码:

代码语言:javascript
复制
if( !class_exists("PageCreator")) {
    class PageCreator
    {

        public function __construct()
        {
            add_action('init', array($this, 'AddThisPage'));
        }

        public function AddThisPage()
        {
            $dirName = "/zotpull/resources/temp/";
            $filename =  dirname(__DIR__, 2) . $dirName . "useData.txt";
            $theFile = fopen($filename, "r");
            $msg = fread($theFile, filesize($filename));
            fclose($theFile);

            $links = explode("\n", $msg);
            foreach( array_slice($links, 0, count($links) -1) as $item ) {

                $item = str_replace("/","-",$item);
                $str2 = substr($item, 5);


                $page = array(
                    'page_template' => 'datePage.php', //Sets the template for the page.
                    'post_title' => $str2, //The title of your post.
                    'post_status' => 'publish',
                    'post_type' => 'page'
                );

                if ( ! function_exists( 'post_exists' ) ) {
                    require_once( ABSPATH . 'wp-admin/includes/post.php' );
                }
                $page_exists = post_exists($page['post_title']);

                if ($page_exists == 0) {
                    $insert = wp_insert_post($page);
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-08 13:48:06

尝试在wp_insert_post之后使用wp_update_post。检查下面的代码。

代码语言:javascript
复制
if( !class_exists("PageCreator")) {
class PageCreator{

        public function __construct()
        {
            add_action('init', array($this, 'AddThisPage'));
        }

        public function AddThisPage()
        {
            $dirName = "/zotpull/resources/temp/";
            $filename =  dirname(__DIR__, 2) . $dirName . "useData.txt";
            $theFile = fopen($filename, "r");
            $msg = fread($theFile, filesize($filename));
            fclose($theFile);

            $links = explode("\n", $msg);
            foreach( array_slice($links, 0, count($links) -1) as $item ) {

                $item = str_replace("/","-",$item);
                $str2 = substr($item, 5);


                $page = array(
                    'page_template' => 'datePage.php', //Sets the template for the page.
                    'post_title' => $str2, //The title of your post.
                    'post_status' => 'publish',
                    'post_type' => 'page'
                );

                if ( ! function_exists( 'post_exists' ) ) {
                    require_once( ABSPATH . 'wp-admin/includes/post.php' );
                }
                $page_exists = post_exists($page['post_title']);

                if ($page_exists == 0) {
                    $insert = wp_insert_post($page);

                    $my_post = array(
                        'ID'           => $insert,
                        'post_title'   => $str2
                    );
                     
                    wp_update_post( $my_post );

                }
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66993715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档