【问题标题】:Output content based on the users input根据用户输入输出内容
【发布时间】:2013-10-16 02:59:04
【问题描述】:

我是 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>

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

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

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

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


谢谢。

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    使用the_author_meta()echo 输出值,因此除非您缓冲输出或其他东西,否则您无法真正提前检查它。相反,您可以使用get_the_author_meta。不同之处在于它不是echoing 值,而是返回它。所以你可以看一下值,然后用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!";
    }
    ?>
    

    【讨论】:

    • 致命错误: 无法在 ../../single.php 的写入上下文中使用函数返回值122
    • 嗯,我以前没遇到过。请尝试我的编辑然后报告
    • 好多了@chiliNUT。如果用户个人资料的任何字段中均未放置任何内容,是否有办法删除&lt;ul&gt;&lt;/ul&gt; 标签?
    • 当然,您可以遍历所有这些,有时称为第一次,看看是否存在。然后,如果您确定至少存在 1,请进行第二次传递并将它们显示在 &lt;ul&gt; 中,否则您将无法显示任何内容。查看我的编辑^^
    • 解析错误: 语法错误,../../single.php 中的意外 '131
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多