【问题标题】:Wordpress Shortcode Returning "Array"Wordpress 简码返回“数组”
【发布时间】:2012-12-17 15:19:39
【问题描述】:

我正在制作一个自定义简码,它基本上只是为了返回我的自定义帖子类型,这是我的代码:

function shortcode_slider($atts, $content=null){  
    extract(shortcode_atts( array('id' => ''), $atts));  
    $return = $content;
    $return .= query_posts( array( 'post_status' => 'publish' , 'post_type' => 'slider'  ) );
    return $return;  
}  
add_shortcode('slider', 'shortcode_slider');

除了一件事之外,短代码可以正常工作 - 当它返回所有帖子时,它还会在列表顶部返回“数组” - 知道为什么会发生这种情况吗?

另外,我希望能够使用“id”输入来指定一个类别,例如

 $return .= query_posts( array( 'post_status' => 'publish' , 'post_type' => 'slider', 'category' => $id  ) );

但我不确定此语法是否正确。

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    首先,不要使用 query_posts
    请检查:

    在简码情况下,我会选择get_posts。它会返回你需要的东西,并且不会弄乱循环。

    值得注意的是,您实际上并不需要提取属性,使用$atts['id'] 就可以了。

    遵循有效的简码,参见 cmets:

    add_shortcode( 'slider', 'shortcode_slider' );
    
    function shortcode_slider( $atts, $content=null )
    {  
        // Initialize variable and check for shortcode content
        $return = '';
        if( $content ) {
            $return = $content;
        }
    
        // Shortcode attribute: title="Lorem Ipsum"
        if( isset( $atts['title'] ) ) 
        {
            $return .= '<br><h2>' . $atts['title'] . '</h2>';
        }
    
        // Get our custom posts
        // 'category' is the category ID or a comma separated list of ID numbers
        $sliders = get_posts( array( 
            'post_status' => 'publish', 
            'post_type' => 'slider',
            'numberposts' => 10, 
            'order'=> 'ASC', 
            'orderby' => 'title',
            'category' => $atts['id']  
        ) );
    
    
        // Auxiliary variable, don't print <br> in the first element
        $first = '';
    
        // Iterate through the resulting array
        // and build the final output
        // Use $slide->post_author, ->post_excerpt, as usual
        //   $slide->ID can be used to call auxiliary functions 
        //   such as get_children or get_permalink
        foreach( $sliders as $slide ) 
        {
            $link = get_permalink( $slide->ID );
            $return .= 
                $first
                . '<a href="' . $link . '">'
                . $slide->post_title 
                . '</a>
            ';
            $first = '<br>';
        }
    
        return $return;  
    }  
    

    在默认帖子类型中应用的短代码:

    自定义帖子类型:

    结果:

    有用的链接:

    【讨论】:

    • 如果我只想返回数组而不是如何获取值有什么替代方法
    【解决方案2】:

    似乎包含“数组”是因为您首先使用 $content。可以肯定的是,您必须在帖子中发布您是如何编写此短代码的,但这是一般的想法。您可能不需要该行:

    $return = $content;
    

    如果你的简码语法是[slider]some text[/slider],那么上面的行就可以了。

    至于第二部分,您将在代码中使用 $atts['id'] 检索 id。您的简码是:

    [slider id="5"]
    

    【讨论】:

    • 谢谢,我会试一试-我的第二部分更多的是将该ID放入查询中的语法是什么-实际上只是$id还是'$id'?编辑-只是试了一下,但在打印结果之前它仍然打印“数组”:S
    • 我的错 - 应该读得更好。 ID 部分很好,但它仍然在顶部打印单词“array” - 有什么想法吗?
    • 我应该添加 use is [slider id="an id"]- 没有任何结束标签。
    【解决方案3】:

    打印 Array 的原因就在这里:

    $return .= query_posts( ...
    

    首先,将$return 设置为内容字符串。然后,将query_posts() 返回的数组附加到它。我有点惊讶您没有收到“数组到字符串转换”错误,但也许您正在抑制它们。

    WordPress 自动回声短代码,所以无论你return 它都会回显。使用简码获取帖子可能不是最好的主意。简码应该返回字符串。

    这个答案感觉不完整,因为不清楚您要完成什么。 query_posts 是为了改变循环,但是当短代码被触发时,你已经在循环中了。

    【讨论】:

    • 基本上,我想要完成的只是一个打印给定类别中所有帖子(包括它们的 HTML)的简码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2013-07-25
    • 2019-09-04
    • 2011-06-22
    • 2013-07-18
    相关资源
    最近更新 更多