它只存储第一个帖子数据的原因是,每个帖子都使用相同的临时名称。如果要存储多个电影瞬态,则需要每个电影的唯一名称。
此外,获取瞬态的方式也违背了使用瞬态的目的。您希望首先尝试获取瞬态,如果它不可用,则生成一个值并将其设置为瞬态。
下面是我如何修改您的代码:
<?php
$cinemamoviename = get_post_meta( $post->ID, "cinama_moviename", true );
$cinemamovieyear = get_post_meta( $post->ID, "cinama_movieyear", true );
$cinemamovienames = urlencode( $cinemamoviename );
$transient_name = "get_movies_data_intrans_$cimemamoviename_$cinemamovieyear";
if( false === ( $movies_details = get_transient( $transient_name ) ) ) {
$args = array(
\'t\' => $cinemamovienames,
\'y\' => $cinemamovieyear,
\'plot\' => \'short\',
\'r\' => \'json\',
);
$url = add_query_arg( $args, \'http://www.omdbapi.com/\' );
$json = wp_remote_get( $url );
$obj = json_decode( $json );
$movies_title = $obj->Title;
$movies_year = $obj->Year;
$movies_datas = array( $movies_title, $movies_year );
set_transient( $transient_name, $movies_datas, 365 * DAY_IN_SECONDS );
}
?>