Hello. I just migrated from Sage8 to Sage9 and I am trying to understand Blade as much as I can and I love it so far. Now I have created a custom post type in my “app/setup.php” file and I added 10 posts in the backend. I now want to do a custom WP Query as I usually do but using Blade so I tried to add the following to “app/controllers/app.php”:
public function projectsLoop()
{
$projects_items = get_posts([
'post_type' => 'ctp_projects',
'posts_per_page'=>'6',
]);
return array_map(function ($post) {
return apply_filters('the_content', $post->post_content);
}, $projects_items);
}
this of course is inside class App extends Controller {
and then inside “views/partials/homepage/homepage-projects.blade.php” I have added the following code:
@foreach($projectsLoop as $project_item)
{!! $project_item !!}
@endforeach
I get this error:
Undefined variable: projectsLoop in /srv/www/michelec/current/web/app/uploads/cache/d8946328bcc3eb4c6204474ad8a86a6bd66b21d4.php on line 5
It’s worth to mention that I have done the following changes in this project which shouldn’t affect anything I assume:
I have created a custom “app-custom.blade.php” file inside the layouts folder which I have cloned from the original one and slightly changed to my needs:
<!doctype html>
<html @php(language_attributes())>
@include('partials.head')
<body @php(body_class())>
@php(do_action('get_header'))
@include('partials.header')
<div class="wrap" role="document">
<div class="content">
<main class="main">
@yield('content')
</main>
</div>
</div>
@php(do_action('get_footer'))
@include('partials.footer')
@php(wp_footer())
</body>
</html>
Created a custom template for the homepage called “template-homepage.blade.php”:
@extends('layouts.app-custom')
@section('content')
@while(have_posts()) @php(the_post())
@include('partials.content-homepage')
@endwhile
@endsection
And then inside the “partials” folder I have the “content-homepage.blade.php” that includes each and every section that I need to build the long homepage:
@include('partials.homepage.homepage-intro')
@include('partials.homepage.homepage-about')
@include('partials.homepage.homepage-projects')
Reading this post I am starting to understand how to create controllers but I have some questions. I see that there is a controller for FrontPage.php. What if I want to create a new controller just for my custom template (template-homepage.blade.php). Because I don’t have a “front-page.php” file I have put mine in “controllers/app.php”. Is that wrong?
Also I am going to add ACF soon so I might have some questions but I see that Advanced Custom Fields is already discussed in other topics so I will try to read them first after I solve this issue.
Sorry for the long post and thanks for the time.