[英]How to rewrite default php code to a valid echo line output?
我是 PHP 的新手,为了开发 Wordpress 主题,我需要重新编写以下 php/html 代码行,以便可以在我的函数中使用它。ZE1BFD762321E409CEE4AC0B6E841963C 我发现我需要将它重写为“echo”调用,但我总是收到错误,因为我的语法错误。
这是我们正在谈论的行:
<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() .'">';
但我在封装我猜的东西时做错了。
编辑:根据要求,功能的一部分。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' );
让我们看看这对我们有什么帮助,因为我已经清理了一些代码。 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' );
你的行中间有一个分号
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().'">';
但不知道如何处理行首的点。 点表示 append 字符串之前到字符串之后,但之前没有任何内容,所以这是一个编译错误。 您也可以在第二行添加另一个回声而不是点
echo '" onclick=location.href="'. the_permalink().'">';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.