Hey @anon21078981,
So I’ve set up a use-case using your example, using a YouTube ID (as per our private discussion).
In you’re in the loop, it’s better to use a static public function (they do not get converted to a variable, and can be run from within your Blade template), which allows the function to be run from within the loop.
So, if you’re in an archive page, this will not work;
public function getYouTubeID()
{
return get_field('youtube_id');
}
It’s not being run from within the loop, so it will create a single variable $get_you_tube with only the latest post value.
This works well in the case of a single post, but not for the loop.
The solution and correct usage would be to create a public static function, which can be run for each loop iteration.
public static function getYouTubeID()
{
return get_field('youtube_id');
}
You would then call from within the loop (assuming your class name is Archive);
@while (have_posts()) @php(the_post())
{{ Archive::getYouTubeID() }}
@endwhile
If you want to use the function for both archive/index and singular pages, you could turn it into a trait and include in both view controller Classes.
Full example;
resources/controllers/partials/YouTubeID.php
<?php
namespace App;
trait YouTubeID
{
public static function getYouTubeID()
{
return get_field('youtube_id');
}
}
resources/controllers/Archive.php
<?php
namespace App;
use Sober\Controller\Controller;
class Archive extends Controller
{
use YouTubeID;
}
resources/controllers/Single.php
<?php
namespace App;
use Sober\Controller\Controller;
class Single extends Controller
{
use YouTubeID;
}
Then, within the Blade templates;
resources/views/archive.blade.php
@extends('layouts.app')
@section('content')
@include('partials.page-header')
@while (have_posts()) @php(the_post())
{{ Archive::getYouTubeID() }}
@endwhile
{!! get_the_posts_navigation() !!}
@endsection
resources/views/single.blade.php
@extends('layouts.app')
@section('content')
@while(have_posts()) @php(the_post())
{{ Single::getYouTubeID() }}
@endwhile
@endsection
Let me know if that solves your use-case?