是否使用图像标题作为链接标题的自定义图库过滤器?

时间:2014-02-22 作者:Josh M

我使用一个自定义库函数来更改普通的Wordpress标记。通常这很好,但我不太清楚如何在缩略图链接上添加图像标题作为标题属性。(我使用的lightbox将title属性转换为标题。)

以下是过滤器中的相关代码:

foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
        $link = str_replace(\'><img\',\'title="test caption" ><img\',$link); 
        $output .= "<{$itemtag} class=\'item\'>";
        $output .= "            
                $link
            ";
        if ( $captiontag && trim($attachment->post_content) ) {
            $output .= "
                <{$captiontag}>
                " . wptexturize($attachment->post_content) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'\';
    }
    $output .= "
            <br>\\n";
    return $output;
}
那一秒$link 符合str_replace 是我所能做到的。我想也许我可以得到wp\\u get\\u attachment\\u link来输出一个title属性,当我找不到解决方案时,我使用str\\u replace,但我不知道如何获得当前单词“test caption”所在的图像标题。

1 个回复
SO网友:somepaulo

我在WordPress论坛上找到了一个有效的解决方案。代码如下:

function add_img_title_to_anchor($content) {

    /* Find internal links */

    //Check the page for linked images
    $search = \'/<a ([^>]*?)><img ([^>]*?)\\/><\\/a>/i\';
    preg_match_all( $search, $content, $matches, PREG_SET_ORDER);

    //Check which attachment is referenced
    foreach ($matches as $val) {
        // Only if the Link doesn\'t already have a Title attribute, we work
        if (!preg_match("#title=#", $val[1])) {

            // Find all Link attributes and sanitize the Href att
            $anchor_temp = preg_match_all("#([a-z-]+?)=([\'\\"]{1})([^\'\\"]*)([\'\\"]{1})#", $val[1], $matches_anchor);
            foreach ($matches_anchor[1] as $key => $value) {
                $anchor_atts[$value] = $matches_anchor[3][$key];
            }

            // Find all Image attributes
            $img_temp = preg_match_all("#([a-z-]+?)=([\\"]{1})([^\\"]*)([\\"]{1})#", $val[2], $matches_img);
            foreach ($matches_img[1] as $key => $value) {
                $img_atts[$value] = $matches_img[3][$key];
            }

            // Get the Image Title attribute and copy it to the Link attributes
            //  Case 1. If Image Title exists we use it
            if (isset($img_atts["title"]) && $img_atts["title"] != "") {
                $anchor_atts["title"] = $img_atts["title"];
            }
            //  Case 2. If no we use the Alt attribute
            else {
                $anchor_atts["title"] = $img_atts["alt"];
            }

            // Rebuilt the HTML tags
            $anchor_attributes = array();
            foreach ($anchor_atts as $key => $value) {
                $anchor_attributes[] = $key . \'="\' . $value . \'"\';
            }

            $img_attributes = array();
            foreach ($img_atts as $key => $value) {
                $img_attributes[] = $key . \'="\' . $value . \'"\';
            }

            // Replace the previous tags by the new
            $replacement = \'<a \' . implode(" ", $anchor_attributes) . \'><img \' . implode(" ", $img_attributes) . \' /></a>\';

            $content = str_replace($val[0], $replacement, $content);
        }
    }

    return $content;
原文如下:https://wordpress.org/support/topic/add-the-title-of-an-image-to-the-image-link-in-gallery

结束

相关推荐

Filters on Login Page

我正在尝试使用修改登录页built in filters. add\\u操作按预期工作,但我无法使筛选器工作。这是我函数中的代码。php:add_filter( \'login_form_top\', \'filter_top_login\' ); function filter_top_login( $content ) { return \'This is what I want it to say!\'; } 但当我加载wp登录时,它似乎没有做任何