Support

Account

Home Forums ACF PRO addslashes for a sub_field

Solved

addslashes for a sub_field

  • Hi together,

    I need your help for once.
    I have a sub-field (WYSIWYG) that is to be played out via schema.org JSON-LD.
    The field contains links to other URLs. And for these links the inverted commas (“) have to be masked.
    I tried this with the function addslashes. Unfortunately, this did not work.
    Here is my code example:

    
    <?php 
    	$fcount=count($faq);
    	if ( $faq ) : ?>
    	<script type="application/ld+json">
    	{
    		  "@context": "https://schema.org",
    		  "@type": "FAQPage",
    		  "mainEntity": [
    			<?php if ( have_rows( 'faq' ) ) : $ftemp=1; ?>
    				<?php while ( have_rows( 'faq' ) ) : the_row(); ?>
    				{
    					  "@type": "Question",
    					  "name": "<?php the_sub_field( 'question' ); ?>",
    					  "acceptedAnswer": {
    						"@type": "Answer",
    						"text": "<?php addslashes(the_sub_field( 'answer' ), '"'); ?>"
    						}
    						}<?php if($ftemp<$fcount){ ?>,<?php } ?>
    				<?php $ftemp++; endwhile; ?>
    			<?php else : ?>
    				<?php // no rows found ?>
    			<?php endif; ?>
    				]
    	}
    	</script>
    <?php endif; ?>
    

    Do any of you have an idea how I can solve the problem?

    Thanks a lot
    Michael

  • Hi,

    does no one have an idea how I can solve the problem?

  • I would try using htmlspecialchars().

    On the other hand if I was doing this I would probably built and array in PHP and then json_encode that array for output allowing PHP to deal with special characters.

    
    <?php 
      if ($faq) {
        $ld = array(
          '@context' => 'https://schema.org',
          '@type' => 'FAQPage',
          'mainEntity' => array()
        );
        if (have_rows('faq')) {
          while (have_rows('faq')) {
            the_row();
            $ld['mainEntity'][] = array(
              '@type' => 'Question',
              'name' => get_sub_field('question'),
              'acceptedAnswer' => array(
                '@type' => 'Answer',
                'text' => get_sub_field('answer')
              )
            );
          }
        }
        ?>
          <script type="application/ld+json"><?php echo json_encode($ld); ?></script>
        <?php 
      }
    ?>
    
  • Hi John,

    many thanks. This works fine.

    But how can I add a “read more” Link after each answer?

    Example: <p>Read more about <strong><a href="<?php the_permalink(); ?>"><?= the_title(); ?></a></strong></p>

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

You must be logged in to reply to this topic.