我正在尝试回显一个包含html代码和PHP代码的混合字符串,但我尝试的所有可能的事情都没有成功。
基本上我想做的是循环以下代码 3 次。每次给我 3 个div,名称中都有数字递增。
此代码将在wordpress模板中使用。
我没有回显的正常代码如下:
<div id="gallery <?php $c; ?>">
<?php query_posts('post_type=portfolio'); ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<h1><?php the_title(); ?></h1>
<a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>" >
<h2>View Project</h2>
</a>
<?php the_content() ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
我对PHP相当陌生,所以我不知道如何以正确的方式回应这一点。我的 for 循环已经设置好了。
我希望你们能帮助我。
亲切问候龙54
像这样:
for($i=1; $<=3; $++){
echo '<div id="gallery gallery_'.$i.'">' . get_the_permalink() . '</div>';
}
应该做这个技巧(见 http://php.net/manual/de/language.operators.string.php)
但请注意,一些 Wordpress 助手函数直接打印/回显内容本身。大多数情况下,您会找到返回值的内容
看http://codex.wordpress.org/Function_Reference/get_permalink(退货)与http://codex.wordpress.org/Function_Reference/the_permalink(打印)
请学习串联,这是编写 PHP/HTML 时必须的!然后,您可以简单地将该块放入一个 foreach 或某种循环中,具体取决于您的新闻系统的设计方式。
<?php
echo '<div id="gallery_'.$c.'">';
query_posts('post_type=portfolio');
while(have_posts()) {
the_post();
if ( has_post_thumbnail() ) {
echo '<a href="'.the_permalink().'" title="'.the_title_attribute();.'" '.the_post_thumbnail().'</a>';
}
}
echo '.<h1>'.the_title().'</h1>
<a href="'.the_permalink().'" title="'.the_title_attribute().'">link</a>
</div>';
?>
echo
它:
<div id="gallery_<?php echo $c; ?>">
另外,ID 中不允许有空格,我将其切换为下划线。