【问题标题】:How do I add A Div around each WordPress post image ? [closed]如何在每个 WordPress 帖子图像周围添加一个 Div? [关闭]
【发布时间】:2014-08-20 11:00:00
【问题描述】:

我想在我的 wordpress 博客文章中发布的每张图片周围添加一个 div。 我该怎么做?

(对于好奇的人,我试图在用户将鼠标悬停在图像上时弹出一些分享按钮)。

【问题讨论】:

  • 你现有的帖子有多少,每个帖子大概有多少张图片?最简单的方法可能是在每个帖子中的每个图像周围手动编码<div class='yourClass'></div>。这是一项相当容易的任务吗?

标签: wordpress wordpress-theming


【解决方案1】:

将以下 sn-p 代码添加到您的 functions.php 文件中:

add_filter( 'image_send_to_editor', 'wp_image_wrap_init', 10, 8 );    
    function wp_image_wrap_init( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
    return '<div id="wp-image-wrap-'. $id .'" class="wp-image-wrap">'. $html .'</div>';
}

【讨论】:

  • 它并没有改变我的图像。
  • 我的代码整理得更长 btw jsfiddle.net/vzurqnvo
  • 我测试了这段代码,应该基于默认的 Wordpress 工作吗?这将使用 div 包装所有默认的帖子图像。如果图像是由自定义函数生成的,这可能不起作用。
  • 默认 wordpress,二十四。我正在处理这个,这还不错wordpress.stackexchange.com/questions/36000/…我会更新:)
  • 我发布了最终答案 :) 非常感谢您的帮助!
【解决方案2】:

所以这解决了一切:)! 谢谢你们在这里的帮助。我希望它对其他人有用。

function breezer_addDivToImage( $content ) {
   // A regular expression of what to look for.
   $pattern = '/(<img([^>]*)>)/i';
   // What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
   $the_url = the_permalink();
   $replacement = '<div class="imgWrap"> 
                        $1
                        <div class="imgDescription">
                                            <div class="theShareLinks">
                                                <img src="http://localhost/mysite/wp-content/uploads/2014/08/dfc2.png" />
                                                <a href="http://twitter.com/share?text=&url='.get_the_permalink() .'" class="img-twitter" title="Share on Twitter" target="_blank"></a>
                                                <a href="http://www.facebook.com/sharer.php?u='.get_the_permalink() .'?" class="img-facebook" title="Share on Facebook" target="_blank" onclick="window.open(this.href, \'newwin\', \'width=500, height=200\'); return false;" ></a>
                                                <a href="https://plus.google.com/share?url='.get_the_permalink() .'" class="img-google" title="Share on Google" target="_blank"></a>
                                            </div>
                                        </div>
                    </div>';

   // run preg_replace() on the $content
   $content = preg_replace( $pattern, $replacement, $content );

   // return the processed content
   return $content;
}

add_filter( 'the_content', 'breezer_addDivToImage' );

【讨论】:

【解决方案3】:

如果它像您想象的那样简单,只需切换到文本编辑器,或禁用可视化编辑器,然后在您插入的每个图像周围添加 div。为它们提供相同的类,然后在样式表中添加该类的样式。

-- 或者,如果您想将此应用到您的 Wordpress 安装中的所有类型的页面(页面、帖子、档案、类别等):--

深入了解 FTP,并开始查看您的主题文件夹和文件。

您有不同的页面用于不同的视图。 attachment.php、page.php 等。根据您要更改的视图,这些文件将包含如下所示的一行:

<p class="attachment">
    <a href="<?php echo wp_get_attachment_url( $post->ID ); ?>" title="<?php the_title(); ?>" rel="attachment">
        <img src="<?php echo $att_image[0]; ?>" width="<?php echo $att_image[1]; ?>" height="<?php echo $att_image[2]; ?>" class="attachment-medium" alt="<?php $post->post_excerpt; ?>" />
    </a>
</p>

或任何提及的地方

<img src="<?php echo $att_image[0]; ?>" />

只需在其周围添加一个 div,然后在样式表中添加样式规则。

【讨论】:

  • 我不认为这是他要问的。他希望将div 包裹在添加到帖子中的任何图像上,而不是在附件页面上。
  • 这就是为什么我说“取决于您要更改的视图”
  • 正确,但帖子中没有内嵌图片的视图。
  • 实际上他可以将 CSS 从字面上应用到帖子中的 img 标签,但显然这可能有其自身的问题。
猜你喜欢
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 2013-03-03
  • 1970-01-01
相关资源
最近更新 更多