I am new to PHP and for the development of a WordPress theme I need to re-write the following line of php/html code so that I can use it in my functions.php. I found out that I would need to rewrite it as an “echo” call, but I am always getting an error because my syntax is wrong.
This is the line we’re talking about:
<div <?php post_class( 'brick_item ' . $termString ) ?> onclick="location.href='<?php the_permalink(); ?>'">
I’ve tried several times, e.g.
echo '<div class="'. post_class("brick_item" . $termString); .'" onclick=location.href="'. the_permalink() .'">';
but I am doing something wrong in encapsulating things I guess.
EDIT: As requested, the part of the functions.php
function get_latest_posts() {
echo '<div class="latest-posts">';
echo '<div class="brick_list">';
$args = array(
post_type => 'post',
cat => '-3,-10',
posts_per_page => 3
);
$latestposts_query = new WP_Query($args);
if ( $latestposts_query->have_posts() ) : while ( $latestposts_query->have_posts() ) : $latestposts_query->the_post();
echo '<div '. post_class( $termString ) .' onclick=location.href='. the_permalink() .'>';
endwhile; else :
get_template_part('template_parts/content','error');
endif;
wp_reset_postdata();
echo '</div>';
echo '</div>';
}
add_shortcode( 'get_latest_posts', 'get_latest_posts' );
Advertisement
Answer
There is a semicolon in the middle of your line
echo '<div class="'. post_class("brick_item" . $termString);
.'" onclick=location.href="'. the_permalink() .'">';
should be
echo '<div class="'. post_class("brick_item" . $termString) .'" onclick=location.href="'. the_permalink() .'">';
semicolons signify end of line in php, thus your code did first execute
echo '<div class="'. post_class("brick_item" . $termString);
which is fine, but only half of what you want. Then php tries executing
.'" onclick=location.href="'. the_permalink() .'">';
but doesn’t know what to do with a dot at the start of the line. Dot means append string before to string after, but there is nothing before, so it’s a compile error. You can also just add another echo to the second line instead of the dot
echo '" onclick=location.href="'. the_permalink() .'">';