1

I already checked this solution, but I still don't know why I'm getting a syntax error for this code:

<?php 
 $args = array('post_type' => 'posts', 'posts_per_page' => 3, 'order'
 => 'ASC', 'orderby' => 'title');

 //$posts = new WP_Query($args);
  if (have_posts()){
  while ( have_posts() ) : the_post();
  get_template_part( 'content', get_post_format());

  //if ($posts->have_posts()) : while($post->have_posts()) : $posts->the_post();

  echo "<span class=\"post-date\">get_the_date()</span>";
  echo "<a href=".get_permalink()."><h2 class='post-title'>".get_the_title()."</h2></a>";
  echo the_post_thumbnail();
  echo get_the_excerpt(); 
  echo "<a href=".get_permalink()."><span class='read-more'>
  Read More >></span></a>";


  endwhile;
}
?>

The one I'm having trouble with is the first echo: get_the_date(). All I'm trying to do is wrap the function in a span so that I can change the color of the date.

The error message I'm getting is: parse error, expecting ', ", or ';'

enter image description here

(Sorry the error message goes away when I try to take a screenshot). Please let me know if you know how to fix this, thanks.

Community
  • 1
  • 1
HappyHands31
  • 4,001
  • 17
  • 59
  • 109

2 Answers2

3

Another way, but something that may help you a lot if you don't already know - the below works :

 $args = array('post_type' => 'posts', 'posts_per_page' => 3, 'order'
 => 'ASC', 'orderby' => 'title');

 //$posts = new WP_Query($args);
  if (have_posts()){
      while ( have_posts() ) : the_post();
          get_template_part( 'content', get_post_format()); ?> // <- here closing php

          <span class="post-date"><?php echo get_the_date();?></span>
          <a href="<?php echo get_permalink();?>">
              <h2 class='post-title'><?php echo get_the_title(); ?></h2>
          </a>
          <?php the_post_thumbnail();echo get_the_excerpt(); ?> // <- here opening + closing php
          <a href="<?php echo get_permalink();?>"><span class='read-more'>Read More >></span></a>

         <?php
      endwhile;
  }

Some tips about wordpress :

Difference between the_permalink() and get_permalink() is that the first one echoes the value instantly. But with the get_* you can store it in a variable.

Same happens for :

the_title() - get_title()

Also

<?= get_permalink();?> where <?= means echo.

Generally you should read the difference between " and '.

Reference here

Community
  • 1
  • 1
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
2

So your main problem here is that your function get_the_date() isn't being concatenated into the string as it should.

Replace line 244 with:

echo "<span class='post-date'>".get_the_date()."</span>";
Frits
  • 7,341
  • 10
  • 42
  • 60