在WordPress循环中自定义每个帖子的图像src

时间:2017-07-24 18:24:05

标签: wordpress loops

所以我有一个WP_Query循环浏览我的自定义帖子类型,我希望根据循环中的帖子自定义我的图片代码的src。实现这一目标的最简洁方法是什么。

将此视为我的循环在每次迭代时吐出的内容:

            <div class="partnerships p-section">
                <div class="-display-inlineBlock partnerships icon">
                  <img style="min-width:71px;" src="<?php bloginfo('template_url')?>/images/icons/trainingAndDevelopment.svg">
                </div>
                <div class="-display-inlineBlock" style="width:70%">
                    <h1><?php the_title();?></h1>
                    <hr>
                   <p><?php the_content(); ?>
                   </p>
                </div>
            </div>

1 个答案:

答案 0 :(得分:0)

为什么你不使用缩略图?

<div class="partnerships p-section">
    <div class="-display-inlineBlock partnerships icon">
      <img style="min-width:71px;" src="<?php the_post_thumbnail_url(); ?>">
    </div>
    <div class="-display-inlineBlock" style="width:70%">
        <h1><?php the_title();?></h1>
        <hr>
       <p><?php the_content(); ?>
       </p>
    </div>
</div>

编辑:

然后你需要允许上传SVG。

但是警告:允许上传SVG不利于安全性 (看看:https://secupress.me/blog/add-svg-support-in-wordpress-medias-yes-but-no/

所以不要使用以下代码,随处可见。

function custom_mtypes( $m ){
    $m['svg'] = 'image/svg+xml';
    $m['svgz'] = 'image/svg+xml';
    return $m;
}
add_filter( 'upload_mimes', 'custom_mtypes' )

第一个解决方案

我不知道如何在上传时清理SVG,幸运的是一个插件可以完成所有存在的操作。这个插件还修复了svg。

的其他一些问题

https://wordpress.org/plugins/scalable-vector-graphics-svg/

第二个解决方案

如果只有admin需要上传svg

您可以使用以下代码:

function custom_mtypes( $m ){
    if(get_current_user_id()==1){
        $m['svg'] = 'image/svg+xml';
        $m['svgz'] = 'image/svg+xml';
        return $m;
    }
}

add_filter( 'upload_mimes', 'custom_mtypes' );

如果你说法语,这是一个关于这个主题的有趣讨论 https://wpchannel.com/autoriser-envoi-fichiers-svg-wordpress/(阅读评论,不要在本文顶部使用代码)

相关问题