简体   繁体   English

Wordpress-在Wordpress之外使用功能

[英]Wordpress - Using functions outside of wordpress

I'm using the following code to access my wordpress from another page: 我正在使用以下代码从另一个页面访问我的wordpress:

    <?php

    include $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
    global $wpdb;

    $image_ID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '6036' AND meta_key = '_wp_attached_file'");

    //regenerate thumbnail
    $fullsizepath = get_attached_file( $image_ID );
    $metadata = wp_generate_attachment_metadata( $image_ID, $fullsizepath );
    wp_update_attachment_metadata( $image_ID, $metadata );

    ?>

I get the following error: 我收到以下错误:

Fatal error: Call to undefined function wp_generate_attachment_metadata()

The $wpdb query works fine and get_attached_file, too. $ wpdb查询和get_attached_file也可以正常工作。 The only problem is that I can't use wp_generate_attachment_metadata . 唯一的问题是我不能使用wp_generate_attachment_metadata

Does anybody know why that happens? 有人知道为什么会这样吗? Did I forget to include something? 我忘了包括一些东西吗?

EDIT: I just realized I just forgot to include( ABSPATH . 'wp-admin/includes/image.php' ); 编辑:我刚刚意识到我只是忘了include( ABSPATH . 'wp-admin/includes/image.php' );

You need to include wp-blog-header.php to set up the wordpress environment. 您需要包括wp-blog-header.php来设置wordpress环境。

    <?php 
    require('blog/wp-blog-header.php');
    $args = array( 'numberposts' => 5, 'post_status'=>"publish", 'post_type'=>"post", 'orderby'=>"post_date");
    $postslist = get_posts( $args );

    foreach ($postslist as $post) : setup_postdata($post); ?>
        <ul class="headline">
            <li class="title">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
        </ul>
    <?php endforeach; ?> 

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

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