dragon19720808 2017-09-30 13:05
浏览 64

如何在精选图片中叠加帖子类型标题

I am using this WP_Query to get the title and the featured images from my post-types.

 <div class="job-cont">
      <?php 
      $query = new WP_Query( array( 'post_type' => 'jobs' ) );

    if ( $query->have_posts() ) :

       while ( $query->have_posts() ) : $query->the_post(); ?>
        <a href="<?php the_permalink (); ?>">
                    <h1 class="the_job_title"><?php the_title(); ?></h1>
                    <img class="the_job_image" src=<?php the_post_thumbnail ();?>
                </a>    
      <?php  endwhile;

    endif;
        ?>
    </div>

Now, with CSS wanna give styles and bring the title in front of each featured image. But position: relative; it doesn't work (each one respect each place). By position:absolute; All the titles of the post-types goes to the same place in the page. I've search if via CSS I can call each array position and give it own position, but I think that it isn't possible.

    .job-cont {
        position:relative;
        height:40%;
        width: 30%;
        z-index: 1;
    }
    .job-cont a {
        text-decoration: none !important;
    }

    .the_job_title {
        overflow: hidden;
        float: right;
        z-index:3;
    }

    .the_job_image {
        max-height: 100%;
        max-width: 100%;
        position:relative;
        filter: brightness(72%);
        display: inline-block;
        z-index:2;
    }  

Maybe there is a way?

  • 写回答

1条回答 默认 最新

  • duan1930 2017-09-30 15:25
    关注

    It happens because you are referring absolute positioned elements to the same relative container, so every title gets the same absolute position.

    Try this:

     <div class="job-cont">
      <?php 
      $query = new WP_Query( array( 'post_type' => 'jobs' ) );
    
    if ( $query->have_posts() ) :
    
       while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="title_image_container">
            <a href="<?php the_permalink (); ?>">
                    <h1 class="the_job_title"><?php the_title(); ?></h1>
                    <?php the_post_thumbnail();?>
                </a> 
        </div>   
      <?php  endwhile;
    
    endif;
        ?>
    </div>
    

    CSS

    .title_image_container {position: relative}
    

    You can also put the image as background of the container if you want.

    This way:

    <div class="job-cont">
      <?php 
      $query = new WP_Query( array( 'post_type' => 'jobs' ) );
    
    if ( $query->have_posts() ) :
    
       while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="title_image_container" style="background:url('<?php the_post_thumbnail_url();?>') center no-repeat">
            <a href="<?php the_permalink (); ?>">
                    <h1 class="the_job_title"><?php the_title(); ?></h1>
                </a> 
        </div>   
      <?php  endwhile;
    
    endif;
        ?>
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图