append_content help

时间:2010-12-14 作者:Niraj Chauhan

我的插件中有一个函数append_the_content($content) 这是用来在帖子中显示我的函数的,但它出现在帖子之前,我想在帖子之后显示,为此我尝试了这段代码,但它没有出现

function parse_twitter_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
  $feed = str_replace("&lt;", "<", $feed);
  $feed = str_replace("&gt;", ">", $feed);
  $clean = explode("<content type=\\"html\\">", $feed);

  $amount = count($clean) - 1;

  echo $prefix;

  for ($i = 1; $i <= $amount; $i++) {
    $cleaner = explode("</content>", $clean[$i]);
    echo $tweetprefix;
    echo $cleaner[0];
    echo $tweetsuffix;
  }

  echo $suffix;
}

function the_twitter_feed($username) {
  // $username = "Mba_"; // Your twitter username.
  $limit = "5"; // Number of tweets to fetch.

  /* These prefixes and suffixes will display before and after the entire block of tweets. */
  $prefix = ""; // Prefix - some text you want displayed before all your tweets.
  $suffix = ""; // Suffix - some text you want displayed after all your tweets.
  $tweetprefix = "<b>".$username.": </b> "; // Tweet Prefix - some text you want displayed before each tweet.
  $tweetsuffix = "<br>"; // Tweet Suffix - some text you want displayed after each tweet.

  $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit;

  $twitterFeed = get_transient($feed);
  if (!$twitterFeed) {
    $twitterFeed = wp_remote_fopen($feed);
    set_transient($feed, $twitterFeed, 3600); // cache for an hour
  }
  if ($twitterFeed)
    parse_twitter_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);
}

function append_the_content($content) {
    if(get_option(\'tweetID\')!=null){
    $content .= "<div class=\'post\'><p>".the_twitter_feed(get_option(\'tweetID\'))."</p></div>";
       echo $content;
      }
       else{
       return $content;
       }
}

add_filter(\'the_content\', \'append_the_content\');

add_action(\'admin_menu\',\'tweet_fetch\');

function tweet_fetch(){
add_options_page(\'Tweet\',\'Tweet\', 8, \'tweet\', \'tweet_fetcher\');
}
function tweet_fetcher(){
?>
<h2>Tweet Fetcher options</h2>
<table>
<form method=\'post\' action=\'options.php\' style=\'margin:0 20px;\'>
<?php wp_nonce_field(\'update-options\'); ?>
<tr><td>Twitter UserID:</td><td><input type="text" name="tweetID"  value="<?php echo get_option(\'tweetID\'); ?>" <?php echo get_option(\'tweetID\'); ?> />
</td></tr>

<input type=\'hidden\' name=\'action\' value=\'update\'/>
<input type=\'hidden\' name=\'page_options\' value=\'tweetID\'/>

<tr><td><p class=\'submit\'>
<input type=\'submit\' name=\'Submit\' value=\'Update Options &raquo;\'/>
</p></td></tr>
</table>
</form>
<?php
}
发帖后我怎样才能做到???

2 个回复
最合适的回答,由SO网友:Atif 整理而成

Edited, try this

<?php

function parse_twitter_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
    $feed = str_replace("&lt;", "<", $feed);
    $feed = str_replace("&gt;", ">", $feed);
    $clean = explode("<content type=\\"html\\">", $feed);

    $amount = count($clean) - 1;

    $output = $prefix;

    for ($i = 1; $i <= $amount; $i++) {
        $cleaner = explode("</content>", $clean[$i]);
        $output .= $tweetprefix;
        $output .= $cleaner[0];
        $output .= $tweetsuffix;
    }

    $output .= $suffix;

    return $output;
}

function the_twitter_feed($username) {
    // $username = "Mba_"; // Your twitter username.
    $limit = "5"; // Number of tweets to fetch.

    /* These prefixes and suffixes will display before and after the entire block of tweets. */
    $prefix = ""; // Prefix - some text you want displayed before all your tweets.
    $suffix = ""; // Suffix - some text you want displayed after all your tweets.
    $tweetprefix = "<b>" . $username . ": </b> "; // Tweet Prefix - some text you want displayed before each tweet.
    $tweetsuffix = "<br>"; // Tweet Suffix - some text you want displayed after each tweet.

    $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" .
        $limit;

    $twitterFeed = get_transient($feed);
    if (!$twitterFeed) {
        $twitterFeed = wp_remote_fopen($feed);
        set_transient($feed, $twitterFeed, 3600); // cache for an hour
    }
    if ($twitterFeed)
        return parse_twitter_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);
}

function append_the_content($content) {
    if (get_option(\'tweetID\') != null) {
        $content .= "<div class=\'post\'><p>" . the_twitter_feed(get_option(\'tweetID\')) .
            "</p></div>";
        return $content;
    } else {
        return $content;
    }
}

add_filter(\'the_content\', \'append_the_content\');

add_action(\'admin_menu\', \'tweet_fetch\');

function tweet_fetch() {
    add_options_page(\'Tweet\', \'Tweet\', 8, \'tweet\', \'tweet_fetcher\');
}

function tweet_fetcher() {

?>
<h2>Tweet Fetcher options</h2>
<table>
<form method=\'post\' action=\'options.php\' style=\'margin:0 20px;\'>
<?php

    wp_nonce_field(\'update-options\');

?>
<tr><td>Twitter UserID:</td><td><input type="text" name="tweetID"  value="<?php

    echo get_option(\'tweetID\');

?>" <?php

    echo get_option(\'tweetID\');

?> />
</td></tr>

<input type=\'hidden\' name=\'action\' value=\'update\'/>
<input type=\'hidden\' name=\'page_options\' value=\'tweetID\'/>

<tr><td><p class=\'submit\'>
<input type=\'submit\' name=\'Submit\' value=\'Update Options &raquo;\'/>
</p></td></tr>
</table>
</form>
<?php

}

?>
SO网友:Rarst

很可能是您的the_twitter_feed() 作用echoes结果,而不是return我在嘲笑他们。

结束

相关推荐

Pre_Comment_Content筛选器使用IntenseDebate增强型审核不起作用

我写了一个插件,可以修改一些评论的内容。它使用pre\\u comment\\u内容过滤器。使用标准WP调节似乎可以正常工作,但是,当我打开IntenseDebate增强调节时就不行了。以下是一些代码:function my_plugin($orig_comment){ $orig_comment = some_func($orig_comment); return $orig_comment; } 编辑:插件基本上只应用一些格式,比如说所有字母