Hello everyone, I am new to sage 9 and looking for some guidance.
I’m doing a basic project to get acclimated to the changes in sage9. I’ve create a links custom post type, given it some fields with cmb2. I also gave it categories using WordPress’s taxonomies, set up a template to query each and whipped up some quick Bootstrap 4 cards in blade to display them.
sage9 made the whole process a dream.
controlers/page-link.php
<?php // @ app/controllers/page-link.php
namespace App;
use Sober\Controller\Controller;
class Link extends Controller
{
public static $prefix = '_link_';
public function linkCategories()
{
$categories_array = get_terms([
'taxonomy' => 'link_category',
'hide_empty' => false,
]);
return $categories_array;
}
public function linkId()
{
return get_the_ID();
}
public function linkURL()
{
return get_post_meta(get_the_ID(), self::$prefix . 'url', true);
}
public function linkUrlSecondary()
{
return get_post_meta(get_the_ID(), self::$prefix . 'url_secondary', true);
}
public function linkTextSecondary()
{
return get_post_meta(get_the_ID(), self::$prefix . 'text_secondary', true);
}
}
vies/page-link.blade.php
We get the categories from a variable passed by the controller
@extends('layouts.app')
@section('content')
@foreach ($link_categories as $category)
@include('partials.link-category', ['category' => $category])
@endforeach
@endsection
Link Category Partial
Runs once per category. Displays links in that category
@php
$links = new WP_Query([
'post_type' => 'link',
'orderby' => 'title',
'order' => 'ASC',
'nopaging' => true,
'tax_query' => [
[
'taxonomy' => 'link_category',
'field' => 'slug',
'terms' => $category->slug,
],
],
]);
@endphp
<div class="link-category">
<h2>{{$category->name}}</h2>
<p>{{$category->description}}</p>
@while($links->have_posts()) @php $links->the_post() @endphp
@include('partials.link-card')
@endwhile
</div>
Link Card
Simply displays a single link
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{ get_the_title() }}</h5>
<p class="card-text">{{ get_the_content() }}</p>
<a href="{{$link_url}}" class="btn btn-primary">Visit Site</a>
@if ($link_url_secondary)
<a href="{{ $link_url_secondary }}" class="btn btn-secondary">{{ $secondary_link_text }}</a>
@endif
</div>
</div>
So here is the issue:
The $link_url and all other link variables are empty, because when the controller runs get_the_id() returns the id of the links page, Not the id I need, which is the id of the link post type.
Now there are 2 ways I can go about this.
Make functions static and call them within the loop
Requires minimal changes and keeps things intuitive.
In the controler:
public static function linkURL()
{
return get_post_meta(get_the_ID(), self::$prefix . 'url', true);
}
In the template:
{{ Link::linkUrl() }}
Run all queries in controller
This would be the textbook answer. We remove all logic from our views and handle everything in the controller, then we pass that information to the views as variables.
Somehow this feels like the messier answer because I would be working against WordPress rather than with it.
What are your thoughts? How would you go about doing this?