0

I have a question about an issue I've been struggling with. If anyone has any ideas - let me know!

I added a "Theme" select field (with advanced custom fields), to allow the client to choose a "Theme" for the page hero on a specific template. The "Theme" dictates some pre-defined combination of colors for the background, text, and button(s). In the template, if an option to "Hero Theme" is selected, a class corresponding to that option is added to the section, and the colors are all set in css.

content-hero.blade.php

@php
  $id                   = get_sub_field('anchor_id');
  $theme                = get_sub_field('hero_theme') ? get_sub_field('hero_theme') : '';
  $bg_image             = get_sub_field('background_image') ? get_sub_field('background_image')['url'] : null;
  $supertext            = get_sub_field('supertext');
  $headline             = get_sub_field('headline');
  $subtitle             = get_sub_field('subtitle');
  $featured_image       = get_sub_field('image');
  $ctas                 = get_sub_field('ctas');
  $bottom_text          = get_sub_field('text_below_cta');
  $bg_color             = !get_sub_field('hero_theme') ? get_sub_field('background_color') : null;
  $light_text           = !get_sub_field('hero_theme') ? get_sub_field('text_color') : null;
@endphp
<section class="{{ $theme }} nm-hero{{ $light_text ? ' light-text' : '' }}{{ $bg_image ? ' has-hero-bg-img' : '' }}" {!! $id ? 'id="' . $id . '"' : '' !!} style="{{ $bg_color ? 'background-color: ' . $bg_color : '' }}">
...
</section>

I also added some code into my breadcrumb code, to set the breadcrumbs to white if the Hero Theme has a dark background:

intro-section.blade.php

@section('breadcrumbs')
  @php $is_white = false @endphp
  @if ( is_page_template('views/non-member.blade.php') )
    @php $nm_hero = get_field('nm_sections')[0] @endphp

    @if ( $nm_hero['acf_fc_layout'] == 'hero' )
      @php $theme = $nm_hero['hero_theme'] @endphp
      @if ( $theme )
        @if ( $theme == 'theme-blue' || $theme == 'theme-violet' || $theme == 'theme-blue-sky-blue' || $theme == 'theme-blue-yellow' || $theme == 'theme-blue-green' || $theme == 'theme-violet-yellow' || $theme == 'theme-gray' || $theme == 'theme-gray-yellow' )
          @php $is_white = true @endphp
        @endif
      @endif
    @endif
  @endif
  <div class="top-line">
    <div class="wrapper">
      <div class="breadcrumbs{{ $is_white ? ' is-white' : '' }}" style="float: {{ isset($contains_file) ? 'left' : 'none' }}">

...

Two things broke when I pushed this code.

First, the hero on all other templates stopped appearing. No error message, just poofed from existence. This issue was resolved when I removed the new breadcrumb code.

Then, the hero for the template I was working on disappeared (ugh). I resolved the issue by removing the updates from content-hero.blade.php.

Now I need to push the update again, but I'm not sure how to push it without running into the same problems. I can't replicate it on staging or local sites.

WordPress: 6.2.2
PHP: 7.3
Laravel/Sage

Let me know if you have any ideas / anything to try.. Thanks for your help!

4
  • In the breadcrumbs part, you may need to open the PHP tag and close it only before the HTML.
    – Ahmad Wael
    Jun 15, 2023 at 8:35
  • @AhmadWael These files were actually written with a language called Blade, which allows you to use the syntax @if @else @endif instead of the more cumbersome <?php if (condition) { ?><?php } else { ?><?php } ?> Jun 15, 2023 at 15:51
  • I suggest you to put all the logic in the breadcrumbs part to a separate function that returns only the value needed. That way will make it looks simpler to you and also will be easier for maintenance.
    – Ahmad Wael
    Jun 15, 2023 at 16:14
  • @AhmadWael That's great advice, I appreciate it. Jun 15, 2023 at 18:07

1 Answer 1

0

Maybe you're not getting an error, if there is no error at all, but just a misunderstanding?

By default, without custom coding, is_page_template() will only work on posts/pages where the Post Meta "_wp_page_template" is set. You can set this value in the Page Attributes Metabox when editing a Post. You can enable this by enabling "page-attributes" through add_post_type_support().

Maybe this Post Meta value is missing on the page's you expect the Hero / Section to show.

Hope this helps.

3
  • Something tells me this is probably the answer, but it may be missing some info. When I check my page attribute options, I only see "Parent, Template, and Order." Did you mean something else by that? And it sounds like you're referring to a hook in your answer. Could you add the full hook? Once that's complete, I'll be able to test out this answer and award it the bounty :) Jun 20, 2023 at 19:45
  • Hi _wp_page_template is the meta_key which is stored in the postmeta database. The add_post_type_support() is a function that can be called on init e.g. add_action( “init”, “some_function” ); See the official docs for more information about the function developer.wordpress.org/reference/functions/…. If you’re registering a Custom Post Type, you can also set the parameter “supports”. This param expects an array e.g. “title”, “editor”, “post-attributes”, … Jun 21, 2023 at 20:58
  • I just checked the WP Core repo. It seems that the function WP_Theme::get_page_templates() is responsible for showing/hiding the “template” option in the Page Attributes Metabox. Luckily there is a filter available “theme_templates”. Or more specified filters based on Post Type etc… developer.wordpress.org/reference/classes/wp_theme/… I’ll probably go down that road to create options without the template comments. Jun 21, 2023 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.