Escape ampersands in Custom Post Type loop titles (2 posts)

  1. dkristine
    Posted 6 years ago #

    I created a custom post type for a restaurant called "menu" and am displaying the menu in a table like so:

    <?php $loop = new WP_Query( array( 'post_type' => 'menus', 'orderby'=> menu_order, 'category_name'=> 'dinner', 'posts_per_page' => -1) ); ?>
          <table id="yoshi-menu">
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
         <tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>">
         <td class="image">
         <?php the_title(); ?></td>
         <td class="desc">
         <?php $values=get_post_custom_values('description'); echo $values[0];?>
         <td class="price-1">
         <?php $values=get_post_custom_values('price'); echo $values[0]; ?>
         <td class="image">
         <a rel="lightbox" href="<?php $big = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array( 1000,1000 ), false, '' );?><?php echo $big[0]; ?>"><?php the_post_thumbnail(array (60,60)); ?></a>
    <?php endwhile; ?>

    The individual posts validate fine, but if the client puts an ampersand "Steak & Prawns" in the title, this template returns validation errors. I have tried using html special characters, but then the titles simply return & amp ; literally (without spaces). Not sure why this is only happening in this particular template while 'single-menu.php' validates fine with an ampersand.

    Any help would be appreciated! I am a php newbie :)


  2. curlybracket
    Posted 6 years ago #

    Hi Danielle,

    I guess you should escape all strings using the htmlspecialchars function http://php.net/manual/en/function.htmlspecialchars.php and respectively http://php.net/manual/en/function.htmlspecialchars-decode.php

    Hope this helps.

Topic Closed

This topic has been closed to new replies.

About this Topic