【问题标题】:How to add Title Attribute to Wordpress Image on Attachment Page in TwentyEleven Theme?如何在 TwentyEleven 主题的附件页面上为 Wordpress 图像添加标题属性?
【发布时间】:2013-12-04 16:18:20
【问题描述】:

这是一个与在附件页面上添加标题属性相关的 WP/SEO 问题。我们使用的是 211 主题和默认的 image.php 代码,如下所示。

我们想要拉入标题标签(甚至自动为所有图像设置标题,因此用户不必单独设置,或者只是使用帖子标题作为标题 - 任何这些)。

查看源代码中的 html 如下所示:

image.php 代码:

<a href="<?php echo esc_url( $next_attachment_url ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment"><?php

                                $attachment_size = apply_filters( 'twentyeleven_attachment_size', 848 );

                                echo wp_get_attachment_image( $post->ID, array( $attachment_size, 1024 ) ); // filterable image width with 1024px limit for image height.

                                ?></a>

【问题讨论】:

    标签: php css wordpress seo


    【解决方案1】:

    我还希望我的所有附件都自动具有标题属性,所以我编写了以下 PHP 代码:

    <?php if(wp_attachment_is_image($post->id)) : $att_image = wp_get_attachment_image_src($post->id, "full"); ?><p class="attachment"><a class="show_image" rel="bookmark" href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php echo get_the_title(); ?>"><img class="img-responsive attachment-medium" src="<?php echo $att_image[0]; ?>" height="<?php echo $att_image[2];?>" width="<?php echo $att_image[1];?>" alt="<?php echo get_the_title(); ?>"></a></p><?php endif; ?>

    此 PHP 代码已在 WordPress 3.7.1 上进行了测试。在我的Web Development & Design Blog

    上可以找到在自定义 WordPress image.php 文件中使用此代码的实时示例

    【讨论】:

    • 非常感谢 - 这太棒了!
    • 请务必将我提供的答案标记为您问题的答案并投票。
    • @GiancarloColfer:我应该把这段代码放在我的主题中的什么位置?
    • @shihon 查看我的另一个答案以获取更多信息stackoverflow.com/questions/20287058/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多