【问题标题】:Wordpress edit <img> markup to include data attributeWordpress 编辑 <img> 标记以包含数据属性
【发布时间】:2016-01-06 07:03:01
【问题描述】:

我在 Wordpress 媒体上传器中添加了一个自定义字段,用于存储 Vimeo ID。我需要将此自定义字段数据(如果已输入)拉到 Wordpress 中默认 &lt;img&gt; 标记的标记中 - 我希望将其添加为 data- 属性。

在网上搜索后,我没有在这里尝试什么的线索,有人有这方面的经验吗?

如果存在上述data- 属性,我还想自动将“video-thumb”类添加到该图像。

我可以按如下方式调用 cusotm 字段,但不知道如何将其合并到 &lt;img&gt; 标签中:

$video_url = get_post_meta($id, 'video-url', true);

默认 Wordpress &lt;img&gt; 代码

<img class="aligncenter size-large wp-image-114" src="#" alt="" width="1024" height="435" />

期望的结果

<img class="video-thumb aligncenter size-large wp-image-114" src="#" alt="" width="1024" height="435" data-vimeo-id="69171201" />

【问题讨论】:

    标签: php jquery html wordpress image


    【解决方案1】:

    你可以在你的帖子循环中试试这个

    $key = 'your custom meta key'

    echo get_post_meta($post-&gt;ID, $key, true);

    全面实施

    <?php
    $query = new WP_Query('showposts=3');
    if ($query->have_posts()):
        while ($query->have_posts()):
            $query->the_post();
            $vimeo = get_post_meta($post->ID, 'your_key', true);
            the_title();
    ?>
    
    <img class="video-thumb aligncenter size-large wp-image-114" src="#" alt="" width="1024" height="435" data-vimeo-id="<?php echo $vimeo; ?>" />
    
    <?php
        endwhile;
    endif;
    wp_reset_query();
    ?>
    

    更多信息请阅读here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 2018-05-28
      • 1970-01-01
      • 2014-01-01
      相关资源
      最近更新 更多