可以';在Wordpress插件中,我无法得到回声来做我想做的事情


Can't get an echo to do what I want in Wordpress plugin

所以我正在尝试编写一个新的插件,因为我还没能找到一个能完全满足我需要的扩展性的插件。该插件的目标是能够使用一个简单的快捷代码来显示一个自动填充博客最新帖子的图像滑块。

我已经准备好了基本的插件文件,并实现和测试了快捷代码。我昨天在SO上解决了一个难题,但这个解决方案突出了一个新问题。这是代码:

function slyd( $category, $slydcount ) {
    global $post;
    $tmp_post = $post;                                                                              // Create $tmp_post to empty $post once Slyd is done with it
    $args = array(
        'category'      =>  $category,
        'numberposts'   =>  $slydcount
    );
    $slydposts = get_posts( $args );
    foreach( $slydposts as $post ) : setup_postdata($post);
        $post_title         =   get_the_title();                                                    // Get the post's title
        $post_content       =   get_the_content();                                                  // Get the post's content - will write code to get excerpt later
        $post_thumb         =   wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );     // Get the post's featured image's src
        $post_permalink     =   get_permalink();                                                    // Get the post's permalink
        echo '<h2><a href="' . $post_permalink . '">' . $post_title . '</a></h2>'
        . '<p>' . $post_content . '</p>'
        . '<p>' . $post_thumb . '</p>';
    endforeach;
    $post = $tmp_post;                                                                              // Empty $post once Slyd is done with it
}
// Create the shortcode
function slyd_shortcode( $atts ) {
    // $atts        ::=     array of attributes
    // examples:            [slyd]
    //                      [slyd category='slide']
    //                      [slyd slydcount='5']
    //                      [slyd theme='default']
    /* Retrieve attributes set by the shortcode and set defaults for
    unregistered attributes. */
    extract( shortcode_atts( array(
        'category'      =>  'slyd',                                                                 // Which category(s) to display posts from
        'slydcount'     =>  '5',                                                                    // How many Slyds to display
        'theme'         =>  'default'                                                               // Which Slyd theme to use
    ), $atts ) );
    return "<p>category = {$category}, count = {$slydcount}</p>"
    . slyd( $category, $slydcount );
}
add_shortcode( 'slyd', 'slyd_shortcode' );

问题出现在function slyd();foreach循环中。我最初使用return,现在使用echo将结果显示在屏幕上。这起到了显示第一个帖子的作用,但它当然会逃脱这个功能。我需要它循环浏览并显示所有的帖子。

通过研究PHP文档,我发现我可以使用print来代替echoreturn,但它给我的结果与echo相同。发生的情况是,代码似乎执行了两次。它第一次把自己放在需要的地方,然后它也会回显到浏览器,并把它放在页面的head下面。

我想我的问题是,是否有returnechoprint的替代方案可以解决我的问题?

提前谢谢。

现在我正试图让插件拉入博客的最新帖子,但我遇到了一点麻烦。当我使用_title()和_permink()时,它们显示在我试图包含它们的代码之外。此外,_content()与_permlink()和_title一起显示一次,然后在应该显示的地方显示第二次。

你可以在这里看到行为。

return就是您在这种情况下想要的。您希望从slyd函数返回一个值(即html代码),以便在slyd_shortcode函数中使用它(在本例中是附加它)。但是,您需要首先将所有输出收集到一个附加变量中(下面的$ret),然后返回该值:

function slyd( $category, $slydcount ) {
    global $post;
    $tmp_post = $post;
    $args = array(
        'category'      =>  $category,
        'numberposts'   =>  $slydcount
    );
    $slydposts = get_posts( $args );
    $ret = '';
    foreach( $slydposts as $post ) {
        setup_postdata($post);
        $post_title         =   get_the_title();
        $post_content       =   get_the_content();
        $post_thumb         =   wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
        $post_permalink     =   get_permalink();
        $ret .= '<h2><a href="' . $post_permalink . '">' . $post_title . '</a></h2>'
        . '<p>' . $post_content . '</p>'
        . '<p>' . $post_thumb . '</p>';
    }
    $post = $tmp_post;                                                                  
    return $ret;
}

正如您所看到的,您应该首先用一个空字符串初始化$ret变量,然后在foreach循环的每次迭代中附加到它。return用于在循环之后返回整个字符串