【问题标题】:php wordpress function returning Warning: Missing argument 1php wordpress 函数返回警告:缺少参数 1
【发布时间】:2012-08-27 22:42:40
【问题描述】:

我写了这几个函数,它们用帖子永久链接替换了一个爆炸的自定义字段字符串:

(自定义字段应如下所示:例如 Google++http//:google.com")

// Custom Permalink
function custom_permalink($url){
    global $post;
    $link = get_post_meta($post->ID,'link',true);
    if ($link) {
        $pieces = explode("++", $link);
        $url = $pieces[1];
    } else {
        $url = the_permalink();
    }
    return $url;
}

// Via Text
function via_text($url){
    global $post;
    $link = get_post_meta($post->ID,'link',true);
    if ($link) {
        $pieces = explode("++", $link);
        $url = ' <span><a href="'.$pieces[1].'">Via '.$pieces[0].'</a></span>';
    } else {
        $url = ' ';
    }
    return $url;
}

...在 MAMP 服务器上测试时可以正常工作,但在部署时它们会返回:

“警告:缺少参数 1”

知道为什么会发生这种情况吗?

【问题讨论】:

标签: wordpress function warnings mamp


【解决方案1】:

好吧...我想通了... 当一个类被实例化并且构造函数没有默认参数时会发生这种情况......

这里是工作函数...

    // Custom Permalink
    function custom_permalink($url='') {
        global $post;
        $link = get_post_meta($post->ID,'link',true);
        if ($link) {
            $pieces = explode("++", $link);
            $url = $pieces[1];
        } else {
            $url = the_permalink();
        }
        return $url;
    }

    // Via Text
    function via_text($url='') {
        global $post;
        $link = get_post_meta($post->ID,'link',true);
        if ($link) {
            $pieces = explode("++", $link);
            $url = ' <span><a href="'.$pieces[1].'">Via '.$pieces[0].'</a></span>';
        } else {
            $url = ' ';
        }
        return $url;
    }

万一有人需要... 他们做什么:

如果有带有自定义字段“链接”的帖子(例如:Google++http://google.com),该函数将使用自定义链接替换永久链接。所以在主题调用中: echo custom_permalink(); ... 而不是 the_permalink();

via_text() 函数的工作方式略有不同;它调用一个带有自定义字段“链接”的跨度标签,如下所示:

     <span><a href="http://google.com">Via Google</a></span>

希望这对其他人有用

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2017-05-05
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多