532   WordPress

wordpress 的links可以显示图片image,但是按原图大小显示,页面容易乱,需要调下样式;

如果links只显示文字name的话,样式不需要调整也能满足,这样话,通过判断显示内容来调整样式啦

 

links显示内容通过WP_Widget_Links类来实现,在wp-includes/default-widgets.php文件中

这个类中的方法widget用来显示内容,最终是调用wp_list_bookmarks了,继续顺藤摸瓜

 

 

wp_list_bookmarks在bookmark-template.php中,打开一看,知道在哪里改了

这里写到

if ( $categorize ) {
// Split the bookmarks into ul’s for each category
foreach ( (array) $cats as $cat ) {}

根据分类来显示bookmarks即links,这样的话,将想要显示的图片image归为一类,显示名字name的归为另一类,然后在这里判断分类是图片还是文字,接着修改输出内容的样式即可,

 

实现代码:

// Split the bookmarks into ul’s for each category
foreach ( (array) $cats as $cat ) {
$catname = apply_filters( ‘link_category’, $cat->name );

// 根据分类来修改显示内容的样式
if($catname == ‘Advertise’){
$params = array_merge($r, array(‘category’=>$cat->term_id));
$bookmarks = get_bookmarks($params);
if ( empty($bookmarks) )
continue;
$ads_content='<div class=”ly-advertisement”>’;

//Ads one content
foreach ( (array) $bookmarks as $bookmark ) {

$one_array=array(
‘href_url’=>esc_url($bookmark->link_url),
‘src_url’=>$bookmark->link_image
);

$ads_content.='<div class=”ly-ad-link”>’;

$ads_content.='<a href=”‘.$one_array[‘href_url’].'” >’;
$ads_content.='<img class=”sidebar-link” src=”‘.$one_array[‘src_url’].'” />’;
$ads_content.='</a>’;
$ads_content.='</div>’;

}
$ads_content.=”</div>”;

$output .= $ads_content;
}
else{
$params = array_merge($r, array(‘category’=>$cat->term_id));
$bookmarks = get_bookmarks($params);
if ( empty($bookmarks) )
continue;
$output .= str_replace(array(‘%id’, ‘%class’), array(“linkcat-$cat->term_id”, $class), $category_before);
/**
* Filter the bookmarks category name.
*
* @since 2.2.0
*
* @param string $cat->name The category name of bookmarks.
*/
$catname = apply_filters( ‘link_category’, $cat->name );

$output .= “$title_before$catname$title_after\n\t<ul class=’xoxo blogroll’>\n”;
$output .= _walk_bookmarks($bookmarks, $r);
$output .= “\n\t</ul>\n$category_after\n”;
}

}

 

大功告成,收官回家




Leave a Reply

Your email address will not be published. Required fields are marked *