在我们做SEO优化的过程中,我们经常提到给文章链接增加标题属性。当然仁者见仁智者见智,有些人觉得这是过度优化。有些人觉得是必须的项目。
不过我不管。我只写教程。就是如何自动为文章内链接自动添加标题属性。
下面是代码。您只需要将其放在您的主题functions.php文件里即可
function ietheme_auto_add_link_titles( $content ) {
// No need to do anything if there isn't any content or if DomDocument isn't supported
if ( empty( $content ) || ! class_exists( 'DOMDocument' ) ) {
return $content;
}
// Define links array
$links = array();
// Create new dom object
$dom = new DOMDocument;
// Load html into the object
$dom->loadHTML( utf8_decode( $content ) );
// Discard white space
$dom->preserveWhiteSpace = false;
// Loop through all content links
foreach( $dom->getElementsByTagName( 'a' ) as $link ) {
// If the title attribute is already defined no need to do anything
if ( $link->getAttribute( 'title' ) ) {
continue;
}
// Get link text
$link_text = $link->textContent;
// If there isn't any link text (most probably an image) lets try and get it from the first child
if ( ! $link_text && ! empty( $link->firstChild ) && $link->firstChild->hasAttributes() ) {
// Get alt
$alt = $link->firstChild->getAttribute( 'alt' );
// If no alt get image title
$alt = $alt ? $alt : $link->firstChild->getAttribute( 'title' );
// Clean up alt (remove dashses and underscores which are common in WP)
$alt = str_replace( '-', ' ', $alt );
$alt = str_replace( '_', ' ', $alt );
// Return cleaned up alt
$link_text = $alt;
}
// 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 = ( $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;
}
// Edit content on the front end
add_filter( 'the_content', 'ietheme_auto_add_link_titles' );
本文已在Ie主题由99839发布
文章来源:https://ietheme.com/auto-link-titles.html