自动将标题属性添加到WordPress中的链接

时间:2011-06-05 作者:Tony Hue

如果我链接到网站上的文章,是否有方法自动包含标题属性,其值为链接页面的标题?

2 个回复
SO网友:Bainternet

不知道这是否有效但应该,

function get_page_title($url){
        if( !class_exists( \'WP_Http\' ) )
            include_once( ABSPATH . WPINC. \'/class-http.php\' );
        $request = new WP_Http;
        $result = $request->request( $url );
        if( is_wp_error( $result ) )
            return false;

        if( preg_match("#<title>(.+)<\\/title>#iU", $result, $t))  {
            return trim($t[1]);
        } else {
            return false;
        }
    }

    add_filter(\'the_content\',\'auto_add_title_to_link\');

    function auto_add_title_to_link($content){
        $html = new DomDocument;
        $html->loadHTML($content); 
        $html->preserveWhiteSpace = false; 
        //get all links
        foreach($html->getElementsByTagName(\'a\') as $link) {
        //make sure it dosent have a title
            if ($link->getAttribute(\'title\') == \'\' || empty($link->getAttribute(\'title\')))
                $links[] = $link->getAttribute(\'href\');
        }
        //get title and add it
        foreach ($links as $link){
            $title = get_page_title($link);
            if (false !== $title){
                $replace = $link.\' title="\'.$title.\'"\';
                $content = str_replace($link,$replace,$content);
            }

        }
        return $content;
    }

SO网友:WPExplorer

这可能更好一些(概念相同,但经过修改):

*编辑:Github上的新修改版本,支持带有链接的图像-https://github.com/wpexplorer/wpex-auto-link-titles/blob/master/wpex-auto-link-titles.php

function wpex_auto_add_link_titles( $content ) {

    // No need to do anything if there isn\'t any content
    if ( empty( $content ) ) {
        return $content;
    }

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content );
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( \'a\' ) as $link ) {

        // If the title attribute is already defined no need to do anything
        if ( ! empty( $link->getAttribute( \'title\' ) ) ) {
            continue;
        }

        // Get link text
        $link_text = $link->textContent;

        // Save links and link text in $links array
        if ( $link_text ) {
            $links[$link_text] = $link->getAttribute( \'href\' );
        }

    }

    // Loop through links array and update post content to add link titles
    if ( ! empty( $links ) ) {
        foreach ( $links as $text => $link ) {
            if ( $link && $text ) {
                $text    = esc_attr( $text ); // Sanitize
                $text    = ucwords( $text );  // Captilize words (looks better imo)
                $replace = $link .\'" title="\'. $text .\'"\'; // Add title to link
                $content = str_replace( $link .\'"\', $replace, $content ); // Replace post content
            }

        }
    }

    // Return post content
    return $content;

}
add_filter( \'the_content\', \'wpex_auto_add_link_titles\' );

结束

相关推荐

title tag function

我正在使用一个函数来显示标题标签上的URL,我对这个函数进行了编码,它可以正常工作,但我想对它进行进一步的自定义。function title_tag() { $a = $_SERVER[\'REQUEST_URI\']; $b = strtoupper($a); $c = str_replace(\'-\', \' \', $b); $d = str_replace(\'/\', \' - \', $c); $e = substr($d, 2);