Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressにjQueryタブウィジェットを追加する方法

人気サイトで、ワンクリックで人気投稿、最新投稿、おすすめ投稿を見ることができるタブエリアを見たことがあるだろうか。これはjQueryタブバーウィジェットと呼ばれ、異なるウィジェットを1つにまとめることでユーザー画面のスペースを節約することができます。今回は、WordPressにjQueryタブウィジェットを追加する方法を紹介する。

A jQuery powered tabber widget in WordPress

なぜjQuery Tabber Widgetを追加する必要があるのか?

WordPressサイトを運営していると、ドラッグ&ドロップのウィジェットを使って簡単にサイドバーに項目を追加することができます。サイトが大きくなるにつれて、サイドバーに十分なスペースがなく、有益なコンテンツをすべて表示できないと感じるかもしれません。そんな時に便利なのがタブバーです。同じエリアに異なる項目を表示することができます。ユーザーはそれぞれのタブをクリックし、最も興味のあるコンテンツを見ることができる。多くの有名サイトでは、今日、今週、今月の人気投稿を表示するのに使っています。このチュートリアルでは、タブウィジェットの作成方法を紹介します。ただし、タブに何を追加するかは紹介しません。基本的に、お好きなものを追加してください。

注: このチュートリアルは中級ユーザー向けで、HTMLとCSSの知識が必要です。初級レベルのユーザーはこの投稿を参照してください。

WordPressでjQueryタブウィジェットを作成する

始めよう。まず最初に、デスクトップにフォルダーを作成し、wPBeginner-tabber-widgetという名前をつけます。その後、メモ帳のようなプレーンテキストエディターを使って、このフォルダー内に3つのファイルを作成する必要があります。

最初に作成するファイルはwpb-tabber-widget.phpです。このファイルには、タブとWordPressカスタムウィジェットを作成するためのHTMLとPHPコードが含まれる。2番目に作成するファイルはwpb-tabber-style.cssで、タブコンテナのCSSスタイリングが含まれます。3番目と最後のファイルはwpb-tabber.jsで、タブを切り替えてアニメーションを追加するためのjQueryスクリプトが含まれる。

wpb-tabber-widget.phpファイルから始めましょう。このファイルの目的は、ウィジェットを登録するプラグインを作成することです。WordPressウィジェットを作成するのが初めての場合は、WordPressウィジェットのカスタマイゼーションガイドを参照するか、このコードをwpb-tabber-widget.phpファイルにコピー&ペーストすることをお勧めします:

[cbk1]

上記のコードでは、まずプラグインを作成し、そのプラグインの中にウィジェットを作成しました。ウィジェットの出力セクションにスクリプトとスタイルシートを追加し、タブのHTML出力を生成しました。最後に、ウィジェットを登録します。各タブに表示したいコンテンツを追加する必要があることを忘れないでください。

タブに必要なPHPとHTMLコードでウィジェットを作成したので、次のステップは、タブコンテナにタブとして表示するためにjQueryを追加することです。そのためには、このコードをwp-tabber.jsファイルにコピー&ペーストする必要があります。

[SKX1]
(function($) {
$(".tab_content").hide();
$("ul.tabs li:first").addClass("active").show();
$(".tab_content:first").show();
$("ul.tabs li").click(function() {)
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab_content").hide();
var activeTab = $(this).find("a").attr("href");
//$(activeTab).fadeIn();
if ($.browser.msie) {$(activeTab).show();}.
else {$(activeTab).fadeIn();}.
return false;
});
})(jQuery);

[SKX2]

これで、jQueryを使ったウィジェットの準備ができました。最後のステップは、タブにスタイルを追加することです。スタイルシートのサンプルを作成したので、wpb-tabber-style.cssファイルにコピー&ペーストしてください:

[cbk2]

これですべてです。あとはwpbeginner-tabber-widgetフォルダをFTPでWordPressサイトの/wp-content/plugins/ディレクトリにアップロードするだけです。また、このフォルダーをzipアーカイブに追加し、WordPress管理エリアのプラグイン ” 新規追加に移動することもできます。アップロードタブをクリックしてプラグインをインストールしてください。プラグインが有効化されたら、外観 ” ウィジェットに移動し、WPBeginner Tabber Widgetをサイドバーにドラッグ&ドロップしてください。

Drag and drop WPBeginner Tabber Widget into your Sidebar

このチュートリアルがWordPressサイトのjQueryタブ作成に役立てば幸いです。ご質問やフィードバックは、下にコメントを残すか、TwitterまたはGoogle+にご参加ください。

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

