【问题标题】:Alt tag is blank for WordPress imagesWordPress 图像的 Alt 标记为空
【发布时间】:2018-02-01 12:58:18
【问题描述】:

我有一个问题,我无法准确地确定缺少什么来声明图像 alt 标记。网站页面张贴在这里 -

https://www.njoyn.com/blog/

我还发布了模板的代码。如果你能帮助解决这个问题,那就太好了。请在此处查看代码:

<?php
/**
 * Template Name: Blog
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */
get_header();
?>
<?php
$page_id = $wp_query->post->ID;
?>

<div id="bd">
    <div class="blog-sec">
    <div class="container blog-main">
       <h2>Blog</h2>
        <div class="blog_left">

           <?php

$args     = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
    while ($my_query->have_posts()):
        $my_query->the_post();


        $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
?>

            <div class="blog_blocks">
                <div class="blog_thumb"><a href="<?php
        echo get_permalink();
?>" ><img src="<?php
        echo $feat_image;
?>" alt=""></a></div>
                <div class="blog_content">
                    <h5><a href="<?php
        echo get_permalink();
?>" ><?php
        the_title();
?></a></h5>
                    <?php

        $content = substr(get_the_content(), 0, 115) . "...";
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
        echo $content;

?>
                   <a href="<?php
        echo get_permalink();
?>" class="readmore"> 
                        <?php
        if (get_bloginfo('language') === "en-US") {

            echo get_option('of_blogtext_eng');
        } else {

            echo get_option('of_blogtext_french');
        }
?>
                                    </a>
                </div>
            </div>
            <?php
    endwhile;
}
?>

        </div>
        <div class="blog_right">

            <?php
if (is_active_sidebar('blog')):
?>

            <?php
    dynamic_sidebar('blog');
?>

            <?php
endif;
?>
       </div>
    <div class="clear"></div>
    </div>
</div>
</div>
<?php
get_footer();
?>

请让我知道我需要将哪些元素添加到数组中,或者声明 Image 变量并在 ALT 标记之间编写 echo 语句。

【问题讨论】:

  • 我不明白。你是在问我们应该如何编码alt=""

标签: php wordpress image alt


【解决方案1】:

那里有一整段代码实际上并不需要:

$feat_image = wp_get_attachment_url(get_post_thumbnail_id($post-&gt;ID));

<img src="<?php
        echo $feat_image;
?>" alt="">

删除$feat_image 行并将第二位替换为

&lt;?php if ( has_post_thumbnail() ) the_post_thumbnail(); ?&gt;

使用the_post_thumbnail()函数将输出必要的HTML并自动填写替代文本,当然前提是在媒体库中的图片帖子上输入了文本。

希望有帮助

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多