【问题标题】:Wordpress if/else statement on archive page存档页面上的 Wordpress if/else 语句
【发布时间】:2015-01-17 08:35:33
【问题描述】:

对于我正在开发的网站,我尝试对令人兴奋的 wordpress 主题 (revera) 进行一些修改。原始代码在存档页面上显示帖子,只有缩略图和帖子标题。以下为原代码:

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <article class="col-sm-3 col-6 portbox post">
         <?php $thumb = get_post_thumbnail_id();    $img_url = wp_get_attachment_url( $thumb,'full' );     //get full URL to image (use "large" or "medium" if the images too big) $image = aq_resize( $img_url,     750, 500, true ); //resize & crop the image  ?>
         <?php if($image) : ?> <div class="hthumb">         <a href="<?php the_permalink(); ?>"><img     class="img-responsive" src="<?php echo $image ?>"/></a> </div> <?php endif; ?> <h3><a href="<?php     the_permalink(); ?>"> <?php the_title(); ?></a></h3> 
    </article>
<?php endwhile; ?>

现在我在archive.php中做了一个if/else语句(显示在下面),所以某些特定类别的存档页面显示了不同于上面提到的另一个代码。

现在,问题是存档不再正确显示原始代码(存档页面)。我所做的if else语句代码如下(错误应该在“else”之后的部分):

<?php
    if ( is_category( 'producten-diensten' )  ) {
        echo'
             the code for producten-diensten category ';

} else {
  //everything else

 while ($wp_query->have_posts()) :
     $wp_query->the_post();
     echo'<article class="col-sm-3 col-6 portbox post">';
     $thumb = get_post_thumbnail_id();
     $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
     $image = aq_resize( $img_url, 750, 500, true ); //resize & crop the image
     if($image) : ;
        echo'<div class="hthumb"><a href="' . the_permalink() . '"><img class="img-responsive" src="' . $image . '"/></a></div>';
     endif;
     echo'<h3><a href="' . the_permalink() . '">' . the_title() . '</a></h3>';
     echo'</article>';
 endwhile;

}?>

页面 (http://tinyurl.com/of2xslw) 正确显示缩略图,但缩略图应链接到的单个帖子的 URL 显示在缩略图上方。缩略图链接到存档页面而不是单个帖子页面,并且帖子的标题(带有指向单个帖子的永久链接)未显示在缩略图下方。取而代之的是一个奇怪的网址。页面应该是这样的

这里出了什么问题?我希望有人可以帮助我。

【问题讨论】:

    标签: php wordpress if-statement


    【解决方案1】:

    这很简单:

    您不应该回显 the_permalink() 和 the_title(),因为它们已经回显了各自的结果:

    <?php
        if ( is_category( 'producten-diensten' )  ) {
            echo'
                 the code for producten-diensten category ';
    
    } else {
      //everything else
    
     while ($wp_query->have_posts()) :
         $wp_query->the_post();
         echo'<article class="col-sm-3 col-6 portbox post">';
         $thumb = get_post_thumbnail_id();
         $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
         $image = aq_resize( $img_url, 750, 500, true ); //resize & crop the image
         if($image) : ?>
            <div class="hthumb"><a href="<?php the_permalink(); ?>"><img class="img-responsive" src="<?php echo $image; ?>"/></a></div>';
         <?php endif;?>
              <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
         <?php echo'</article>';
     endwhile;
    
    }?>
    

    注意代码可能无法正常工作,因为我没有库 aq_resize,因此无法正确测试。

    【讨论】:

    • 感谢您的帮助!试过了,但不幸的是它不起作用。 link 第 224 行出现错误。似乎永久链接链接到存档页面而不是单个帖子。单个帖子标题也不显示。
    • @LarsLenselink 我在复制粘贴时搞砸了,现在试试吧!
    • 我们快到了!帖子标题现在可以正确显示(check),但有些图像如何链接到存档页面,并将它实际链接到的链接放在图像上方。当我检查源代码时,图像的 href 为空。很奇怪。
    • 哇,好用!唯一不正确的是';在图像 div 后面。我已经删除了它,现在一切看起来都很好。非常感谢!
    • @LarsLenselink 是的,我说过我无法正确调试它,因为我没有您正在使用的一些库,例如 aq_resize()。很高兴看到它工作,干杯!
    【解决方案2】:

    最终正确的代码是(感谢 nunorbatista):

    <?php
        if ( is_category( 'producten-diensten' )  ) {
            echo'
                 the code for producten-diensten category ';
    
    } else {
      //everything else
    
     while ($wp_query->have_posts()) :
         $wp_query->the_post();
         echo'<article class="col-sm-3 col-6 portbox post">';
         $thumb = get_post_thumbnail_id();
         $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
         $image = aq_resize( $img_url, 750, 500, true ); //resize & crop the image
         if($image) : ?>
            <div class="hthumb"><a href="<?php the_permalink(); ?>"><img class="img-responsive" src="<?php echo $image; ?>"/></a></div>';
         <?php endif;?>
              <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
         <?php echo'</article>';
     endwhile;
    
    }?>
    

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 2021-03-22
      • 1970-01-01
      • 2019-04-09
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      相关资源
      最近更新 更多