根据用户输入输出内容

时间:2013-10-16 02:59:05

标签: php html wordpress

我是PHP的新手,想根据用户输入显示一些信息;在网站上创建无序列表及其详细信息。

我的WordPress主题中有以下脚本......

的functions.php:

/* Add/remove author profile fields */
function new_contact_methods( $contactmethods ) {
    unset($contactmethods['aim']);
    unset($contactmethods['jabber']);
    unset($contactmethods['yim']);
    $contactmethods['google_plus'] = 'Google+';
    $contactmethods['twitter'] = 'Twitter';
    $contactmethods['facebook'] = 'Facebook';
    $contactmethods['linkedin'] = 'Linkedin';   

    return $contactmethods;
}

add_filter('user_contactmethods','new_contact_methods', 10, 1);

single.php中:

<ul style="margin: 0;">
    <li><a href="<?php the_author_meta('url'); ?>" target="_blank"></a></li>
    <li><a href="https://plus.google.com/<?php the_author_meta('google_plus'); ?>" target="_blank"></a></li>
    <li><a href="http://twitter.com/<?php the_author_meta('twitter'); ?>" target="_blank"></a></li>
    <li><a href="http://facebook.com/<?php the_author_meta('facebook'); ?>" target="_blank"></a></li>
    <li><a href="http://au.linkedin.com/in/<?php the_author_meta('linkedin'); ?>" target="_blank"></a></li>
</ul>

如何从用户个人资料设置中输出信息:

enter image description here

只有 if 他们已将内容放入上述任何字段。

例如,我不希望显示无序列表或未列入任何字段的列表项:

  • https://plus.google.com/example
  • http://au.linkedin.com/in/example


感谢。

1 个答案:

答案 0 :(得分:2)

使用the_author_meta()echo输出值,因此除非缓冲输出或其他内容,否则无法提前检查它。相反,您可以使用get_the_author_meta。区别在于,它不是echo值,而是返回它。所以你可以查看该值,然后使用php的empty来查看它是否已填写,所以你的代码可能看起来像这样

<ul style="margin: 0;">
<?php
$url=get_the_author_meta('url');
if (!empty($url))
{
?>
<li><a href="<?php the_author_meta('url'); ?>" target="_blank">
<?php the_author_meta('url'); ?></a>
</li>
<?php
}
//...

以及快速获取所需属性的方法

<?php
$metaInfo=array('url','twitter','google_plus','facebook','linkedin');
$validProperties=0;
foreach($metaInfo as $infoItem)
{
$itemValue=get_the_author_meta($infoItem);
if (!empty($itemValue))
{
$validProperties++; //increments by 1
}
}
?>
<?php
if ($validProperties > 0)
{ //meaning we found at least 1
?>
<ul style="margin: 0;">
<?php
foreach($metaInfo as $infoItem)
{
$itemValue=get_the_author_meta($infoItem);
if (!empty($itemValue))
{
?>
<li>
<a href="<?php the_author_meta($infoItem); ?>" target="_blank">
<?php the_author_meta($infoItem); ?></a>
</li>
<?php
}

}
?>
</ul>
<?php
}
else
{
//else we found none so display nothing! or do something like
echo "no data found!";
}
?>