PHP – warning: Illegal string offset in custom WordPress theme

I’m trying to fix a custom WordPress theme I didn’t write. My limited PHP knowledge can’t figure this out. My exhaustive search have found similar questions with most of the answers say the array is passing a string or wrong type. I just don’t know how to fix it, or maybe I just should suppress the warning?

Warning: Illegal string offset ‘the_link’ on line 39
Warning: Illegal string offset ‘subheader’ on line 58

Read More

Here’s the code in question. Any help would be greatly appreciated.

    <?php get_header(); ?>
    <?php wp_nav_menu( array( 'container_class' => 'header-bar','theme_location' => 'reviews' ) ); ?>

<div class="content wiemer-pages">
  <?php the_post(); ?>
  <div id="page-header-wrap">
<div id="page-header" class="no-shadow wreath">
  <?php the_post_thumbnail(); ?>
  <h1 class="page-title">
    <?php the_title(); ?>
  </h1>
  <div id="page-header-content">
    <?php $pageheader_metabox->the_meta(); 
$pageheader_metabox->the_value('title');?>
  </div>
  <div class="clear"></div>
</div>
  </div>
  <!-- [END] page-header-wrap + page header -->
  <div class="page-content">
<?php the_content(); ?>
<?php edit_post_link(); ?>
  </div>
  <?php   
$args=array(
   'post_type'=>'reviews',  
'posts_per_page'=>60
);
$catreview = new WP_Query($args);
global $post;
   ?>
  <?php if($catreview->have_posts()): ?>
  <h2 class="review-category" id="accolades">Recent Accolades </h2>
  <?php while ($catreview->have_posts()) : $catreview->the_post();?>
  <?php // load the meta     
        $mainurl='#';
    $meta= get_post_meta($post->ID, '_urls', true);
             $mainurl = $meta['the_link']; <!-- line 39 -->
     ?>

  <div class="review-item">
<div class="review-thumb-container">
  <?php // post thumbnail 
            if (has_post_thumbnail()){  ?>
  <a class="review-thumbnail" href="<?php echo $mainurl ?>">
  <?php the_post_thumbnail(array(144,180),array("title" => get_the_title($post->ID) . " - Thumbnail")); ?>
  </a>
  <?php } ?>
</div>
<div class="review-body">
  <h6>
    <?php the_time('F Y') ?>
    <?php //echo get_the_term_list( $post->ID, 'accolades', '| '); ?>
  </h6>
  <h3><a href="<?php echo $mainurl; ?>">
    <?php the_title() ?>
    </a></h3>
  <?php if ($sh = $meta['subheader']){ echo "<h4> $sh </h4>"; } ?>  <!-- line 58 -->
  <div class="review-content">
    <?php the_content(); ?>
    <?php edit_post_link( 'Edit', '<div class="edit-link">', '</div>' ); ?>
  </div>
  <?php if (isset($meta['link_title'])): ?>
  <ul class="url-list">
    <li> <a href="<?php echo $meta['the_link']; ?>" class="note-cat">
<?php echo $meta['link_title']; ?></a> </li>
  </ul>
  <?php endif; //urls ?>
</div>
</div>
<?php endwhile; ?>
<p><a class="to-top" title="Top of the page" href="#masthead">Top&uarr;</a></p>
<?php endif; //End if for Accolades ?>
</div>
<?php get_footer(); ?>

Related posts

1 comment

  1. I think your problem is that $meta isn’t an array because get_post_meta is currently returning a single value because the third parameter is set to true.

    Description of the third parameter in the WordPress documentation:

    $single
    (bool) (Optional) Whether to return a single value.

    Default value: false

    You are currently using:

    $meta = get_post_meta($post->ID, '_urls', true);
    

    The third parameter is set to true. So it will return a single value (not an array). So you can set it to false (or remove the third parameter because the default value is false) to return an array.

    You can find more info about the get_post_meta function here:
    https://developer.wordpress.org/reference/functions/get_post_meta/

    Info on the ‘Warning: Illegal string offset’ error:
    https://stackoverflow.com/a/20271518/3093142

Comments are closed.