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 応答は以前と同じようにファイルに書き込まれますが、データのない空のスケルトンになります。私が行っているのは、上記のコードを新しい関数内に囲み、top$test_tag変数を渡して関数を実行することだけです。echo()渡されたものを検証するも追加$test_tagします。何らかの理由で、$test_tag が両方のテストで同じであっても、 get_pix()and/or関数がデータの取得に失敗しているようです。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));
}

シンプルなものが欠けていることを本当に願っています。newFunction()内部の ,$test_tagは、echo() が同じ値を持っていることを証明しているにもかかわらず、どういうわけか異なる方法で扱われているとしか思えません。

4

1 に答える 1

0

私はそれを考え出した。wordpress 関数 get_the_ID() は関数内では機能せず、null 値を返します。代わりに、同一の $post->ID を使用しましたが、うまくいきました。私はワードプレスが好きではありません。

于 2012-11-13T23:10:01.713 に答える