我想给所有的照片添加alt属性!

时间:2020-03-14 作者:fantomas17

大家好,我找到你们了,我有一个动态网站,其中包含超过200000张照片的问题,这是一个新闻网站。该网站建立在WordPress平台上,并进行了许多更改以适应我们的业务。我徒劳地完成了他没有出现在现场的每张照片的属性alt。经过多次协商后,必须将代码添加到此帖子所附的代码中。我的问题是,应该添加什么代码,以及在这个文件中的什么位置?如果有人能帮助我,我将不胜感激,谢谢!

<?php
class home_main_post_below_list extends AQ_Block {
    //set and create block
    function __construct() {
        $block_options = array(
            \'name\' => \'Home main post with below list post\',
            \'size\' => \'span12\',
        );

        //create the block
        parent::__construct(\'home_main_post_below_list\', $block_options);
    }
    //create form
    function form($instance) {
        $titles = isset($instance[\'titles\']) ? esc_attr($instance[\'titles\']) : \'Recent Posts\';
        $number_show = isset($instance[\'number_show\']) ? absint($instance[\'number_show\']) : 5;
        ?>
        <p><label for="<?php echo $this->get_field_id(\'titles\'); ?>"><?php _e(\'Title:\', \'tl_back\'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id(\'titles\'); ?>" name="<?php echo $this->get_field_name(\'titles\'); ?>" type="text" value="<?php echo $titles; ?>" /></p>
        <p><label for="<?php echo $this->get_field_id(\'number_show\'); ?>"><?php _e(\'Number of posts to show:\', \'tl_back\'); ?></label>
            <input id="<?php echo $this->get_field_id(\'number_show\'); ?>" name="<?php echo $this->get_field_name(\'number_show\'); ?>" type="text" value="<?php echo $number_show; ?>" size="3" /></p>

            <label for="<?php echo $this->get_field_id(\'cats\'); ?>"><?php _e(\'Select categories to include in the recent posts list:\', \'tl_back\'); ?> 

                <?php
                $categories = get_categories(\'hide_empty=0\');
                echo "<br/>";
                foreach ($categories as $cat) {
                    $option = \'<input type="checkbox" id="\' . $this->get_field_id(\'cats\') . \'[]" name="\' . $this->get_field_name(\'cats\') . \'[]"\';
                    if (isset($instance[\'cats\'])) {
                        foreach ($instance[\'cats\'] as $cats) {
                            if ($cats == $cat->term_id) {
                                $option = $option . \' checked="checked"\';
                            }
                        }
                    }
                    $option .= \' value="\' . $cat->term_id . \'" />\';
                    $option .= \'&nbsp;\';
                    $option .= $cat->cat_name;
                    $option .= \'<br />\';
                    echo $option;
                }
                ?>
            </label>    
        <?php

    }

    //create block
    function block($instance) {

            extract($instance);

        $titles = apply_filters(\'widget_title\', empty($instance[\'titles\']) ? \'Recent Posts\' : $instance[\'titles\'], $instance, $this->id_base);


                if (!isset($instance["cats"])) {
            $cats = \'\';}

        // array to call recent posts.

        $themeloy_args = array(
            \'showposts\' => $number_show,
            \'category__in\' => $cats,
        );

        $themeloy_widget = null;
        $themeloy_widget = new WP_Query($themeloy_args);

        ?>
        <div class="widget post_list_medium_widget">
        <?php if (!empty($instance[\'titles\'])) {?><h3 class="widget-title"><span><?php echo $instance["titles"];?></span></h3><?php }?>
        <div class="widget_container">
        <div class="post_list_medium">
        <?php
        $i = 0;
        while ($themeloy_widget->have_posts()) {
           $i++;
           $themeloy_widget->the_post();
           $post_id = get_the_ID();
           $thumb = themeloy_get_thumbnail(get_the_ID());
            if ($i == 1) {          
                ?>   

                 <div class="list_item">

                    <div class="entry-thumb feature-item"> 
                    <a href="<?php the_permalink(); ?>"> 
                    <?php if ( has_post_thumbnail()) {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), \'feature-large\' );
    ?>
    <img class="cat-feature-large-img" src="<?php echo $image[0]; ?>"/>
<?php }else{echo \'<img class="cat-feature-large-img" src="\'.get_template_directory_uri().\'/images/demo/feature-large.jpg\'.\'">\';} ?>
                    </a>

                    <div class="cat-feature-large">
                    <?php $post_cat = get_post_custom_values(\'cat_themeloy_select\', get_the_ID());
         $post_cat = ($post_cat[0] != \'\') ? $post_cat[0] : of_get_option(\'blog_category_post\');
         if($post_cat == \'1\'){echo \'<p class="cat-slider">\'; echo the_category(\', \').\'</p>\';}?>
         </div>

                    <?php echo themeloy_post_type(); ?>
                    </div>
                 <h2><a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a></h2>

<?php echo themeloy_post_meta(get_the_ID());?>
    <p><?php echo themeloy_short_title(320, get_the_excerpt(\'\')); ?>  </p>
    </div>
     <div class="clear margin-buttons"></div>
                <?php }else{?>                  

                <div class="small-list-content">
            <?php
                                    $post_date = get_post_custom_values(\'date_themeloy_select\', get_the_ID()); 
                                    $post_date = ($post_date[0] != \'\') ? $post_date[0] : of_get_option(\'blog_date_post\');
                                 if($post_date == \'1\'){
     ?>
     <div class="feature_post_style">
                        <span class="post_date"><span class="date_number"><?php echo get_the_date(\'d\');?></span> <?php echo get_the_date(\'M\');?>
                        <i class="icon-caret-right feature-icon-right"></i>
                        </span>
                        <span class="post_time"><i class="icon-time"></i> <?php echo get_the_time(\'H:i\');?></span>
                        </div>
                        <?php }?>
                        <div class="feature-link feature-item">             
                       <a href="<?php the_permalink(); ?>" class="feature-link">
<?php if ( has_post_thumbnail()) {
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), \'medium-feature\' );
    ?>
    <img src="<?php echo $image[0]; ?>"/>
<?php }else{echo \'<img src="\'.get_template_directory_uri().\'/images/demo/medium-feature.jpg\'.\'">\';} ?>
                        </a>
                        <?php echo themeloy_post_type(); ?>
                        </div>

                       <div class="list_desc">
                           <h4 class="list_title"><a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                            <?php the_title(); ?>
                            </a></h4>

                   <?php echo themeloy_post_meta(get_the_ID());?>

</div>
<div class="clear"></div>                     
</div>

                <?php }}?>


      </div>
        </div>
        </div>

        <?php
        wp_reset_query();   

    }

        function update($new_instance, $old_instance) {
        return $new_instance;
    }


}

2 个回复
SO网友:Mikhail

如果当前代码管理所有块,使用图像替换使用wp_get_attachment_image_src 带硬编码输出img 标签,带echo wp_get_attachment_image() 将返回img 标记为字符串,默认alt为image,如果未设置则为空。

下面是您的代码更新后的想法描述。

<?php
class home_main_post_below_list extends AQ_Block {
    //set and create block
    function __construct() {
        $block_options = array(
            \'name\' => \'Home main post with below list post\',
            \'size\' => \'span12\',
        );

        //create the block
        parent::__construct(\'home_main_post_below_list\', $block_options);
    }
    //create form
    function form($instance) {
        $titles = isset($instance[\'titles\']) ? esc_attr($instance[\'titles\']) : \'Recent Posts\';
        $number_show = isset($instance[\'number_show\']) ? absint($instance[\'number_show\']) : 5;
        ?>
        <p><label for="<?php echo $this->get_field_id(\'titles\'); ?>"><?php _e(\'Title:\', \'tl_back\'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id(\'titles\'); ?>" name="<?php echo $this->get_field_name(\'titles\'); ?>" type="text" value="<?php echo $titles; ?>" /></p>
        <p><label for="<?php echo $this->get_field_id(\'number_show\'); ?>"><?php _e(\'Number of posts to show:\', \'tl_back\'); ?></label>
            <input id="<?php echo $this->get_field_id(\'number_show\'); ?>" name="<?php echo $this->get_field_name(\'number_show\'); ?>" type="text" value="<?php echo $number_show; ?>" size="3" /></p>

        <label for="<?php echo $this->get_field_id(\'cats\'); ?>"><?php _e(\'Select categories to include in the recent posts list:\', \'tl_back\'); ?>

            <?php
            $categories = get_categories(\'hide_empty=0\');
            echo "<br/>";
            foreach ($categories as $cat) {
                $option = \'<input type="checkbox" id="\' . $this->get_field_id(\'cats\') . \'[]" name="\' . $this->get_field_name(\'cats\') . \'[]"\';
                if (isset($instance[\'cats\'])) {
                    foreach ($instance[\'cats\'] as $cats) {
                        if ($cats == $cat->term_id) {
                            $option = $option . \' checked="checked"\';
                        }
                    }
                }
                $option .= \' value="\' . $cat->term_id . \'" />\';
                $option .= \'&nbsp;\';
                $option .= $cat->cat_name;
                $option .= \'<br />\';
                echo $option;
            }
            ?>
        </label>
        <?php

    }

    //create block
    function block($instance) {

        extract($instance);

        $titles = apply_filters(\'widget_title\', empty($instance[\'titles\']) ? \'Recent Posts\' : $instance[\'titles\'], $instance, $this->id_base);


        if (!isset($instance["cats"])) {
            $cats = \'\';}

        // array to call recent posts.

        $themeloy_args = array(
            \'showposts\' => $number_show,
            \'category__in\' => $cats,
        );

        $themeloy_widget = null;
        $themeloy_widget = new WP_Query($themeloy_args);

        ?>
        <div class="widget post_list_medium_widget">
            <?php if (!empty($instance[\'titles\'])) {?><h3 class="widget-title"><span><?php echo $instance["titles"];?></span></h3><?php }?>
            <div class="widget_container">
                <div class="post_list_medium">
                    <?php
                    $i = 0;
                    while ($themeloy_widget->have_posts()) {
                        $i++;
                        $themeloy_widget->the_post();
                        $post_id = get_the_ID();
                        $thumb = themeloy_get_thumbnail(get_the_ID());
                        if ($i == 1) {
                            ?>

                            <div class="list_item">

                                <div class="entry-thumb feature-item">
                                    <a href="<?php the_permalink(); ?>">
                                        <?php if ( has_post_thumbnail()) {
                                            echo wp_get_attachment_image( get_post_thumbnail_id( $post_id ), \'feature-large\', false, array(\'class\'=>\'cat-feature-large-img\') );
                                        } else {echo \'<img class="cat-feature-large-img" src="\'.get_template_directory_uri().\'/images/demo/feature-large.jpg\'.\'" alt="">\';} ?>
                                    </a>

                                    <div class="cat-feature-large">
                                        <?php $post_cat = get_post_custom_values(\'cat_themeloy_select\', get_the_ID());
                                        $post_cat = ($post_cat[0] != \'\') ? $post_cat[0] : of_get_option(\'blog_category_post\');
                                        if($post_cat == \'1\'){echo \'<p class="cat-slider">\'; echo the_category(\', \').\'</p>\';}?>
                                    </div>

                                    <?php echo themeloy_post_type(); ?>
                                </div>
                                <h2><a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a></h2>

                                <?php echo themeloy_post_meta(get_the_ID());?>
                                <p><?php echo themeloy_short_title(320, get_the_excerpt(\'\')); ?>  </p>
                            </div>
                            <div class="clear margin-buttons"></div>
                        <?php }else{?>

                            <div class="small-list-content">
                                <?php
                                $post_date = get_post_custom_values(\'date_themeloy_select\', get_the_ID());
                                $post_date = ($post_date[0] != \'\') ? $post_date[0] : of_get_option(\'blog_date_post\');
                                if($post_date == \'1\'){
                                    ?>
                                    <div class="feature_post_style">
                        <span class="post_date"><span class="date_number"><?php echo get_the_date(\'d\');?></span> <?php echo get_the_date(\'M\');?>
                        <i class="icon-caret-right feature-icon-right"></i>
                        </span>
                                        <span class="post_time"><i class="icon-time"></i> <?php echo get_the_time(\'H:i\');?></span>
                                    </div>
                                <?php }?>
                                <div class="feature-link feature-item">
                                    <a href="<?php the_permalink(); ?>" class="feature-link">
                                        <?php if ( has_post_thumbnail()) {
                                            echo wp_get_attachment_image( get_post_thumbnail_id( $post_id ), \'medium-feature\', false );
                                        }else{echo \'<img src="\'.get_template_directory_uri().\'/images/demo/medium-feature.jpg" alt="">\';} ?>
                                    </a>
                                    <?php echo themeloy_post_type(); ?>
                                </div>

                                <div class="list_desc">
                                    <h4 class="list_title"><a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                                            <?php the_title(); ?>
                                        </a></h4>

                                    <?php echo themeloy_post_meta(get_the_ID());?>

                                </div>
                                <div class="clear"></div>
                            </div>

                        <?php }}?>


                </div>
            </div>
        </div>

        <?php
        wp_reset_query();

    }

    function update($new_instance, $old_instance) {
        return $new_instance;
    }


}
也可回退img 如果未获得post\\u缩略图集,则标记alt 属性为空,因为它是辅助功能指南所要求的。

SO网友:fantomas17

这里我有同样的问题,你能在这段代码中实现其他代码吗?

<?php
add_action( \'widgets_init\', \'themeloy_rec_register_widgets\' );

function themeloy_rec_register_widgets() {
    register_widget( \'themeloy_rec_posts_widget\' );
}

class themeloy_rec_posts_widget extends WP_Widget {


    function __construct() {
        $widget_ops = array(
            \'classname\'   => \'post_list_widget\', 
            \'description\' => __(\'Display a list of recent post entries from choosing categories.\', \'tl_back\')
        );
        parent::__construct(\'rec-recent-posts\', __(\'Themeloy: Recently Posts\', \'tl_back\'), $widget_ops);
    }



    function widget($args, $instance) {
        extract($args);
        $title = apply_filters(\'widget_title\', empty($instance[\'title\']) ? \'Recently Articles\' : $instance[\'title\'], $instance, $this->id_base);
        if (!$number = absint($instance[\'number\']))
            $number = 6;
        if (!$cats = $instance["cats"])
            $cats = \'\';


        $post_cat_args = array(
            \'showposts\' => $number,
            \'category__in\' => $cats,
        );

        $post_cat_widget = null;
        $post_cat_widget = new WP_Query($post_cat_args);


        echo $before_widget;


        echo $before_title;
        echo $instance["title"];
        echo $after_title;

        // Post list in widget
        echo \'<div class="widget_container">\';
        echo \'<ul class="post_list">\';

        while ($post_cat_widget->have_posts()) {
            $post_cat_widget->the_post();
            $post_id = get_the_ID();
            ?>
           <li class="clearfix">    
                <div class="img_thumbnail feature-item">
                <?php               
                echo \'<a class="img_thumbnail" href="\' . get_permalink() . \'" title="\' . the_title_attribute(\'echo=0\') . \'">\';?>
<?php if ( has_post_thumbnail()) {
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), \'small-feature\' );?>
        <img src="<?php echo $image[0]; ?>" />
<?php }else{echo \'<img src="\'.get_template_directory_uri().\'/images/demo/small-feature.jpg\'.\'">\';} ?>   

              </a>
              <?php echo themeloy_post_type(); ?>
         </div>
                <div class="list_desc">
                     <h4 class="list_title"><a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                    </a></h4>
                   <?php echo themeloy_post_meta(get_the_ID());?>
                            <?php 
                                $post_review = get_post_custom_values(\'enable_review_themeloy_select\', $post_id); 
                                $post_review = ($post_review[0] != \'\') ? $post_review[0] : of_get_option(\'enable_all_review\');
                                $total_review = absint(themeloy_get_total_review($post_id));
                                if($post_review == \'1\'){
                               ?>
<div class="ratings">
<div class="rating-box">
<div class="rating" style="width:<?php echo themeloy_get_total_review(get_the_ID()); ?>%"></div>
</div>
</div>
 <?php }?> 

                </div>


            </li>
            <?php
        }

        wp_reset_query();

        echo "</ul>\\n";
        echo "</div>\\n";
        echo $after_widget;
    }



    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        $instance[\'cats\'] = $new_instance[\'cats\'];
        $instance[\'number\'] = absint($new_instance[\'number\']);

        return $instance;
    }



    function form( $instance ) {
        $title = isset($instance[\'title\']) ? esc_attr($instance[\'title\']) : \'Recent Posts\';
        $number = isset($instance[\'number\']) ? absint($instance[\'number\']) : 5;

?>
        <p><label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\', \'tl_back\'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo $title; ?>" /></p>

        <p><label for="<?php echo $this->get_field_id(\'number\'); ?>"><?php _e(\'Number of posts to show:\', \'tl_back\'); ?></label>
        <input id="<?php echo $this->get_field_id(\'number\'); ?>" name="<?php echo $this->get_field_name(\'number\'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

         <p>
            <label for="<?php echo $this->get_field_id(\'cats\'); ?>"><?php _e(\'Select categories to include in the recent posts list:\', \'tl_back\');?> 

                <?php
                   $categories=  get_categories(\'hide_empty=0\');
                     echo "<br/>";
                     foreach ($categories as $cat) {
                    $option = \'<input type="checkbox" id="\' . $this->get_field_id(\'cats\') . \'[]" name="\' . $this->get_field_name(\'cats\') . \'[]"\';

                    if (isset($instance[\'cats\'])) {
                        foreach ($instance[\'cats\'] as $cats) {
                            if ($cats == $cat->term_id) {
                                $option = $option . \' checked="checked"\';
                            }
                        }
                    }

                    $option .= \' value="\' . $cat->term_id . \'" />\';
                    $option .= \'&nbsp;\';
                    $option .= $cat->cat_name;
                    $option .= \'<br />\';
                    echo $option;
                }

                    ?>
            </label>
        </p>

<?php
    }
}
?>
谢谢!