Support

Account

Home Forums General Issues How to Echo a WYSIWYG Field?

Solved

How to Echo a WYSIWYG Field?

  • Hi,

    How do you echo the content of an ACF field of WYSIWYG type?

    This seems like a very simple question, but I can’t seem to find an answer, so…

    I’m still learning WordPress development, so please bear with me. I’ve tried echo get_field(“field_name”) to no avail. Nothing whatsoever is displayed in my web browser. I’ve discerned that it thinks the field is either null or empty, though I can confirm that it should not be – the page editor in the admin screen definitely has published content saved to the “field_name” field, and there are no typos in my page template’s PHP code.

    Searching the ACF documentation reveals that “each field type returns different forms of data (string, int, array, etc),” (https://www.advancedcustomfields.com/resources/get_field/). But the entry for ACF Wysiwyg fields (https://www.advancedcustomfields.com/resources/wysiwyg-editor/#template-usage) neglects to mention what type it is. I assume it’s a string?

    Or maybe I instead need to do something to get it to post in a while(have_posts()) loop? If so, what? It does not show up in the existing loop I have in my page template, which is in fact displaying content correctly saved in the normal WP WYSIWYG editor for the page in question.

    Thanks for any help,
    Brian

  • Since you say that you are learning,

    Step 1 is to make sure you’re in the template that is showing the content for the page/post in question. I would suggest installing a plugin like https://wordpress.org/plugins/which-template-am-i/

    Step 2 is that you must be inside “The Loop” https://codex.wordpress.org/The_Loop that is showing the current post/page/etc

  • Hi John,

    Thanks. I appreciate the basic info, though perhaps I have understated my experience level. I’m more an intermediate-level coder with respect to both WordPress and PHP; I understand the basics of both, but still have to look up a lot of things like filters and actions or PHP functions and precise parameters as I more often than not need a refresher.

    So, I definitely do know which template I’m using for the page in question already. I can tell in the page editor, and it’s a template I wrote the PHP file for myself. Other fields and custom fields are working fine, including not only the built-in WYSIWYG editor but 3 other ACF fields. Only the ACF WYSIWYG field is not displaying anything.

    I’ve also tried echoing the problematic ACF field both inside and out of the loop, to no avail.

    Maybe a code snippet would be helpful. Here’s my template PHP file’s contents (page-feature.php). The $subtext variable (get_field(“additional_subtext”)) is the problematic field; the other three fields all work fine ($heading and $button_text are ACF Text fields, $btnLink is an ACF Link field).

    <?php
    /**
     * Elegant Simplicity Theme: Feature Page Template
     *
     * This template is...
     *
     * Template Name: Feature Page
     *
     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
     *
     * @package Elegant_Simplicity
     * @since 5.2.0
     */
    
            get_header();
    
            $heading = get_field("heading");
            $btnText = get_field("button_text");
            $btnLink = get_field("featured_link");
            $subtext = get_field("additional_subtext");
    ?>
            <h1 class="cover-heading"><?php echo $heading; ?></h1>
    <?php   while (have_posts())
              {
              the_post();
              get_template_part('template-parts/content-featurepost', 'page');
              }
    ?>      <p class="lead"><a href="<?php echo $btnLink; ?>" class="btn btn-lg btn-outline-secondary"><?php echo $btnText; ?></a></p>
    <?php   if (!isset($subtext) || '' === trim($subtext))
              {
              echo $subtext;
              }
    
            get_footer();
    ?>

    Thanks!

  • Ah nevermind! I see my error now. I made a couple of PHP typos.

    I switched the exclamation points in that one line of code to be this:

    if (isset($subtext) || '' !== trim($subtext))

    It now works. Sorry to waste your time.

    Thanks,
    Brian

  • Sorry for going to basic, and for not getting back to you sooner. It’s always good to post code as someone would have caught the ! for you.

    I’ve been developing WP sites many years and I still need to look things up, the only difference may be that I find what I’m looking for faster.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.