将函数值转换为字符串?

时间:2018-02-25 21:11:27

标签: php

我添加了require_once $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-load.php';

到我的网站,以便在我的自定义php内置网站的页脚中显示最新的博客帖子。

然而,由于我的网站特别是来自搜索引擎蜘蛛的流量,我的服务器使用的资源增加了三倍,导致我的服务器出现问题。

为了解决这个问题,我希望将此文件包含在cronjob文件中,该文件会将最后5篇博文发布到新的mysql表中(在完全清空表之前)并从我的网站中删除wp-load.php。 / p>

但是我遇到了从函数中将urls / titles插入我的mysql表的问题。

$args = array(
'posts_per_page' => 5 // Specify how many posts you'd like to retrieve
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
    while ( $latest_posts->have_posts() ) {
    $latest_posts->the_post();      

class url {
    public function __toString() {
      the_permalink();
    }
}

$url = strval(new url);

class title {
    public function __toString() {
        echo the_title();
    }
}

$title = strval(new title);

    $stmt = $pdo->prepare("INSERT INTO latestblogposts (`url`, `title`) VALUES (?,?);");
    $stmt->execute([$url, $title]);
    }
}

以下是the_permalink()的内容;功能

    function the_permalink( $post = 0 ) {
18          /**
19           * Filters the display of the permalink for the current post.
20           *
21           * @since 1.5.0
22           * @since 4.4.0 Added the `$post` parameter.
23           *
24           * @param string      $permalink The permalink for the current post.
25           * @param int|WP_Post $post      Post ID, WP_Post object, or 0. Default 0.
26           */
27          echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
28  }

如何将其转换为字符串,以便将内容插入mysql?

0 个答案:

没有答案