2012-02-08 8 views
1

私は望みの拡張性をもって欲しいものを見つけられなかったので、新しいプラグインを作成しようとしています。プラグインの目的は、簡単なショートコードを使用して、ブログの最新投稿を自動的に埋め込む画像スライダーを表示できるようにすることです。プラグインでthe_title()とthe_permalink()を使用するのに問題がある

私は基本的なプラグインファイルを用意して、ショートコードを実装してテストしました。今私はブログの最新の記事を取り込むためにプラグインを取得しようとしているが、私はちょっとしたものにぶち当たっている。 The_title()とthe_permalink()を使用すると、コードの外に表示されます。さらに、The_content()はthe_permalink()とthe_title()で一度表示され、次にもう一度表示されます。

hereの動作を確認できます。ここで は、私が使用しているコードです:

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_permalink = the_permalink(); // Get the post's permalink 
     $post_title = the_title();   // Get the post's title 
     $post_content = the_content();  // Get the post's content - will write code to get excerpt later 
     return '<h2><a href="' . $post_permalink . '">' . $post_title . '</a></h2>\n' 
     . '<p>' . $post_content . '</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'  => '',    // 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'); 

答えて

4

をデフォルトでは、the_titleは(エコー)のタイトルをプリントアウトします。

<?php the_title('', '', false); ?> 

しかし、それだけではなく、 get_the_titleを使用するようにしても良いでしょう - と同じことが、同様 get_the_contentget_permalinkのために行く:あなたはそうのように、それだけでタイトルを返すために取得するために、関数に追加のパラメータを渡すことができます。

+0

恐ろしく、これは間違いなく解決策でした。私はWPコーデックス・ショートコードのページでエコーの代わりにリターンを使用する方法を読んでいましたが、問題の解決方法がわかりませんでした。本当にありがとう! – trezy

+1

これは 'get_permalink'です。それはおそらくWordPressの中で最も迷惑な矛盾の1つです。 – Simon

+0

サイモンの権利は、get_permalinkです。私はコードでもう少し研究をしなければならなかった。 – trezy

関連する問題