Hello, I hope you’re doing well.
I’m encountering an issue where get_the_ID()
isn’t returning the expected ID within my Sage controller. I’m using the controller to handle some logic, but the retrieved ID seems to be incorrect.
My controller looks like this:
<?php
namespace App\View\Composers;
use Roots\Acorn\View\Composer;
class Test extends Composer
{
/**
* List of views served by this composer.
*
* @var string[]
*/
protected static $views = [
'*',
];
/**
* Data to be passed to view before rendering.
*
* @return array
*/
public function with()
{
return [
'taxName' => $this->display_taxonomy_name(get_the_ID(), 'tax-name'),
];
}
/**
* Display Taxonomy Name
*/
public static function display_taxonomy_name($post_id, $taxonomy) {
$terms = get_the_terms($post_id, $taxonomy);
if ($terms && !is_wp_error($terms)) {
$term_names = wp_list_pluck($terms, 'name');
return implode(', ', $term_names);
}
return '';
}
}
In my blade template (ACF Block), I’m attempting to showcase it with {!! $taxName !!}
. However, it appears blank. Interestingly, when I directly integrate display_taxonomy_name(get_the_ID(), 'tax-name')
into the template, it functions properly.
Thank you in advance for your assistance.