Wordpress - Using functions outside of wordpress

2019-04-14 03:30发布

I'm using the following code to access my wordpress from another page:

    <?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. The only problem is that I can't use 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' );

标签: php wordpress
1条回答
萌系小妹纸
2楼-- · 2019-04-14 04:27

You need to include wp-blog-header.php to set up the wordpress environment.

    <?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; ?> 
查看更多
登录 后发表回答