我有一个Wordpress功能,可以检查用户是否填写了他或她个人资料中的某些字段。如果它被填写,则回显其内容,如果不是:不要。这是有趣的
的简化版本<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li>
<?php endif; // End check for linkedin ?>
</ul>
这很有效。但是,出于布局目的,我在这些元素上使用inline
,我不能使用浮点数。您可能知道,这会导致元素之间出现“间隙”,因为它们都在新行上回显。
我不希望这些差距。我知道我可以通过任意负余量来解决它,但我不想使用它,因为我使用的是响应式,基于百分比的布局。解决方案是通过将所有HTML粘合在一起而不使用新行来“填补HTML中的空白”。举个例子:
在内联显示时会产生间隙:
<ul>
<li>Twitter</li>
<li>Instagram</li>
<li>LinkedIn</li>
</ul>
不会有差距:
<ul>
<li>Twitter</li><li>Instagram</li><li>LinkedIn</li>
</ul>
我正在寻找一个PHP解决方案(没有CSS)可以摆脱ul
内的所有新行,但我不知道从哪里开始。或者,更准确地说,我不知道在哪里放置删除新行的功能。如何包装所有HTML,然后用...... nothing 替换新行?
答案 0 :(得分:0)
您可以这样做:
ob_start();
//echo whatever stuff you need
echo str_replace(PHP_EOL, NULL, ob_get_clean());
使用PHP_EOL
代替\n
或\r\n
,无论平台如何,都会抓住换行符。
答案 1 :(得分:0)
你有没有试过像:
$string = str_replace("\n", "", $string);
或:
$string = str_replace("\n\r", "", $string);
(其中$ string是你想在一行上得到的html)
答案 2 :(得分:0)
您可以使用缓冲区:
<?php
function remove_white_spaces($buffer){
return trim(preg_replace('/\s\s+/', ' ', $buffer));
}
ob_start('remove_white_spaces');
?>
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li>
<?php endif; // End check for linkedin ?>
</ul>
<?php ob_end_flush(); ?>
捕获缓冲区上的所有输出,运行一个函数以用空格替换新行,然后释放缓冲区。
答案 3 :(得分:0)
只需将ul font-size设置为0并将大小应用于li
font-size: 15px; display: inline-block;
答案 4 :(得分:0)
根据@ APAD1的评论和@CBRoe的反馈,我决定采用评论方法。虽然HTML输出不漂亮(空注释,byebye语义),但它完美无缺。
<ul class="author-meta">
<?php if (get_the_author_meta('url',$theID)) : ?><li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li><?php endif; // End check for url ?><!--
--><?php if ( get_the_author_meta('twitter',$theID)) : ?><li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li><?php endif; // End check for twitter ?><!--
--><?php if ( get_the_author_meta('instagram',$theID)) : ?><li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li><?php endif; // End check for instagram ?><!--
--><?php if ( get_the_author_meta('linkedin',$theID)) : ?><li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li><?php endif; // End check for linkedin ?>
</ul>
请注意,评论应 if
子句,因为如果它们在里面,则可能无法正确回显,例如:缺少开头或结束标签。
答案 5 :(得分:0)
你走了:
<ul><?php
$output = '';
if (get_the_author_meta('url', $theID)) {
$output .= '<li class="url"><a href="' . the_author_meta('url',$theID) . '" title="">Website</a></li>';
} elseif (get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter"><a href="http://twitter.com/' . the_author_meta('twitter', $theID) . '" title="">Twitter</a></li>';
} elseif (get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram"><a href="http://instagram.com/' . the_author_meta('instagram', $theID) . '" title="">Instagram</a></li>';
} elseif (get_the_author_meta('linkedin',$theID)) {
$output .= '<li class="linkedin"><a href="http://www.linkedin.com/' . the_author_meta('linkedin', $theID) . '" title="">LinkedIn</a></li>';
}
echo $output;
?></ul>
答案 6 :(得分:0)
<ul class="author-meta">
<?php
if (get_the_author_meta('url', $theID)) {
$output = '<li class="url"><a href="' . get_the_author_meta('url', $theID) . '" title="">Website</a></li>';
}
if ( get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter"><a href="http://twitter.com/' . get_the_author_meta('twitter', $theID) . '" title="">Twitter</a></li>';
}
if ( get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram"><a href="http://instagram.com/' . get_the_author_meta('instagram', $theID) . '" title="">Instagram</a></li>';
}
if ( get_the_author_meta('linkedin', $theID)) {
$output .= '<li class="linkedin"><a href="http://www.linkedin.com/' . get_the_author_meta('linkedin', $theID) . '" title="">LinkedIn</a></li>';
}
echo $output;
?>
</ul>
Bram Vanroy编辑:在PHP中使用该函数时,请确保使用get_the_author_meta
而不是the_author_meta
。
答案 7 :(得分:0)
要制作易于使用的版本,您可以将所有网站数据存储在一个数组中:
$site=Array(
"url"=>"Website",
"twitter"=> "Twitter",
"instagram"=> "Instagram",
"linkedin"=>"LinkedIn"
);
然后生成整个列表。我使用了一个简单的示例案例,其中所有域都是.com。如果您想要存储更多信息,则必须改进数组格式。
<ul><?php
foreach($site as $k=>$v)
if (get_the_author_meta($k,$theID))
echo '<li class="'.$k.'"><a href="'
.(($k=="url")?"":"http://$k.com/")
.the_author_meta($k,$theID).'">'.$v.'</a></li>';
?></ul>
请勿忘记删除a
代码中的空标题属性。
请注意,还有其他CSS备选方案:在display:table
上使用ul
,在display:table-cell
上使用li
,或使用display: flex
等上。