我正在尝试用WordPress循环替换下面代码块中的字符串“Something Good will show up here”,但它似乎不起作用。我希望在单击Link1时,让ID为“intro段落”的帖子显示出来。如何正确地做到这一点?
<?php
$longString = "Something Nice will show up here";
$link=$_GET[\'link\'];
if ($link == \'1\'){
echo $longString;
}
if ($link == \'2\'){
include \'page2.php\';
}
if ($link == \'3\'){
include \'page3.php\';
}
if ($link == \'4\'){
include \'page4.php\';
}
?>
像这样
<?php
$longString = "<?php
$query = new WP_Query(\'category_name=intro-paragraphs\');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<div class="mid-box">
<h1>
<?php the_title(); ?>
</h1>
<?php echo excerpt(40); ?><br/>
<a href="<?php echo get_permalink(); ?>">» read more...</a> </div>
<!-- End Mid-Box -->
<?php endwhile; ?>
<?php endif; ?>";
最合适的回答,由SO网友:Chris_O 整理而成
不能在字符串中执行PHP代码但是,您可以定义一个执行代码的函数,并在if子句中调用该函数function dz_slick_longstring() {
$query = new WP_Query(\'category_name=intro-paragraphs\');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<div class="mid-box">
<h1><?php the_title(); ?></h1>
<?php echo excerpt(40); ?><br/>
<a href="<?php echo get_permalink(); ?>">» read more...</a>
</div>
<!-- End Mid-Box -->
<?php endwhile; ?>
<?php endif; ?>
<?php
}
然后,不要这样做:
if ($link == \'1\'){
echo $longString;
}
执行以下操作:
if ($link == \'1\'){
dz_slick_longstring();
}