Wordpress:将文章自动显示为两列

很多博客都有一行显示多个post文章的样式,他的原理其实非常简单,今天找到一段php代码,只需在functions.php文件中添加上,那么你的文章也会多列显示,非常实用!

1.PHP代码:

打开functions.php文件,添加如下的php代码,通过判断是否是第二次输出来给出左右浮动的div标签。

?[Copy to clipboard]View Code PHP

1234567891011121314151617181920212223242526272829303132
< ?phpfunction my_multi_col($content){	$columns = explode("<h2>", $content); 	$i = 0; 	foreach ($columns as $column) {		if (($i % 2) == 0){			$return .= '<div class="content_left">';			if ($i > 1){				$return .= "<h2>";			} else{				$return .= '<div class="content_right"><h2>';			}			$return .= $column;			$return .= "</h2></div>";			$i++;		} 		if(isset($columns[1])){			$content = wpautop($return);		}else{			$content = wpautop($content);		}		echo $content;	}} add_filter('the_content', 'my_multi_col'); ?></h2></div>

代码中的h2是你的每个文章标题所包含的标签。

2.CSS代码:

完成在functions.php文件中添加好上面的代码后,记得打开style.css文件,添加上样式代码。

?[Copy to clipboard]View Code CSS

1234567891011
.content_right, .content_left {    width:45%;} .content_left {    float:left;} .content_right {    float:right;}

那么同理,我们可以根据判断$i变量来输出3列、4列等。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Wordpress:将文章自动显示为两列》
文章链接:https://www.skykkk.com/archives867.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

相关推荐

  • 暂无文章