dougourang1856 2016-08-12 13:59
浏览 11
已采纳

关于博客创建的CSS,WordPress,PHP问题

I am very new to WordPress and I am having this issue now. My code displays all images and blog contents but the problem here is, if there are no images to display the content gets auto aligned since there is CSS for this class.

I need the blog post should get the width:620 if there are no images to post and width:420 if there are images in loop. Help needed.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongzhuoxie1244 2016-08-12 14:34
    关注

    You coould add some classes to div containing post content, like for example with-media and no-media.

    To find if post has any media, you can use this code:

    $media = get_attached_media( 'image' );
     if (is_array($media) && count($media) > 0) {
    $classname = 'with-media';
    } else {
      $classname = 'no-media';
    }
    

    then print it in div:

    <div class="some-class another-class <?php echo $classname; ?>"...
    

    now, when you have it, you can add css definition to style.css:

    .no-media { width: 620px; }
    .with-media {width: 420px; }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM