0

我无法回显每个数组。不知道我做错了什么。回显每个数组的方法是什么?尝试了以下一些方法来回显,但没有成功。

function simple_social_sharing( $attr_twitter = null, $attr_items = null ) {

    // parse variables
    $twitter_account = $attr_twitter;
    $item_toggles = $attr_items;

    // get post content and urlencode it
    global $post;
    $browser_title_encoded = urlencode( trim( wp_title( '', false, 'right' ) ) );
    $page_title_encoded = urlencode( get_the_title() );
    $page_url_encoded = urlencode( get_permalink($post->ID) );

    // create share items array
    $share_items = array ();

    // set each item
    $item_facebook = array(
        "class" => "facebook",
        "href" => "http://www.facebook.com/sharer.php?u={$page_url_encoded}&t={$browser_title_encoded}",
        "text" => "Share on Facebook"
    );
    $item_twitter = array(
        "class" => "twitter",
        "href" => "http://twitter.com/share?text={$page_title_encoded}&url={$page_url_encoded}&via={$twitter_account}",
        "text" => "Share on Twitter"
    );
    $item_google = array(
        "class" => "google",
        "href" => "http://plus.google.com/share?url={$page_url_encoded}",
        "text" => "Share on Google+"
    );

    // test whether to display each item
    if($item_toggles) {
        // explode into array
        $item_toggles_array = explode( ",", $item_toggles );
        // set each item on or off
        $show_facebook = $item_toggles_array['0'];
        $show_twitter = $item_toggles_array['1'];
        $show_google = $item_toggles_array['2'];
    }
    else {
        $display_all_items = 1;
    }

    // form array of items set to 1
    if( $show_facebook==1 || $display_all_items ) {
        array_push( $share_items, $item_facebook );
    }
    if( $show_twitter==1 || $display_all_items) {
        array_push( $share_items, $item_twitter );
    }
    if( $show_google==1 || $display_all_items) {
        array_push( $share_items, $item_google );
    }

    // if one or more items
    if ( ! empty( $share_items ) ) {
        // create output
        $share_output = "<ul class=\"ss-share\">\n";
        foreach ( $share_items as $share_item ) {
            $share_output .= "<li class=\"ss-share-item\">\n";  
            $share_output .= "<a class=\"ss-share-link ico-{$share_item['class']}\" href=\"{$share_item["href"]}\" rel=\"nofollow\" target=\"_blank\">{$share_item['text']}</a>\n"; 
            $share_output .= "</li>\n"; 
        }
        $share_output .= "</ul>";
        // echo output
        echo $share_output;
    }
}

// add shortcode to output buttons
function simple_social_sharing_shortcode( $atts, $content = null ) {
    // parse variables / set defaults
    extract( shortcode_atts( array(
        'twitter' => '',
        'display' => '1,1,1',
    ), $atts ) );
    // output buttons
    ob_start();
    simple_social_sharing( $twitter, $display );
    $output_string = ob_get_contents();
    ob_end_clean();
    return force_balance_tags( $output_string );
}

开发人员调用功能也不起作用

<?php simple_social_sharing('twitteraccount', '1,1,1'); ?>

我试图通过这种方式调用每个数组,但这是错误的。

<?php simple_social_sharing([1]); ?>
4

2 回答 2

0

向下到底部,您的 foreach 块中有此代码段:
... href=\"{$share_item["href"]}\" ...

href 周围的双引号会给您带来问题。

您需要将其设置为
... href=\"{$share_item['href']}\" ...

于 2013-02-04T19:33:43.940 回答
0

函数应该返回值并且不回显它..

而不是echo $share_output;,你应该拥有它return $share_output;

回显函数的返回值(如果是数组,则返回print_recho以查看输出

<?php echo simple_social_sharing('twitteraccount', '1,1,1'); //NOTE THE 'echo' HERE.. ?>
于 2013-02-04T19:42:05.790 回答