下面的函数根据自定义字段列出帖子。
结果包含在列表中<li></li>
. 我想做的是计算有多少列表项,然后将其分为两个ul。有没有办法将结果分为两列?
function article_series() {
global $post;
$series = get_post_meta($post->ID, \'Series\', true);
if($series) :
$args = array(
\'numberposts\' => -1,
\'meta_key\' => \'Series\',
\'meta_value\' => $series,
);
$series_posts = get_posts($args);
if($series_posts) :
$class = preg_replace("/[^a-z0-9\\\\040\\\\.\\\\-\\\\_\\\\\\\\]/i", "", $series);
$class = strtolower(str_replace(array(\' \', \' \'), \'-\', $class));
echo \'<div class="series series-\' . $class . \'"><h4 class="series-title">\' . __(\'Articles in this series\') . \'</h4><ul>\';
foreach($series_posts as $serial) :
if($serial->ID == $post->ID)
echo \'<li class="current-post">\' . $serial->post_title . \'</li>\';
else
echo \'<li><a href="\' . get_permalink($serial->ID) . \'" title="\' . str_replace(\'"\', \'"\', $serial->post_title) . \'">\' . str_replace(\'"\', \'"\', $serial->post_title) . \'</a></li>\';
endforeach;
echo \'</ul></div>\';
endif;
endif;
函数创建人
justintadlock.com谢谢你的帮助。
SO网友:Nate Cook
您需要查看系列文章的数量,并粘贴一个结束/开始<ul>
在中间类似于:
echo \'<ul class="left">\';
$series_count = count($series_posts);
$series_middle = ceil($series_count / 2);
for ($i = 0; $i < $series_count; $i++) {
if ($serial->ID == $post->ID)
echo \'<li class="current-post">\' . $serial->post_title . \'</li>\';
else
echo \'<li><a href="\' . get_permalink($serial->ID) . \'" title="\' . str_replace(\'"\', \'"\', $serial->post_title) . \'">\' . str_replace(\'"\', \'"\', $serial->post_title) . \'</a></li>\';
if ($i == $series_middle)
echo \'</ul><ul class="right">\';
}
echo \'</ul>\';
然后可以使用CSS设置两个列表的样式,使其相邻显示。