English 中文(简体)
(wp-blog-header.php) 不在循环中工作
原标题:require( wp-blog-header.php ) does not work in a loop

我试图从外部的 php 文件 wp_ 插入_ post 。 如果没有循环, 此文件可以工作 。 我花了很长时间, 但我无法查找类似信息 。

<?php

require( wp-blog-header.php );
$tmpstr = array(
           ID  => 1,
           post_title  => $title,
           post_content  => $post content,
           post_status  =>  publish ,
           post_author  =>  1 ,
           post_type  => $type
       );
wp_insert_post($tmpstr);
?>`

然而,当我把一个环,

<?php
for ($i=0;$i<10,$i++) {
require( wp-blog-header.php );
$tmpstr = array(
           ID  => 1,
           post_title  => $title[$i],
           post_content  => $post content[$i],
           post_status  =>  publish ,
           post_author  =>  1 ,
           post_type  => $type
        );
   wp_insert_post($tmpstr);
}
?>

It insert only 1 time into mysql database, then it stops I have tried changing the require( wp-blog-header.php ); to require( /path/to/wp-blog-header.php ); but it still does not solve my problem. If i comment out the wp_insert_post and require( wp-blog-header.php ); and add echo $post_content[$j];echo $post_title[$j]; all values are displayed correctly in my browser

Could anyone please help me to make it loop for 10 times, so that it can insert 10 entries? Thank you in advance!

最佳回答

您一直要求每个循环中的文件。 永远不要这样做。 请在循环中将要求放入循环中。 如果您将其放入循环中, PHP 会错误地说函数已经定义了 。

<?php
require( wp-blog-header.php );
for ($i = 0; $i < 10; $i++) {
$tmpstr = array(
           ID  => 1,
           post_title  => $title[$i],
           post_content  => $post content[$i],
           post_status  =>  publish ,
           post_author  =>  1 ,
           post_type  => $type
        );
   wp_insert_post($tmpstr);
}
问题回答
for ($i=0;$i<10,$i++) {
               ^

错误! 一定是:

for ($i=0;$i<10;$i++) {
               ^




相关问题
Wrap stray text in <p> tags

Wordpress issue.. how do I wrap stray text in P tags? Example: Before- <div class = "content"> <img src = "hello.jpg"/> <h1>Introduction</h1> Hello! this is ...

Using jQuery Plugins with Wordpress

Having a bit of trouble using jQuery plugins (Superfish, jQuery UI, etc) using Wordpress. Everything works fine in my plain non-Wordpress site, but Wordpress seems to conflict with JQuery. There must ...

WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Why can t I properly style a blockquote in Wordpress?

On the design I just created for my website, I have a blockquote styled with two quote images using the span technique in css: blockquote { background-image: url(images/openquote.jpg); background-...

How does the WordPress <!--nextpage--> tag actually work?

What happens? I m guessing that somehow the post or page is parsed before displaying, and then just split into two methods? I can t seem to find any documentation on how the underlying <?php ...

Wordpress Plug-ins: How-to add custom URL Handles

I m trying to write a Wordpress Plug-in but can t seem to figure out how you would modify how a URL gets handled, so for example: any requests made for: <url>/?myplugin=<pageID> will ...

热门标签