php - Allow HTML in custom taxonomy description, and show it on front end

admin2025-01-08  5

Sorry I'm quite new at coding, and also working with WordPress and ACF, and I'm trying my best but I can't figure this out:

I'm trying to load my taxonomy description in rich text. Currently I'm using this code:

<?php
$terms = get_the_terms(get_the_ID(), 'locatie'); // Get the terms associated with the post
if ($terms && !is_wp_error($terms)) :
foreach ($terms as $term) : ?>
<h2><?php echo ($term->name); ?></h2>
<p><?php echo ($term->description); ?></p>
<?php endforeach;
endif;
?>

I want to put in location information, add some line breaks, maybe use HTML, and add a link to Google Maps or something. I downloaded a plugin to enable a rich text editing within the taxonomy, and formatted it correctly. However, the code return is without all the formatting. How can I make sure that the formatted, rich text/html description is loaded?

Any suggestions are more then welcome <3

Sorry I'm quite new at coding, and also working with WordPress and ACF, and I'm trying my best but I can't figure this out:

I'm trying to load my taxonomy description in rich text. Currently I'm using this code:

<?php
$terms = get_the_terms(get_the_ID(), 'locatie'); // Get the terms associated with the post
if ($terms && !is_wp_error($terms)) :
foreach ($terms as $term) : ?>
<h2><?php echo ($term->name); ?></h2>
<p><?php echo ($term->description); ?></p>
<?php endforeach;
endif;
?>

I want to put in location information, add some line breaks, maybe use HTML, and add a link to Google Maps or something. I downloaded a plugin to enable a rich text editing within the taxonomy, and formatted it correctly. However, the code return is without all the formatting. How can I make sure that the formatted, rich text/html description is loaded?

Any suggestions are more then welcome <3

Share Improve this question edited Nov 17, 2024 at 20:36 Casper Boon asked Nov 17, 2024 at 20:36 Casper BoonCasper Boon 13 bronze badges 2
  • Duplicate of stackoverflow.com/questions/79197929/…. Please only post on once, on the most appropriate site. – Caleb Commented Nov 18, 2024 at 14:57
  • @Caleb I thought these were completely different website haha. Only just figured out it's actually the same company! Sorry! – Casper Boon Commented Nov 18, 2024 at 21:00
Add a comment  | 

1 Answer 1

Reset to default 0

So after some coding, and some testing and stuff I finally got it working. I noticed that the Term description was being stored in the database as escaped HTML entities (<br>), which means WordPress is storing the <br> tags as text, not as actual HTML.

So I wrote some code to decode the HTML entities to actually bring back the proper
tags!

<?php
// Temporarily remove all filters on term_description
remove_all_filters('term_description');

// Get terms associated with the post
$terms = get_the_terms(get_the_ID(), 'locatie'); 
if ($terms && !is_wp_error($terms)) :
    foreach ($terms as $term) :
        // Decode HTML entities to get actual <br> tags
        $decoded_description = html_entity_decode($term->description);
        ?>
        <p>
            <b><?php echo esc_html($term->name); ?></b><br>
            <?php echo wp_kses_post($decoded_description); ?><br>
            <?php echo esc_html($term->adresveld); ?>
        </p>
    <?php endforeach;
endif;

// Re-add the filters for term_description if needed
add_filter('term_description', 'wpautop');
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269025a1320.html

最新回复(0)