ぺんです。
今回は、ワードプレスの親テーマのウイジットを子テーマで書き換える方法をご紹介します。
CONTENTS
親テーマのウイジットを書き換えた理由
以前ご紹介した、無料のワードプレステーマ「MagCast]を使って、別のサイトを運営しているのですが、このテーマは日本語対応していないため、日付のフォーマットが正しく表示されていないという不具合がありました。

左がオリジナルのウイジットの日付表記。右側の書式に変更したい。
ソースコードを調べてみると、日付表記の書式がウイジットの中にハードコーディングされていることが理由でした。そのため、日付表記の書式指定を下記のように書き換えてやればよいわけです。
get_the_time(‘j F Y’) → get_the_time(‘Y.m.d’)
ただし、親テーマのソースコードを直接書き換えてしまうと、親テーマをアップデートするたびに、カスタマイズした箇所が上書きされて消えてしまうので、親テーマのウイジットは、子テーマ上で書き換える必要があります。
親テーマのウイジットを子テーマで書き換える方法
親テーマのウイジット修正を子テーマで行う手順は、以下の通りです。
- 子テーマのウイジットを登録する()
- 親テーマのウィジットを消す()
そのやり方を検討するまえに、親テーマと子テーマのソースコードがどのような順番で処理されるのかを確認する必要があります。
親テーマと、子テーマのソースコードの処理順
しらべてみたところ、以下の手順でコードが処理されることが判りました。
1.子テーマ functions.php
2.親テーマ functions.php
3.子テーマ after_setup_theme アクションフック作動
4.親テーマ after_setup_theme アクションフック作動
ということは・・・
1.子テーマ functions.php
> 子テーマのウイジットを登録する()
2.親テーマ functions.php
3.子テーマ after_setup_theme アクションフック作動
> 親テーマのウイジットを消す()
4.親テーマ after_setup_theme アクションフック作動
という順番で処理する必要があります。
ポイントとしては・・・
- 親テーマのfunctions.phpの実行は止められないので、コピーした子テーマのウイジットコードに記載のClass名は、新しい物にする必要がある。
ということです。
参考にさせていただいたサイト
今回はこちらのサイトを参考にさせていただきました!
子テーマのウイジットを登録する
子テーマを登録するには、親テーマのウイジット登録用のソースファイルをコピーします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
magcast ├─font-awesome │ ├─css │ └─fonts ├─inc │ ├─admin │ │ ├─css │ │ └─js │ ├─footer-info │ ├─functions │ ├─structure │ └─widgets ├─js ├─languages ├─owlcarousel └─page-templates |
MagCastのウイジット登録用のコードは、そのまんまwidgetsというフォルダーに格納されている、magcast-widgets.php というファイルです。
まず、このファイルを子テーマのフォルダーの /inc/widgets フォルダーにコピーします。
magcast-widgets.phpの変更点
少々長いですが、以下をオリジナルから変更しています。
- 関数名をmagcast_widgets_init()からnew_magcast_widgets_init()に変更
- add_actionに新しい関数名、’new_magcast_widgets_init’を指定
- 既存のクラス名にnew_を追加して、ユニークなクラス名に変更
- 日付の書式フォーマットを変更 get_the_time(‘j F Y’) → get_the_time(‘Y.m.d’)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
<?php /** * Contains all the functions related to sidebar and widget. * * @package Theme Horse * @subpackage Magcast * @since Magcast 1.0 */ /****************************************************************************************/ add_action('after_setup_theme', 'new_magcast_widgets_init',20); /** * Function to register the widget areas(sidebar) and widgets. */ function new_magcast_widgets_init() { /* get_the_time('Y.m.d')-> get_the_time('Y.m.d') Replaced the Widgets to fix date formats. */ // Registering Header add sidebar register_sidebar(array( 'name' => __('Header Sidebar', 'magcast') , 'id' => 'magcast_headeradd_sidebar', 'description' => __('Show TH: Advertisement widget beside Site Logo/Title.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); // Registering Magazine Page template Primary register_sidebar(array( 'name' => __('Magazine Template Primary', 'magcast') , 'id' => 'magcast_magazine_template_primary', 'description' => __('Shows widgets on Magazine Page Template. Suitable widget: TH: Horizontal Vertical Post, TH: Two Column Category Post, TH: Two Column Grid, TH: Two Column Large Image', 'magcast') , 'before_widget' => '<section id="%1$s" class="widget %2$s">', 'after_widget' => '</section>', 'before_title' => '<h2 class="widget-title">', 'after_title' => '</h2>', )); // Registering Magazine Page template Secondary register_sidebar(array( 'name' => __('Magazine Template Secondary', 'magcast') , 'id' => 'magcast_magazine_template_secondary', 'description' => __('Shows widgets on Magazine Page Template. Suitable widget: TH: Horizontal Vertical Post, TH: Two Column Category Post, TH: Two Column Grid, TH: Two Column Large Image', 'magcast') , 'before_widget' => '<section id="%1$s" class="widget %2$s">', 'after_widget' => '</section>', 'before_title' => '<h2 class="widget-title">', 'after_title' => '</h2>', )); // Registering main left sidebar register_sidebar(array( 'name' => __('Left Sidebar', 'magcast') , 'id' => 'magcast_left_sidebar', 'description' => __('Shows widgets at Left side.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); // Registering main right sidebar register_sidebar(array( 'name' => __('Right Sidebar', 'magcast') , 'id' => 'magcast_right_sidebar', 'description' => __('Shows widgets at Right side.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); // Registering contact Page sidebar register_sidebar(array( 'name' => __('Contact Template Sidebar', 'magcast') , 'id' => 'magcast_contact_page_sidebar', 'description' => __('Shows widgets on Contact Page Template.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); /** * Registering footer sidebar 1 * For upgrade compatible reason footer id not kept magcast_footer_column1 */ register_sidebar(array( 'name' => __('Footer - Column1', 'magcast') , 'id' => 'magcast_footer_sidebar', 'description' => __('Shows widgets at footer Column 1.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); // Registering footer sidebar 2 register_sidebar(array( 'name' => __('Footer - Column2', 'magcast') , 'id' => 'magcast_footer_column2', 'description' => __('Shows widgets at footer Column 2.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); // Registering footer sidebar 3 register_sidebar(array( 'name' => __('Footer - Column3', 'magcast') , 'id' => 'magcast_footer_column3', 'description' => __('Shows widgets at footer Column 3.', 'magcast') , 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); // Registering widgets register_widget("new_magcast_advertisement_widget"); register_widget("new_magcast_two_column_largeimage_widget"); register_widget("new_magcast_horizontal_vertical_widget"); register_widget("new_magcast_two_column_category_widget"); register_widget("new_magcast_two_column_grid_widget"); } /****************************************************************************************/ /** * Widget for magazine that shows selected page content,title and featured image. * Construct the widget. * i.e. Name, description and control options. */ class new_magcast_two_column_largeimage_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'widget_2_column_large_image clearfix', 'description' => __('NEW:Display Two Column Large Image (Magazine Template)', 'workstyle') ); $control_ops = array( 'width' => 200, 'height' => 250 ); parent::__construct(false, $name = __('WS: Two Column Large Image', 'workstyle') , $widget_ops, $control_ops); } function form($instance) { $instance = wp_parse_args((array)$instance, array( 'magcast_title' => '', 'magcast_link'=>'', 'magcast_date' => false, 'magcast_author' => false, 'magcast_category' =>'', 'magcast_number' => 2 )); $magcast_title = esc_attr($instance['magcast_title']); $magcast_link = esc_url($instance['magcast_link']); $magcast_date = absint($instance['magcast_date']); $magcast_author = absint($instance['magcast_author']); $magcast_category = esc_attr($instance['magcast_category']); $magcast_number = (int)$instance['magcast_number']; ?> <p> <label for="<?php echo $this->get_field_id('magcast_number'); ?>"> <?php esc_html_e('Number of Post:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_number'); ?>" name="<?php echo $this->get_field_name('magcast_number'); ?>" type="text" value="<?php echo (int)$magcast_number; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'magcast_date' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_date'] ); ?> id="<?php echo $this->get_field_id( 'magcast_date' ); ?>" name="<?php echo $this->get_field_name( 'magcast_date' ); ?>" /> <?php esc_html_e( 'Hide Date', 'magcast' ); ?> </label> <label for="<?php echo $this->get_field_id( 'magcast_author' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_author'] ); ?> id="<?php echo $this->get_field_id( 'magcast_author' ); ?>" name="<?php echo $this->get_field_name( 'magcast_author' ); ?>" /> <?php esc_html_e( 'Hide Author', 'magcast' ); ?> </label> </p> <hr> <p> <label for="<?php echo $this->get_field_id('magcast_title'); ?>"> <?php esc_html_e('Title:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_title'); ?>" name="<?php echo $this->get_field_name('magcast_title'); ?>" type="text" value="<?php echo esc_attr($magcast_title); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('magcast_link'); ?>"> <?php esc_html_e('Link:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_link'); ?>" name="<?php echo $this->get_field_name('magcast_link'); ?>" type="text" value="<?php echo esc_url($magcast_link); ?>" /> </p> <label for="<?php echo $this->get_field_id('magcast_category'); ?>"> <?php esc_html_e('Category', 'magcast'); ?> :</label> <?php wp_dropdown_categories(array( 'show_option_none' => ' ', 'name' => $this->get_field_name('magcast_category') , 'selected' => $instance['magcast_category' ] )); ?> <hr> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['magcast_title'] = esc_attr($new_instance['magcast_title']); $instance['magcast_link'] = esc_url($new_instance['magcast_link']); $instance['magcast_date'] = magcast_sanitize_checkbox($new_instance['magcast_date']); $instance['magcast_author'] = magcast_sanitize_checkbox($new_instance['magcast_author']); $instance['magcast_category'] = esc_attr($new_instance['magcast_category']); $instance['magcast_number'] = (int)$new_instance['magcast_number']; return $instance; } function widget($args, $instance) { extract($args); extract($instance); global $post; $magcast_title = isset( $instance[ 'magcast_title' ] ) ? $instance[ 'magcast_title' ] : ''; $magcast_link = isset( $instance[ 'magcast_link' ] ) ? $instance[ 'magcast_link' ] : ''; $magcast_date = isset( $instance[ 'magcast_date' ] ) ? $instance[ 'magcast_date' ] : false; $magcast_author = isset( $instance[ 'magcast_author' ] ) ? $instance[ 'magcast_author' ] : false; $magcast_category = isset( $instance[ 'magcast_category' ] ) ? $instance[ 'magcast_category' ] : ''; $magcast_number = isset( $instance[ 'magcast_number' ] ) ? $instance[ 'magcast_number' ] : ''; $get_featured_pages = new WP_Query(array( 'posts_per_page' => $magcast_number, 'post_type' => array( 'post' ) , 'category__in' => $magcast_category, )); echo $before_widget; $i=1; if(!empty($magcast_title)){ ?> <h2 class="widget-title"><a href="<?php echo esc_url($magcast_link); ?>" title="<?php echo esc_attr($magcast_title); ?>"><?php echo esc_attr($magcast_title); ?></a></h2> <?php } ?> <div class="owl-carousel clearfix"> <?php while ($get_featured_pages->have_posts()): $get_featured_pages->the_post(); ?> <article <?php post_class('col'); ?>> <?php if ( has_post_thumbnail() ) { ?> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-large-thumb'); ?></a> <span class="cat-links"> <?php do_action( 'magcast_post_categories' ); ?> </span> </figure><!-- .post-featured-image --> <?php } ?> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('j F Y') ); ?>"><?php echo esc_attr( get_the_time('j F Y') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </article><!-- .post .col --> <?php endwhile; // Reset Post Data wp_reset_query(); ?> </div><!-- .owl-carousel --> <?php echo $after_widget . '<!--.widget_2_column_large_image -->'; } } /****************************************************************************************/ /** * Widget for magazine that shows selected page content,title and featured image. * Construct the widget. * i.e. Name, description and control options. */ class new_magcast_advertisement_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'widget_add_size_728x90', 'description' => __('New:Display Advertisement', 'workstyle') ); $control_ops = array( 'width' => 200, 'height' => 250 ); parent::__construct(false, $name = __('WS: Advertisement', 'workstyle') , $widget_ops, $control_ops); } function form($instance) { $instance = wp_parse_args((array)$instance, array( 'magcast_advertisement_image' => '', 'magcast_advertisement_image_url' => '' )); $magcast_advertisement_image = esc_url($instance['magcast_advertisement_image']); $magcast_advertisement_image_url = esc_url($instance['magcast_advertisement_image_url']); ?> <p> <label for="<?php echo $this->get_field_id('magcast_advertisement_image'); ?>"> <?php esc_html_e('Image Link:', 'magcast'); ?> </label> <input type="text" class="upload1" id="<?php echo $this->get_field_id( 'magcast_advertisement_image' ); ?>" name="<?php echo $this->get_field_name('magcast_advertisement_image'); ?>" value="<?php echo esc_url($magcast_advertisement_image); ?>"/> <input type="button" class="button custom_media_button"name="<?php echo $this->get_field_name('magcast_advertisement_image'); ?>" id="custom_media_button_services" value="<?php esc_attr_e('Upload Image','magcast');?>" onclick="mediaupload.uploader( '<?php echo $this->get_field_id( 'magcast_advertisement_image' ); ?>' ); return false;"/> </p> <p> <label for="<?php echo $this->get_field_id('magcast_advertisement_image_url'); ?>"> <?php esc_html_e('Redirect Url:', 'magcast'); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id('magcast_advertisement_image_url'); ?>" name="<?php echo $this->get_field_name('magcast_advertisement_image_url'); ?>" type="text" value="<?php echo $magcast_advertisement_image_url; ?>" /> </p> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['magcast_advertisement_image'] = esc_url($new_instance['magcast_advertisement_image']); $instance['magcast_advertisement_image_url'] = esc_url($new_instance['magcast_advertisement_image_url']); return $instance; } function widget($args, $instance) { extract($args); extract($instance); $magcast_advertisement_image = apply_filters('magcast_advertisement_image', empty($instance['magcast_advertisement_image']) ? '' : $instance['magcast_advertisement_image'], $instance, $this->id_base); $magcast_advertisement_image_url = apply_filters('magcast_advertisement_image_url', empty($instance['magcast_advertisement_image_url']) ? '' : $instance['magcast_advertisement_image_url'], $instance, $this->id_base); echo $before_widget; ?> <a href="<?php echo esc_url($magcast_advertisement_image_url); ?>" title="add_size_728x90"><img alt="add_size_728x90" src="<?php echo esc_url($magcast_advertisement_image); ?>"></a> <?php echo $after_widget; } } /****************************************************************************************/ /** * Widget for magazine that shows selected page content,title and featured image. * Construct the widget. * i.e. Name, description and control options. */ class new_magcast_horizontal_vertical_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'widget_horizontal_vertical_post clearfix', 'description' => __('NEW:Display Horizontal Vertical Post (Magazine Template)', 'workstyle') ); $control_ops = array( 'width' => 200, 'height' => 250 ); parent::__construct(false, $name = __('WS: Horizontal Vertical Post', 'workstyle') , $widget_ops, $control_ops); } function form($instance) { $instance = wp_parse_args((array)$instance, array( 'magcast_title' => '', 'magcast_link'=>'', 'magcast_date' => false, 'magcast_author' => false, 'magcast_category' =>'', 'magcast_number' => 5, 'magcast_horizontal'=> true, 'magcast_vertical'=> false )); $magcast_title = esc_attr($instance['magcast_title']); $magcast_link = esc_url($instance['magcast_link']); $magcast_date = absint($instance['magcast_date']); $magcast_author = absint($instance['magcast_author']); $magcast_category = esc_attr($instance['magcast_category']); $magcast_number = (int)$instance['magcast_number']; $featured_display = ( isset( $instance['featured_display'] ) && is_numeric( $instance['featured_display'] ) ) ? (int) $instance['featured_display'] : 1; ?> <p> <label for="<?php echo $this->get_field_id('magcast_number'); ?>"> <?php esc_html_e('Number of Post:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_number'); ?>" name="<?php echo $this->get_field_name('magcast_number'); ?>" type="text" value="<?php echo (int)$magcast_number; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'magcast_date' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_date'] ); ?> id="<?php echo $this->get_field_id( 'magcast_date' ); ?>" name="<?php echo $this->get_field_name( 'magcast_date' ); ?>" /> <?php esc_html_e( 'Hide Date', 'magcast' ); ?> </label> <label for="<?php echo $this->get_field_id( 'magcast_author' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_author'] ); ?> id="<?php echo $this->get_field_id( 'magcast_author' ); ?>" name="<?php echo $this->get_field_name( 'magcast_author' ); ?>" /> <?php esc_html_e( 'Hide Author', 'magcast' ); ?> </label> </p> <p> <legend><?php esc_html_e('Post Display Option:','magcast');?></legend> <input type="radio" id="<?php echo ($this->get_field_id( 'featured_display' ) . '-1') ?>" name="<?php echo ($this->get_field_name( 'featured_display' )) ?>" value="1" <?php checked( $featured_display == 1, true) ?>> <label for="<?php echo ($this->get_field_id( 'featured_display' ) . '-1' ) ?>"><?php esc_html_e('Horizontal', 'magcast') ?></label> <input type="radio" id="<?php echo ($this->get_field_id( 'featured_display' ) . '-2') ?>" name="<?php echo ($this->get_field_name( 'featured_display' )) ?>" value="2" <?php checked( $featured_display == 2, true) ?>> <label for="<?php echo ($this->get_field_id( 'featured_display' ) . '-2' ) ?>"><?php esc_html_e('Vertical', 'magcast') ?></label> </p> <hr> <p> <label for="<?php echo $this->get_field_id('magcast_title'); ?>"> <?php esc_html_e('Title:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_title'); ?>" name="<?php echo $this->get_field_name('magcast_title'); ?>" type="text" value="<?php echo esc_attr($magcast_title); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('magcast_link'); ?>"> <?php esc_html_e('Link:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_link'); ?>" name="<?php echo $this->get_field_name('magcast_link'); ?>" type="text" value="<?php echo esc_url($magcast_link); ?>" /> </p> <label for="<?php echo $this->get_field_id('magcast_category'); ?>"> <?php esc_html_e('Category', 'magcast'); ?> :</label> <?php wp_dropdown_categories(array( 'show_option_none' => ' ', 'name' => $this->get_field_name('magcast_category') , 'selected' => $instance['magcast_category' ] )); ?> <hr> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['magcast_title'] = esc_attr($new_instance['magcast_title']); $instance['magcast_link'] = esc_url($new_instance['magcast_link']); $instance['magcast_date'] = magcast_sanitize_checkbox($new_instance['magcast_date']); $instance['magcast_author'] = magcast_sanitize_checkbox($new_instance['magcast_author']); $instance['magcast_category'] = esc_attr($new_instance['magcast_category']); $instance['magcast_number'] = (int)$new_instance['magcast_number']; $instance['featured_display'] = ( isset( $new_instance['featured_display'] ) && $new_instance['featured_display'] > 0 && $new_instance['featured_display'] < 3 ) ? (int) $new_instance['featured_display'] : 0; return $instance; } function widget($args, $instance) { extract($args); extract($instance); global $post; $magcast_title = isset( $instance[ 'magcast_title' ] ) ? $instance[ 'magcast_title' ] : ''; $magcast_link = isset( $instance[ 'magcast_link' ] ) ? $instance[ 'magcast_link' ] : ''; $magcast_date = isset( $instance[ 'magcast_date' ] ) ? $instance[ 'magcast_date' ] : false; $magcast_author = isset( $instance[ 'magcast_author' ] ) ? $instance[ 'magcast_author' ] : false; $magcast_category = isset( $instance[ 'magcast_category' ] ) ? $instance[ 'magcast_category' ] : ''; $magcast_number = isset( $instance[ 'magcast_number' ] ) ? $instance[ 'magcast_number' ] : ''; $featured_display = ( isset( $instance['featured_display'] ) && is_numeric( $instance['featured_display'] ) ) ? (int) $instance['featured_display'] : 1; $get_featured_pages = new WP_Query(array( 'posts_per_page' => $magcast_number, 'post_type' => array( 'post' ) , 'category__in' => $magcast_category, )); echo $before_widget; $i=1; if(!empty($magcast_title)){ ?> <h2 class="widget-title"><a href="<?php echo esc_url($magcast_link); ?>" title="<?php echo esc_attr($magcast_title); ?>"><?php echo esc_attr($magcast_title); ?></a></h2> <?php } if($featured_display == 1){ $classes = 'horizontal-post clearfix'; }else{ $classes = 'vertical-post widget-column-wrap clearfix'; } ?> <div class="<?php echo esc_attr($classes); ?>"> <?php while ($get_featured_pages->have_posts()): $get_featured_pages->the_post(); if($i==1){ ?> <article <?php post_class('col clearfix'); ?>> <?php if ( has_post_thumbnail() ) { ?> <div class="post-featured-image-wrap"> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-large-thumb'); ?></a> <span class="cat-links"> <?php do_action( 'magcast_post_categories' ); ?> </span> </figure><!-- .post-featured-image --> </div><!-- .post-featured-image-wrap --> <?php } ?> <div class="post-featured-content"> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> <div class="entry-content"> <?php the_excerpt(); ?> </div><!-- .entry-content --> </div><!-- .post-featured-content --> </article><!-- .post --> <div class="thumbnail-post col clearfix"> <?php } else { ?> <div class="single-thumbnail-post"> <article <?php post_class('clearfix'); ?>> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-small-thumb'); ?></a> </figure><!-- .post-featured-image --> <div class="post-featured-content"> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </div><!-- .post-featured-content --> </article><!-- .post --> </div><!-- .single-thumbnail-post --> <?php } $i++; endwhile; // Reset Post Data wp_reset_query(); ?> </div><!-- .thumbnail-post col --> </div><!-- .<?php echo esc_attr($classes); ?> --> <?php echo $after_widget . '<!--.widget_horizontal_vertical_post -->'; } } /****************************************************************************************/ /** * Widget for magazine that shows selected page content,title and featured image. * Construct the widget. * i.e. Name, description and control options. */ class new_magcast_two_column_category_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'widget_2_column_category widget-column-wrap clearfix', 'description' => __('New:Display Two Column Category Post (Magazine Template)', 'workstyle') ); $control_ops = array( 'width' => 200, 'height' => 250 ); parent::__construct(false, $name = __('WS: Two Column Category Post', 'workstyle') , $widget_ops, $control_ops); } function form($instance) { $instance = wp_parse_args((array)$instance, array( 'magcast_first_title' => '', 'magcast_first_link'=>'', 'magcast_second_title' => '', 'magcast_second_link'=>'', 'magcast_date' => false, 'magcast_author' => false, 'magcast_category_first' =>'', 'magcast_category_second' =>'', 'magcast_number' => 5 )); $magcast_first_title = esc_attr($instance['magcast_first_title']); $magcast_second_title = esc_attr($instance['magcast_second_title']); $magcast_first_link = esc_url($instance['magcast_first_link']); $magcast_second_link = esc_url($instance['magcast_second_link']); $magcast_date = absint($instance['magcast_date']); $magcast_author = absint($instance['magcast_author']); $magcast_category_first = esc_attr($instance['magcast_category_first']); $magcast_category_second = esc_attr($instance['magcast_category_second']); $magcast_number = (int)$instance['magcast_number']; ?> <p> <label for="<?php echo $this->get_field_id('magcast_number'); ?>"> <?php esc_html_e('Number of Post:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_number'); ?>" name="<?php echo $this->get_field_name('magcast_number'); ?>" type="text" value="<?php echo (int)$magcast_number; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'magcast_date' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_date'] ); ?> id="<?php echo $this->get_field_id( 'magcast_date' ); ?>" name="<?php echo $this->get_field_name( 'magcast_date' ); ?>" /> <?php esc_html_e( 'Hide Date', 'magcast' ); ?> </label> <label for="<?php echo $this->get_field_id( 'magcast_author' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_author'] ); ?> id="<?php echo $this->get_field_id( 'magcast_author' ); ?>" name="<?php echo $this->get_field_name( 'magcast_author' ); ?>" /> <?php esc_html_e( 'Hide Author', 'magcast' ); ?> </label> </p> <hr> <p> <label for="<?php echo $this->get_field_id('magcast_first_title'); ?>"> <?php esc_html_e('First Column Title:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_first_title'); ?>" name="<?php echo $this->get_field_name('magcast_first_title'); ?>" type="text" value="<?php echo esc_attr($magcast_first_title); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('magcast_first_link'); ?>"> <?php esc_html_e('First Column Link:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_first_link'); ?>" name="<?php echo $this->get_field_name('magcast_first_link'); ?>" type="text" value="<?php echo esc_url($magcast_first_link); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('magcast_category_first'); ?>"> <?php esc_html_e('First Column Category', 'magcast'); ?> :</label> <?php wp_dropdown_categories(array( 'show_option_none' => ' ', 'name' => $this->get_field_name('magcast_category_first') , 'selected' => $instance['magcast_category_first' ] )); ?> </p> <hr> <p> <label for="<?php echo $this->get_field_id('magcast_second_title'); ?>"> <?php esc_html_e('Second Column Title:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_second_title'); ?>" name="<?php echo $this->get_field_name('magcast_second_title'); ?>" type="text" value="<?php echo esc_attr($magcast_second_title); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('magcast_second_link'); ?>"> <?php esc_html_e('Second Column Link:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_second_link'); ?>" name="<?php echo $this->get_field_name('magcast_second_link'); ?>" type="text" value="<?php echo esc_url($magcast_second_link); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('magcast_category_second'); ?>"> <?php esc_html_e('Second Column Category', 'magcast'); ?> :</label> <?php wp_dropdown_categories(array( 'show_option_none' => ' ', 'name' => $this->get_field_name('magcast_category_second') , 'selected' => $instance['magcast_category_second' ] )); ?> </p> <hr> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['magcast_first_title'] = esc_attr($new_instance['magcast_first_title']); $instance['magcast_first_link'] = esc_url($new_instance['magcast_first_link']); $instance['magcast_second_title'] = esc_attr($new_instance['magcast_second_title']); $instance['magcast_second_link'] = esc_url($new_instance['magcast_second_link']); $instance['magcast_date'] = magcast_sanitize_checkbox($new_instance['magcast_date']); $instance['magcast_author'] = magcast_sanitize_checkbox($new_instance['magcast_author']); $instance['magcast_category_first'] = esc_attr($new_instance['magcast_category_first']); $instance['magcast_category_second'] = esc_attr($new_instance['magcast_category_second']); $instance['magcast_number'] = (int)$new_instance['magcast_number']; return $instance; } function widget($args, $instance) { extract($args); extract($instance); global $post; $magcast_first_title = isset( $instance[ 'magcast_first_title' ] ) ? $instance[ 'magcast_first_title' ] : ''; $magcast_first_link = isset( $instance[ 'magcast_first_link' ] ) ? $instance[ 'magcast_first_link' ] : ''; $magcast_second_title = isset( $instance[ 'magcast_second_title' ] ) ? $instance[ 'magcast_second_title' ] : ''; $magcast_second_link = isset( $instance[ 'magcast_second_link' ] ) ? $instance[ 'magcast_second_link' ] : ''; $magcast_date = isset( $instance[ 'magcast_date' ] ) ? $instance[ 'magcast_date' ] : false; $magcast_author = isset( $instance[ 'magcast_author' ] ) ? $instance[ 'magcast_author' ] : false; $magcast_category_first = isset( $instance[ 'magcast_category_first' ] ) ? $instance[ 'magcast_category_first' ] : ''; $magcast_category_second = isset( $instance[ 'magcast_category_second' ] ) ? $instance[ 'magcast_category_second' ] : ''; $magcast_number = isset( $instance[ 'magcast_number' ] ) ? $instance[ 'magcast_number' ] : ''; $get_featured_category_first = new WP_Query(array( 'posts_per_page' => $magcast_number, 'post_type' => array( 'post' ) , 'category__in' => $magcast_category_first, )); $get_featured_category_second = new WP_Query(array( 'posts_per_page' => $magcast_number, 'post_type' => array( 'post' ) , 'category__in' => $magcast_category_second, )); echo $before_widget; $i=1; ?> <div class="col"> <?php if(!empty($magcast_first_title)){ ?> <h2 class="widget-title"><a href="<?php echo esc_url($magcast_first_link); ?>" title="<?php echo esc_attr($magcast_first_title); ?>"><?php echo esc_attr($magcast_first_title); ?></a></h2> <?php } ?> <?php while ($get_featured_category_first->have_posts()): $get_featured_category_first->the_post(); if($i==1){ ?> <article <?php post_class(); ?>> <?php if ( has_post_thumbnail() ) { ?> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-large-thumb'); ?></a> <div class="entry-header"> <span class="cat-links"> <?php do_action( 'magcast_post_categories' ); ?> </span> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </div><!-- .entry-header --> </figure><!-- .post-featured-image --> <?php } ?> </article><!-- .post --> <div class="thumbnail-post"> <?php } else {?> <article <?php post_class('clearfix'); ?>> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-small-thumb'); ?></a> </figure><!-- .post-featured-image --> <div class="post-featured-content"> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </div><!-- .post-featured-content --> </article><!-- .post --> <?php } $i++; endwhile; // Reset Post Data wp_reset_query(); ?> </div><!-- .thumbnail-post --> </div><!-- .col --> <?php $j=1; ?> <div class="col"> <?php if(!empty($magcast_second_title)){ ?> <h2 class="widget-title"><a href="<?php echo esc_url($magcast_second_link); ?>" title="<?php echo esc_attr($magcast_second_title); ?>"><?php echo esc_attr($magcast_second_title); ?></a></h2> <?php } ?> <?php while ($get_featured_category_second->have_posts()): $get_featured_category_second->the_post(); if($j==1){ ?> <article <?php post_class('clearfix'); ?>> <?php if ( has_post_thumbnail() ) { ?> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-large-thumb'); ?></a> <div class="entry-header"> <span class="cat-links"> <?php do_action( 'magcast_post_categories' ); ?> </span> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </div><!-- .entry-header --> </figure><!-- .post-featured-image --> <?php } ?> </article><!-- .post --> <div class="thumbnail-post"> <?php } else {?> <article <?php post_class('clearfix'); ?>> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-small-thumb'); ?></a> </figure><!-- .post-featured-image --> <div class="post-featured-content"> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </div><!-- .post-featured-content --> </article><!-- .post --> <?php } $j++; endwhile; // Reset Post Data wp_reset_query(); ?> </div><!-- .thumbnail-post --> </div><!-- .col --> <?php echo $after_widget . '<!-- .widget_2_column_category .widget-column-wrap -->'; } } /****************************************************************************************/ /** * Widget for magazine that shows selected page content,title and featured image. * Construct the widget. * i.e. Name, description and control options. */ class new_magcast_two_column_grid_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'widget_2_column_grid clearfix', 'description' => __('New:Display Two Column Grid (Magazine Template)', 'workstyle') ); $control_ops = array( 'width' => 200, 'height' => 250 ); parent::__construct(false, $name = __('WS: Two Column Grid', 'workstyle') , $widget_ops, $control_ops); } function form($instance) { $instance = wp_parse_args((array)$instance, array( 'title' => '', 'link'=>'', 'magcast_date' => false, 'magcast_author' => false, 'category' =>'', 'magcast_number' => 4, 'hide_title'=>false )); $title = esc_attr($instance['title']); $link = esc_url($instance['link']); $magcast_date = absint($instance['magcast_date']); $magcast_author = absint($instance['magcast_author']); $hide_title = absint($instance['hide_title']); $category = esc_attr($instance['category']); $magcast_number = (int)$instance['magcast_number']; ?> <p> <label for="<?php echo $this->get_field_id('magcast_number'); ?>"> <?php esc_html_e('Number of Post:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('magcast_number'); ?>" name="<?php echo $this->get_field_name('magcast_number'); ?>" type="text" value="<?php echo (int)$magcast_number; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'magcast_date' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_date'] ); ?> id="<?php echo $this->get_field_id( 'magcast_date' ); ?>" name="<?php echo $this->get_field_name( 'magcast_date' ); ?>" /> <?php esc_html_e( 'Hide Date', 'magcast' ); ?> </label> <label for="<?php echo $this->get_field_id( 'magcast_author' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['magcast_author'] ); ?> id="<?php echo $this->get_field_id( 'magcast_author' ); ?>" name="<?php echo $this->get_field_name( 'magcast_author' ); ?>" /> <?php esc_html_e( 'Hide Author', 'magcast' ); ?> </label> <label for="<?php echo $this->get_field_id( 'hide_title' ); ?>"> <input class="checkbox" type="checkbox" <?php checked( $instance['hide_title'] ); ?> id="<?php echo $this->get_field_id( 'hide_title' ); ?>" name="<?php echo $this->get_field_name( 'hide_title' ); ?>" /> <?php esc_html_e( 'Hide Title', 'magcast' ); ?> </label> </p> <hr> <p> <label for="<?php echo $this->get_field_id('title'); ?>"> <?php esc_html_e('Title:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('link'); ?>"> <?php esc_html_e('Link:', 'magcast'); ?> </label> <input id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo esc_url($link); ?>" /> </p> <label for="<?php echo $this->get_field_id('category'); ?>"> <?php esc_html_e('Category', 'magcast'); ?> :</label> <?php wp_dropdown_categories(array( 'show_option_none' => ' ', 'name' => $this->get_field_name('category') , 'selected' => $instance['category' ] )); ?> <hr> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = esc_attr($new_instance['title']); $instance['link'] = esc_url($new_instance['link']); $instance['magcast_date'] = magcast_sanitize_checkbox($new_instance['magcast_date']); $instance['magcast_author'] = magcast_sanitize_checkbox($new_instance['magcast_author']); $instance['hide_title'] = magcast_sanitize_checkbox($new_instance['hide_title']); $instance['category'] = esc_attr($new_instance['category']); $instance['magcast_number'] = (int)$new_instance['magcast_number']; return $instance; } function widget($args, $instance) { extract($args); extract($instance); global $post; $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : ''; $link = isset( $instance[ 'link' ] ) ? $instance[ 'link' ] : ''; $magcast_date = isset( $instance[ 'magcast_date' ] ) ? $instance[ 'magcast_date' ] : false; $magcast_author = isset( $instance[ 'magcast_author' ] ) ? $instance[ 'magcast_author' ] : false; $hide_title = isset( $instance[ 'hide_title' ] ) ? $instance[ 'hide_title' ] : false; $category = isset( $instance[ 'category' ] ) ? $instance[ 'category' ] : ''; $magcast_number = isset( $instance[ 'magcast_number' ] ) ? $instance[ 'magcast_number' ] : ''; $get_featured_pages = new WP_Query(array( 'posts_per_page' => $magcast_number, 'post_type' => array( 'post' ) , 'category__in' => $category, )); echo $before_widget; $i=1; if(!empty($title)){ ?> <h2 class="widget-title"><a href="<?php echo esc_url($link); ?>" title="<?php echo esc_attr($title); ?>"><?php echo esc_attr($title); ?></a></h2> <?php } ?> <div class="widget-column-wrap clearfix"> <?php while ($get_featured_pages->have_posts()): $get_featured_pages->the_post(); ?> <article <?php post_class('col'); ?>> <?php if ( has_post_thumbnail() ) { ?> <figure class="post-featured-image"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_post_thumbnail('magcast-small-thumb'); ?></a> <span class="cat-links"> <?php do_action( 'magcast_post_categories' ); ?> </span> </figure><!-- .post-featured-image --> <?php } if($hide_title == false){ ?> <h2 class="entry-title"> <a title="<?php the_title(); ?>" href="<?php the_permalink();?>"><?php the_title(); ?></a> </h2><!-- .entry-title --> <?php } if(($magcast_date == false) || ($magcast_author == false)){ ?> <div class="entry-meta clearfix"> <?php if($magcast_date == false){ ?> <div class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time('Y.m.d') ); ?>"><?php echo esc_attr( get_the_time('Y.m.d') ); ?></a></div> <?php } if($magcast_author == false){?> <div class="by-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php the_author(); ?>"><?php the_author(); ?> </a></div> <?php } ?> </div><!-- .entry-meta --> <?php } ?> </article><!-- .post .col --> <?php endwhile; // Reset Post Data wp_reset_query(); ?> </div><!--.widget-column-wrap --> <?php echo $after_widget . '<!--.widget_2_column_grid -->'; } }?> |
親テーマのウイジットを消す
親テーマのウイジットは、子テーマのfunction.php に記載します。
以下が変更点です。
- 親テーマのウイジットを削除(18行目~29行目)
- 子テーマのウイジットソースコードの読み込み(30行目)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php /** * 子テーマを使う */ add_action('wp_enqueue_scripts', 'theme_enqueue_styles'); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } /* 外部ファイル読み込み */ require_once locate_template('inc/structure/header-extensions_new.php'); require_once locate_template('inc/structure/footer-extensions_new.php'); require_once locate_template('inc/structure/content-extensions_new.php'); function remove_magcast_widgets(){ // magcastのウイジットを削除する。 // Pen : Remove original widget. remove_action('widgets_init', 'magcast_widgets_init'); unregister_widget('magcast_advertisement_widget'); unregister_widget('magcast_two_column_largeimage_widget'); unregister_widget('magcast_horizontal_vertical_widget'); unregister_widget('magcast_two_column_category_widget'); unregister_widget('magcast_two_column_grid_widget'); } add_action('after_setup_theme', 'remove_magcast_widgets',20); require_once locate_template('inc/widgets/magcast-widgets.php'); //--------------------------------------------------------------------------- // スマホならtrue, タブレット・PCならfalseを返す // https://ruuski.net/web/how-smartphone-friendly-function-is_mobile-works/ //--------------------------------------------------------------------------- function is_mobile(){ $useragents = array( 'iPhone', // iPhone 'iPod', // iPod touch 'Android', // 1.5+ Android 'dream', // Pre 1.5 Android 'CUPCAKE', // 1.5+ Android 'blackberry9500', // Storm 'blackberry9530', // Storm 'blackberry9520', // Storm v2 'blackberry9550', // Storm v2 'blackberry9800', // Torch 'webOS', // Palm Pre Experimental 'incognito', // Other iPhone browser 'webmate' // Other iPhone browser ); $pattern = '/'.implode('|', $useragents).'/i'; return preg_match($pattern, $_SERVER['HTTP_USER_AGENT']); } // 自動整形停止 add_action('init', function() { remove_filter('the_excerpt', 'wpautop'); remove_filter('the_content', 'wpautop'); }); add_filter('tiny_mce_before_init', function($init) { $init['wpautop'] = false; $init['apply_source_formatting'] = ture; return $init; }); ?> |
まとめ
いかがでしたか?
仕組みさえ判れば、子テーマから親テーマのカスタマイズをすることは比較的容易ですね!
MagCast以外のテーマにも、同様に使えるテクニックだと思います。
では!
この記事へのコメントはありません。