如何将默认的php代码重写为有效的回显行输出



我是PHP的新手,为了开发Wordpress主题,我需要重写下面一行PHP/html代码,以便在我的functions.PHP中使用它。我发现我需要把它重写为";回声;调用,但由于语法错误,我总是出错。

这就是我们谈论的路线:

<div <?php post_class( 'brick_item ' . $termString ) ?> onclick="location.href='<?php the_permalink(); ?>'">

我已经试过好几次了,例如

echo '<div class="'. post_class("brick_item" . $termString); .'" onclick=location.href="'. the_permalink() .'">';

但我想我在封装东西的过程中做错了什么。

编辑:根据要求,部分功能.pp

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' );

行中间有一个分号

echo '<div class="'. post_class("brick_item" . $termString);.'" onclick=location.href="'. the_permalink() .'">';

应该是

echo '<div class="'. post_class("brick_item" . $termString) .'" onclick=location.href="'. the_permalink() .'">';

分号表示php中的行结束,因此您的代码首先执行了

echo '<div class="'. post_class("brick_item" . $termString);

这很好,但只是你想要的一半。然后php尝试执行

.'" onclick=location.href="'. the_permalink() .'">';

但不知道如何处理行开头的点。Dot的意思是将字符串before附加到字符串after,但之前没有任何内容,所以这是一个编译错误。你也可以只在第二行添加另一个回波,而不是点

echo '" onclick=location.href="'. the_permalink() .'">';

让我们看看这会给我们带来什么,因为我已经清理了一些代码。div会一直挂在那里,所以我把永久链接放在里面。

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()) {
$thePost = $latestposts_query->the_post();
echo '<div ' . post_class($thePost) . ' onclick="location.href='' . the_permalink() . ''">' . the_permalink() . '</div>';
}
} else {
get_template_part('template_parts/content','error');
}
wp_reset_postdata();
echo '</div>';
echo '</div>';
}
add_shortcode( 'get_latest_posts', 'get_latest_posts' );

最新更新