【问题标题】:Add links to images gallery添加图片库的链接
【发布时间】:2019-10-31 03:02:25
【问题描述】:

我需要将一些画廊图片链接到不同的外部网站。经过一些研究,我无法找到不使用插件的解决方案。在不使用插件的情况下,这可以在 wordpress 中实现吗?我无法为这个项目安装插件,所以任何建议都将不胜感激。

这是我的代码:

$args = array(
'post_type' => 'post',
'name' => 'partners'
);
$logo_img = new WP_Query( $args );
?>
<div class="container-fluid" id="">
  <div class="row" style="margin-top:1em;margin-bottom:1em;">
<?php if( $logo_img->have_posts() ): while( $logo_img->have_posts() ): $logo_img->the_post();
$logo_gallery = get_post_gallery_images( $post->ID );
if( $logo_gallery ): ?>
  <div class="col-sm-12 col-md-12 col-lg-12 text-center">
<?php foreach($logo_gallery as $logo ): ?>
    <img class="img-fluid" src="<?php echo $logo; ?>" alt="" width="60" id="partner-logo" style="margin:0 .5em 0 .5em;"/>
<?php endforeach; ?>
  </div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
  </div>
</div>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    根据我们谈论的图像数量,如果这些图像被命名,您可以使用一些逻辑根据名称创建一个 URL,如果它们没有命名并且数量不多,则可以重命名它们。

    这是一个例子;

    <?php
    // If there is a matching string, set a specific URL
    if (preg_match('/site1/',$logo)) { $set = "yes"; $link = "https://website1.com"; }
    if (preg_match('/site2/',$logo)) { $set = "yes"; $link = "https://website2.com"; }
    if (preg_match('/site3/',$logo)) { $set = "yes"; $link = "https://website3.com"; }
    
    // If there is no matching string, set a default URL
    if ($set !== "yes") { $link = "https://default.com"; }
    
    // Now encase your image in a URL
    echo "<a href='$link'>
    <img class='img-fluid' src='$logo' alt='' width='60' id='partner-logo' style='margin:0 .5em 0 .5em;'/>
    </a>";
    ?>
    

    顺便说一下,id='partner-logo' - id 应该是唯一的。

    【讨论】:

    • 图片是各个合作伙伴的标志,没有命名,他们会随着时间的推移增加数量,所以我认为我不能使用你建议的方法。主要问题是我不知道是否有任何钩子或 wordpress 过滤器可以用来添加链接。
    • 您需要为图像添加一些独特的东西。如果您无法重命名它们但它们在表格中被引用,则在表格中添加一个文本列并将图像的相应 URL 放在该列中。
    • 这是一个基于 wordpress 的网站,我根本无法控制表格或代码会发生什么。
    • 最后一次尝试:在您的 WordPress 上找到图片,它们将有一个唯一的名称。记下每个图像名称以及它们应重定向到的位置,然后使用我的编码示例。
    • 我会尝试,但有更好的方法不会违反 DRY 原则,并且如果在循环中使用也对 wordpress 更友好。
    猜你喜欢
    • 1970-01-01
    • 2022-07-20
    • 2021-12-29
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多