0

我正在尝试回显包含 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 循环已经设置好了。

我希望你们能帮助我。

亲切的问候 Dragon54

4

3 回答 3

1

就像是:

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(打印)

于 2012-06-24T16:51:17.293 回答
0

请学习串联,在编写 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>';
?>
于 2012-06-24T16:51:29.263 回答
0

你需要echo它:

<div id="gallery_<?php echo $c; ?>">

另外,ID 中不允许有空格,我将其切换为下划线。

于 2012-06-24T16:52:11.350 回答