1

这段代码工作正常。我能够获得写入缓存文件的填充 JSON 页面:

$test_tag = "afro";

$page = 1;
$images = array();
$tags = array();
$imagetype = 'Recent Cuts';
$per_page = 60;
$orderby_view = FALSE;

$tags2 = $test_tag;
$tags = explode(',', $test_tag);
if( count($tags) == 1 && strlen($tags[0]) == 0 ) $tags = array();
$tag_url = urlencode($tags2);

$cachename = dirname( __FILE__ ) . '/cache-fp/' . $imagetype . '-' . $per_page . '-' . $page . '-' . ($orderby_view ? 'by_view' : 'by_date') . $tag_url . '.json';

    $detailurl = get_option('image_detail_url');
    $detailurl .= (strstr($detailurl, '?') === FALSE ? '?' : '&');
    $json = array();
    $images = array();
    $posts = get_pix($imagetype, array('per_page' => $per_page, 'page' => $page, 'tags' => $tags), $orderby_view);


    foreach( $posts['attachments'] as $ii => $post ) {
        $ta = array();
        $meta = array();
        $imagesrclight2 = array();
        // BWP - Theater mode
        $ta['detail_url'] = $detailurl . 'uid=' . $post->post_author . '&img_id=' . $post->ID . '&theater';


        $meta = get_post_meta(get_the_ID(), 'image_tag', false);
        $ta['image_tags'] = implode(' ', $meta);


        $ta['attachment_image'] = wp_get_attachment_image($image->ID, 'thumbnail'); 
        $imagesrclight2 = wp_get_attachment_image_src($image->ID, array(150, 150)); 
        $ta['attachment_image_src'] = rawurlencode($imagesrclight2[0]);


        $images[] = $ta;
    }

    file_put_contents($cachename, json_encode($images));

但是当我将此代码包含在一个函数中时,JSON 响应会像以前一样写入文件,但它只是一个没有数据的空骨架。我所做的就是将上面的代码包含在一个新函数中,传入顶部$test_tag变量并执行该函数。我还添加了一个echo()验证传入的验证。由于某种原因,即使 $test_tag 在两个测试中相同,$test_tag似乎get_pix()和/或函数也无法获取数据。get_option()

$test_tag = "afro";
newFunction($test_tag);

function newFunction($test_tag) {

$page = 1;
$images = array();
$tags = array();
$imagetype = 'Recent Cuts';
$per_page = 60;
$orderby_view = FALSE;


echo $test_tag; //this works, so var is passed in properly

$tags2 = $test_tag;
$tags = explode(',', $test_tag);
if( count($tags) == 1 && strlen($tags[0]) == 0 ) $tags = array();
$tag_url = urlencode($tags2);

$cachename = dirname( __FILE__ ) . '/cache-fp/' . $imagetype . '-' . $per_page . '-' . $page . '-' . ($orderby_view ? 'by_view' : 'by_date') . $tag_url . '.json';

    $detailurl = get_option('image_detail_url');
    $detailurl .= (strstr($detailurl, '?') === FALSE ? '?' : '&');
    $json = array();
    $images = array();
    $posts = get_pix($imagetype, array('per_page' => $per_page, 'page' => $page, 'tags' => $tags), $orderby_view);


    foreach( $posts['attachments'] as $ii => $post ) {
        $ta = array();
        $meta = array();
        $imagesrclight2 = array();
        // BWP - Theater mode
        $ta['detail_url'] = $detailurl . 'uid=' . $post->post_author . '&img_id=' . $post->ID . '&theater';


        $meta = get_post_meta(get_the_ID(), 'image_tag', false);
        $ta['image_tags'] = implode(' ', $meta);


        $ta['attachment_image'] = wp_get_attachment_image($image->ID, 'thumbnail'); 
        $imagesrclight2 = wp_get_attachment_image_src($image->ID, array(150, 150)); 
        $ta['attachment_image_src'] = rawurlencode($imagesrclight2[0]);


        $images[] = $ta;
    }

    file_put_contents($cachename, json_encode($images));
}

我真的希望我错过了一些简单的东西。我只能怀疑 inside newFunction(),$test_tag以某种方式被不同地对待,即使 echo() 证明它具有相同的值。

4

1 回答 1

0

我想到了。wordpress 函数 get_the_ID() 在函数内部不起作用,返回空值。相反,我使用了 $post->ID,它是相同的,并且有效。我不喜欢 Wordpress。

于 2012-11-13T23:10:01.713 回答