【问题标题】:Set background-image css to the featured image in wordpress using jQuery使用 jQuery 将背景图像 css 设置为 wordpress 中的特色图像
【发布时间】:2012-04-10 14:46:22
【问题描述】:

供参考 http://vvcap.net/db/Bs03ucQSrylV5LBiWduY.htp 这是帖子特色图片的缩略图 http://vvcap.net/db/_45qWkuQwluTrYF51tFn.htp 这是一个我想设置背景图像的正方形。小方块由后循环制成(如下所示)。循环内部是检查帖子是否为“特色图片吸引”

CSS: 目前这是唯一一个设置 div bg 样式的 CSS。 (这应该被替换)

 section > div { background:#00c8e8;}

编译的 HTML 源代码 第二部分发布了一个特色图片(在 div 顶部)作为示例。第一部分没有特色图片。

<section  class="resource">
   <div> 
        <a href="http://localhost/dov1/?custom_type=logo-design" rel="bookmark" title=" Logo Design">Logo Design</a>
  </div>
</section>

<section  class="resource">
  <div> 
        <a href="http://localhost/dov1/?custom_type=test" rel="bookmark" title=" Magazine Spread">Magazine Spread</a>

            <img width="125" height="125" src="http://localhost/dov1/wp-content/uploads/2012/03/project2_web-125x125.jpg" class="attachment-post-thumbnail wp-post-image" alt="project2_web" title="project2_web" />     
 </div>
</section>

HTML/PHP

<?php 

        $args = array(  
        'post_type' => 'custom_type', 
        'posts_per_page' => '-1' 
        );

        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <section  class="resource">
              <div> 
          <a href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ?>">
                <?php the_title(); ?>
                <?php 
            // check if the post has a Post Thumbnail assigned to it. 
        if ( has_post_thumbnail() ) 
            { 
          //run jQuery here (prob replace the_post_thumbnail();)
              the_post_thumbnail();
        } 
        ?>
    </a> </div>
</section>
<?php endwhile; ?>

我不是很擅长 jQuery 或 php。幸运的是,这个过程的逻辑是有道理的。

  1. 循环发布帖子(检查)
  2. 测试帖子是否有特色图片(检查)
  3. 执行jQuery,用the_post_thumbnail()替换背景图片;
  4. 利润!

我们遇到的另一个问题是我们如何区分和分配正确的背景图像?我只有 1 种适合 css 样式的表单。如果它处理每个条目的过程,那将是有意义的。不幸的是,它不会重写每个条目吗?我知道这是一个很大的问题,但我会永远爱你的帮助!

马修

更新的 HTML 源代码

   <section  class="resource">

      <div> 

            <a href="http://localhost/dov1/?custom_type=test" rel="bookmark" title=" Magazine Spread">Magazine Spread</a>

            <img width="125" height="125" src="http://localhost/dov1/wp-content/uploads/2012/03/project2_web-125x125.jpg" class="attachment-post-thumbnail wp-post-image" alt="project2_web" title="project2_web" />      </div>

    </section>

【问题讨论】:

  • 要使用 jQuery,我们需要查看(相关)呈现的 HTML(“查看源代码”)。
  • 很抱歉它在那里。编译源码下
  • 如果你愿意,我可以发布所有的源代码吗?如果您愿意,我会更新整个页面的源代码,但这就是帖子的内容
  • 我确实删除了 the_post_thumbnail();如果这会导致任何混乱,我会在此处将其添加到 cmets 中。
  • 特色图片是div 中唯一的img 元素?并且图像应该成为div 本身的背景图像? img 应该随后被删除吗?

标签: php javascript jquery css wordpress


【解决方案1】:

假设每个section 元素只有一个img 元素,并且您希望 图像成为div 的父divbackground-imageimg

$('section img').each(
    function(){
        var src = this.src,
            h = $(this).height(),
            w = $(this).width();
        $(this).closest('div').css({
            'min-width' : w,
            'min-height' : h,
            'background-image' : 'url(' + src + ')',
            'background-repeat' : 'no-repeat',
            'background-position' : '50% 50%'
        });
    }).remove();​

JS Fiddle demo.

参考资料:

【讨论】:

  • 它有效!但是, .remove 准确地删除了它们。我通过拿走 .remove(); 来测试这个。它显示了两者。不幸的是,当我删除 the_post_thumbnail();它带走了两者。关于如何获得叠加图像但保留背景图像的任何想法?
  • ".remove() 确实删除了它们..." 什么?使用 Chromium 17、Firefox 11 和 Opera 11.61(都在 Ubuntu 11.04 上)不会重现您似乎所描述的问题。在这些浏览器中,您可以获得背景图像并删除 img 元素。你期待/希望发生什么?
  • 是 PHP 造成了问题 the_post_thumbnail();你的代码很棒!只是在另一个上获得 2 张图片
  • 我将在上面重新发布源代码,向您展示 php 正在做什么。所以我们也许可以写另一行来删除它。
  • 你解决了什么问题?如何?可能值得将其作为实际答案发布,以便这篇文章可以帮助该网站的未来用户。
【解决方案2】:

通过Codex 的外观,您可以将该信息作为第二个参数传递。

CSS

.post-thumbnail {
    background:#00c8e8;
}

PHP

$size = array( 32, 32 );
$attr = array( 'class' => 'post-thumbnail' );
the_post_thumbnail( $size, $attr );

为了使帖子缩略图成为背景,我会使用 get_the_post_thumbnail()

<?php $bg = wp_get_attachment_image_src( get_post_thumbnail_id($post_id, $size, $attr)); ?>
<div style="background: <?php echo $bg; ?>;">...</div>

您必须将所需的参数传递给它 - 特定的帖子 ID 或其他。

【讨论】:

  • 我相信我链接你的代码有一些混乱.. 现在 the_post_thumbnail();坐在盒子的顶部vvcap.net/db/E8hsVqoS-XW-yilykDId.htp 这是一张更好的照片。我尝试了代码,但似乎不起作用。也许我只是放错了?
  • 所以你是说你希望它是一个 div bg 而不是一个图像标签?
  • 是的,是的!经过大多数分析后,我认为这将是一个相当大的过程。我必须循环所有帖子然后使用 jquery 替换每个帖子的 css?这种方法是有道理的,但似乎真的很难。你知道如何完成这个过程吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 2010-10-05
  • 1970-01-01
  • 2017-10-06
  • 2013-12-19
  • 1970-01-01
相关资源
最近更新 更多