dongtui4038 2014-11-19 17:41
浏览 49
已采纳

存档页面上的Wordpress if / else语句

For a website I am working on I've tried to do some modifications on a exciting wordpress theme (revera). The original code displayed posts on archive pages with only a thumbnail image and the post title. The following is the original code:

<?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; ?>

Now I have made a if/ else statement in the archive.php (display below) so the archive page of some specific categories shows another code than the above mentioned.

Now, the problem is that the archive is not displaying the original code correctly anymore (archive page). The code if else statement I've made is the following (the error should be in the part after "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;

}?>

The page (http://tinyurl.com/of2xslw) is showing the thumbnail correctly but the url of the single post to which the thumbnail should link is displayed above the thumbnail. The thumbnail links to the archive page instead of the single post page, and the title of the post (with permalink to the single post) is not displaying below the thumbnail. Instead of that, a strange url appears. The page should look like this

What is going wrong here? I hope someone can help me.

  • 写回答

2条回答 默认 最新

  • doutou7740 2014-11-19 17:49
    关注

    This is an easy one:

    You shouldn't echo the_permalink() and the_title() as they already echo their respective results:

    <?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;
    
    }?>
    

    Attention the code might not work because I don't have the library aq_resize and therefore I can't test it properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)