26件のコメント返信を残す

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Nitish Chauhan

    Hi,
    My plugin is activated but in the widget section it show “There are no options for this widget.” message.please tell me how to activate all the function and i want to create plugin like
    “jQuery(document).ready(function() {
    var wrapper = jQuery(“.input_fields_wrap”); //Fields wrapper
    var add_button = jQuery(“.add_field_button”); //Add button ID

    //initlal text box count
    jQuery(add_button).click(function(e){ //on add input button click
    e.preventDefault();
    //max input box allowed
    //text box increment
    jQuery(wrapper).prepend(‘×’); //add input box

    jQuery(‘.input_fields_wrap’).sortable();
    jQuery(‘.input_fields_wrap’).disableSelection();

    });

    jQuery(wrapper).on(“click”,”.remove_field”, function(e){ //user click on remove text
    e.preventDefault(); jQuery(this).parent(‘div’).remove();

    });

    });”

    my code of java script .please suggest if you have any solution.
    Thanks

  3. Nabam Rikam

    I have inserted the plugins in the sidebar, but when i try to click it says there is no option for this plugin. And after we browse it in website, we see three blank tbs. Guide me here a little bro.

  4. goutham

    Iam not gettng tabs r widgets in my site.i have copied on pasted same code.It is not working.

  5. Kunle

    i want to place the plugin just created in a place in my page, and not in the side bars or footer.
    how do i do that, to place it anywhere in my web page

  6. Zadius

    This is the second tutorial I have tried and for some reason the plugin file does not show up under the plugin directory on my site. I upload the file directly using FTP but when I log into my wordpress admin area nothing appears under the plugin’s tab. Please advise. Thank you.

    Update: I zipped the file and uploaded it via the wordpress plugin interface. The file does not appear in my plugin’s folder on my FTP interface so I have zero clue where it show’s up. But I got it installed so thanks!

  7. John

    Thank you for the tutorial. However, I noticed that the title is missing when I add the widget to the widget area. How can I add the title space to input a title?

  8. Drazen

    Hey

    Thanks for this. I was just wondering, how to add option, so that when I am viewing widget, I can simply paste links in it, in each tab?

    For example:
    Tab 1 (option to rename it in widget options)
    – Text box below it in widget options(so that I can add text, links etc.)

    Tab 2 (option to rename it in widget options)
    – Text box below it in widget options(so that I can add text, links etc.)

    Tab 3 (option to rename it in widget options)
    – Text box below it in widget options(so that I can add text, links etc.)

    Thanks

  9. Gavin Wilshen

    Brilliant tutorial. Thanks guys!

  10. Grant

    It keeps giving me this error:

    Plugin could not be activated because it triggered a fatal error.

    Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING in /home/content/11/10826211/html/wp-content/plugins/wpbeginner-tabber-widget/wpb-tabber-widget.php on line 16

  11. Rahul

    Thanks man you’re a genius. I was just going to buy a premium plugin from codecanyon and then found this guide.

  12. Jonathan

    Why is it that when I install this plugin it is saying it needs to be updated, and the update is from a another developer & is over 3 years old?

    • WPBeginner Support

      It should not do that. If you have changed the plugin name and it matches another plugin then WordPress would confuse it with the other plugin.

      管理者

      • Jonathan

        I didn’t change anything; I only did just what you showed above.

        • Jonathan

          This is the plugin that WordPress thinks it is & is trying to update it to. http://wordpress.org/plugins/tabber-widget/

          I just updated the plugin to version 2.0 & that (for whatever reason) got it to stop asking to update it to the other plugin. I’d try renaming & changing the other plugin info, but that was the only thing that seemed to work.

        • WPBeginner Support

          The only reason we can think of is that you probably named the plugin file or folder to tabber-widget.php instead of wpb-tabber-widget.php which caused WordPress to confuse the plugin with this other one. The version trick is ok too until this other plugin releases 2.0+ :) so its bed to clear the confusion.

        • WPBeginner Support

          We were unable to reproduce this. Do you have access to another WordPress site where you can try this, just to test that there is nothing wrong on your end?

  13. Doris

    This kind of defeats the purpose of WordPress being dynamic, doesn’t it? Hard coding text into a widget? Is there a way to pull dynamic content from the database? Us noobs don’t have much coding experience ya know…One would think there is a plugin that would do this…

    • WPBeginner Support

      This tutorial is aimed at intermediate level users and the goal here is to show them how to create a tabber widget. For beginner level users, there are several built in template tags that can dynamically generate content inside each tab. For example:

      Display a list of your WordPress pages:

      <ul>
      <?php wp_list_pages('title_li='); ?>
      </ul>
      

      Show Random Posts:

      <ul>
      <?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
      <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
      </li>
      <?php } ?>
      </ul>
      

      Show recent comments:

      <?php
      $args = array(
      	'status' => 'approve',
      	'number' => '5'
      );
      $comments = get_comments($args);
      foreach($comments as $comment) :
      	echo($comment->comment_author . '<br />' . $comment->comment_excerpt);
      endforeach;
      ?>
      

      And many more.

      管理者

  14. manoj sakhwar

    Nice article. thanks…

  15. Grant

    What I don’t understand is where to paste the code. What type of document do I put the code in? (I have mac).

  16. Keith Davis

    Love this one guys.
    Always looking for ways to make better use of limited real estate.

  17. Jim Davis

    Installed the files and activated the widget. It displays as expected, however, clicking the Tab 2 and Tab 3 tabs does not change the content. The content remains as the content under Tab 1. Have I missed something? See my test site at http://jimdavis.org/blog/

    Jim

    • WPBeginner Support

      Jim you have not missed any thing. This is an example widget and you can edit it. Enter your own code and content inside each tab by editing the plugin file wpb-tabber-widget.php

      管理者

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。