Roots Discourse

SoberWP Controller: Access $post objects data from archive page

I’m using the Controller plugin to grab ACF data from posts. For example:

public function my_value() {
    return get_field('my_value');
}

I need to access this data from single pages but also from archive pages like front-page, so I placed these methods in the App controller.

Calling {{ $my_value }} from single returns the value but returns false from within The Loop in front-page . Digging a bit deeper, I found out that the same if true when calling get_the_ID() from the class: the ID is retuned in single, but not for archives.

Perhaps I need to explicitly invoque the $post element from within the class to make it accessible, however I’m not sure how to do this.

One thing is clear, I would like to avoid having to pass the ID as a variable in template files, as is the case for single templates.

1 Like

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?

11 Likes

Controller is so insanely useful that I think there’s an argument to be made for shipping it with Sage, or at least as a Composer package that could be optionally included with Sage and autoloaded.

I get why it’s a plugin right now, but its job is fundamental to how the theme is developed, which makes me want to package it with the theme itself.

9 Likes

This could not be more clear! Thanks so much for all your help. I have everything I need now to work on the weekend :slight_smile:

2 Likes

No problem, and sorry it took a while! It’s been a busy week.

Let me know if you’ve got any other questions as you go!

This is off topic, so might warrant a separate thread, but is it within your interest, @withjacoby, to abstract the plugin one step and turn the Controller functionality into a Composer package, and then include THAT in the plugin? That would let us also include said package in a Sage theme and development could occur in a single repository.

1 Like

Man, that would be AWESOME. I get why it’s a plugin, but, at least in my case, controllers are so “linked” with my theme files and structure that I would much prefer to have the soberwp/controller as a theme dependency rather than a mu-plugin.

4 Likes