• I am now looking to display JUST the view count and duration values for the video on the home/list pages (via loop.php) but having difficulty determining the appropriate PHP code to do this.

    Here is the function that I’ve been using successfully for the post view:

    add_shortcode(‘yt’, ‘getYoutubeDetails’);

    function getYoutubeDetails($atts) {
    extract(shortcode_atts(array(
    ‘video’ => ”
    ), $atts));

    // Get YouTube data via the API
    $JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos?q=$video&alt=json”);
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{‘feed’}->{‘entry’}[0]->{‘yt$statistics’}->{‘viewCount’};
    $views = number_format($views);
    $duration = $JSON_Data->{‘feed’}->{‘entry’}[0]->{‘media$group’}->{‘yt$duration’}->{‘seconds’};

    echo “<iframe width=\”1280\” height=\”720\” src=\”//www.youtube.com/embed/$video?showinfo=0&rel=0&modestbranding=1&theme=light&iv_load_policy=3&autohide=1&enablejsapi=1\” frameborder=\”0\” allowfullscreen></iframe>”;
    echo “Views: $views
    Duration: “;
    echo sec2hms($duration);
    echo “

    “;
    }

    Within my post content, I tag my videos as [yt video=”VIDEOID”]. I was hoping I could get the specific attribute for views/duration by using: ‘<?php echo GetYoutubeDetails($views) ?>’ in loop.php, but that doesn’t seem to work. Is there a way to structure this to pull in a specific attribute within the function? Would it be able to leverage the value being passed into the video attribute of the shortcode that’s already in the post content?

    I’d prefer to tap into the existing shortcode already in the post content (so I’m not having to maintain it two or more different places). However, if there isn’t a way to echo the value of an existing function for a particular post in loop.php, I was thinking it may need to get handled via a custom field somehow.

    Any suggestions for how to tackle this issue?

  • The topic ‘How to Reference/Echo Attribute from Another PHP Function’ is closed to new replies.