Roots Discourse

Controller method outputting the_content() html tags as text; the_excerpt() does not work at all

I have been trying to create a selectable template that behaves like a WordPress default archive.php page. I’m using Sage 9, and specifically I’m using Controller to route some custom ACF fields as well as default WordPress functions, specifically the_excerpt() and the_content. I’ve had limited success in getting these to output anything at all, and when I do, html tags are either omitted entirely, or they show up inline with the text, not as html characters that help structure the data, but just as characters of text, visible on the page. I’m really not sure what to do here. I think I may need to add another loop somewhere but I’m not sure.

(As an aside that I think is probably related: For quite some time, I couldn’t get any inline PHP directives like {{ get_the_author() }} or (at)php the_author() (at)endphp to work, but then I added (at)while (have_posts()) (at)php the_post() (at)endphp and they started working).

Here is my Controller method for the page

public function archiveLoop()
{
$archive_items = get_posts([
‘posts_per_page’=>‘5’,
‘meta_query’ => array([
‘key’ => ‘featured’,
‘value’ => ‘1’,
‘compare’ => ‘!=’,
]),
]);
return array_map(function ($archive_items)
{
return [
‘archive-content’ => apply_filters(‘the_content’, $archive_items->post_content),
‘permalink’ => get_post_permalink($archive_items->ID),
‘archive-feat-img-url’ => get_the_post_thumbnail_url($archive_items->ID),
‘archive-feat-img-alt’ => get_the_post_thumbnail_caption($archive_items->ID),
‘archive-title’ => get_the_title($archive_items->ID),
‘archive-excerpt’ => get_the_excerpt($archive_items->post_content),
];
}, $archive_items);
}
}

Here is the template file

@extends('layouts.app')
@section('content')
  <!-- @ resources/views/archive.blade.php -->
{{--
  Template Name: Blog
--}}
@if (have_posts())
@while (have_posts()) @php the_post() @endphp
 @include('partials.hero-archive')
@endwhile
 @endif
  @if (!have_posts())
   <div class="container700">
    <div class="alert alert-warning">
      {{ __('Sorry, no results were found.', 'sage') }}
    </div>
    {!! get_search_form(false) !!}
</div>
  @endif
  @if (have_posts())
<div class="container700">
@while (have_posts()) @php the_post() @endphp
//I forgot to include the loop above, and until I did that, entry-meta.blade.php wasn't outputting any dynamic data;
 @include('partials.content-archive')
@endwhile
</div>
 @endif
  {!! get_the_posts_navigation() !!}
@endsection

Here is the section I’m having trouble with; included via the previous block of text

  <!-- @ resources/views/partials/content-archive.blade.php -->
@foreach ($archive_loop as $archive_items)
<div class="post-excerpt">
  <article @php post_class() @endphp>
    <header>
      <div class="feat-img-container">
        <img class="feat-img" src="{{ $archive_items['archive-feat-img-url'] }}" alt="{{ $archive_items['archive-feat-img-alt'] }}" />
      </div>
      <h2 class="entry-title"><a href="{{ $archive_items['permalink'] }}">{{ $archive_items['archive-title'] }}</a></h2>
      @include('partials/entry-meta')
    </header>
    <div class="entry-summary">
      <h6>$archive_items['archive-content']</h6><br>
      {{ $archive_items['archive-content'] }}
      <br><br>
      <h6>$archive_items['archive-excerpt']</h6><br>
      {{ $archive_items['archive-excerpt'] }}
    </div>
  </article>
</div>
@endforeach

And finally, here is a pic of what is rendering on the pertinent section of the page:

https://laravel.com/docs/5.6/blade#displaying-data

By default, Blade {{ }} statements are automatically sent through PHP’s htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

You’re echoing escaped text, hence the plaintext tags (if you examine your HTML source you’ll see they’re HTML entities).

Your aside is almost certainly unrelated: many WordPress tags have to be inside The Loop in order to function, and Sage is no different. However, data provided by Controller is not tied to the loop in that way, and echoing variables passed from a Controller will (generally) not be affected by the loop.

1 Like

Thanks! I forgot about that syntax for unescaped content.