{"id":2047,"date":"2018-08-19T19:36:31","date_gmt":"2018-08-19T10:36:31","guid":{"rendered":"https:\/\/xn--w8jyfva.jp\/box\/?p=2047"},"modified":"2019-04-13T20:18:10","modified_gmt":"2019-04-13T11:18:10","slug":"add-widget","status":"publish","type":"post","link":"https:\/\/xn--w8jyfva.jp\/box\/add-widget\/","title":{"rendered":"\u3010WordPress\u3011\u8d85\u7c21\u5358\uff01\u6700\u521d\u306eh2\u4e0a\uff06\u672c\u6587\u4e0b\u306b\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u30a8\u30ea\u30a2\u3092\u5b9f\u88c5\u3059\u308b\u30b3\u30fc\u30c9"},"content":{"rendered":"\n<p>WordPress\u3067\u30d6\u30ed\u30b0\u3092\u904b\u55b6\u3057\u3066\u3044\u308b\u3068\u3001\u30a2\u30c9\u30bb\u30f3\u30b9\u3084\u5e83\u544a\u306a\u3069\u4f55\u304b\u3068\u631f\u307f\u305f\u304f\u306a\u308b\u3010\u6700\u521d\u306eh2\u4e0a\u3011\u3068\u3010\u8a18\u4e8b\u672c\u6587\u4e0b\u3011\u3002\u3053\u306e2\u304b\u6240\u306b\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u30a8\u30ea\u30a2\u3092\u5b9f\u88c5\u3057\u3066\u304a\u304f\u3053\u3068\u3067\u3001\u7c21\u5358\u306b\u5909\u66f4\u306e\u52b9\u304f\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<p>\u65b9\u6cd5\u306f\u9a5a\u304f\u307b\u3069\u7c21\u5358\u3067\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092functions.php\u306e\u4e00\u756a\u4e0b\u306b\u30b3\u30d4\u30da\u3059\u308b\u3060\u3051\u3067OK\u3002\u203bfunctions.php\u3092\u7de8\u96c6\u3059\u308b\u3068\u304d\u306f\u5fc5\u305a\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u3092\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u521d\u306eh2\u4e0a\u3068\u8a18\u4e8b\u672c\u6587\u4e0b\u306b\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u30a8\u30ea\u30a2\u3092\u5b9f\u88c5\u3059\u308b\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6700\u521d\u306eh2\u4e0a\u306b\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u3092\u8ffd\u52a0\u3000------------------------------------------\ndefine('H2_REG', '\/&lt;h2.*?>\/i');\n  \nfunction get_h2_included_in_body( $the_content ){\n  if ( preg_match( H2_REG, $the_content, $h2results )) {\n    return $h2results[0];\n  }\n}\n  \nfunction add_widget_before_1st_h2($the_content) {\n  if ( is_single() &amp;&amp;\n       is_active_sidebar( 'widget-in-article' )\n  ) {\n    ob_start();\n    dynamic_sidebar( 'widget-in-article' );\n    $ad_template = ob_get_clean();\n    $h2result = get_h2_included_in_body( $the_content );\n    if ( $h2result ) {\n      $count = 1;\n      $the_content = preg_replace(H2_REG, $ad_template.$h2result, $the_content, 1);\n    }\n  }\n  return $the_content;\n}\nadd_filter('the_content','add_widget_before_1st_h2');\n  \nregister_sidebar( array(\n     'name' => __( '\u6700\u521d\u306eh2\u4e0a' ),\n     'id' => 'widget-in-article',\n     'description' => '\u6700\u521d\u306eh2\u4e0a\u306b\u8868\u793a\u3055\u308c\u308b\u30a6\u30a4\u30b8\u30a7\u30c3\u30c8\u3002',\n     'before_widget' => '&lt;div class=\"before-h2\">',\n     'after_widget' => '&lt;\/div>',\n     'before_title' => '',\n     'after_title' => '',\n) );\n  \n\/\/ \u6295\u7a3f\u672c\u6587\u4e0b\u306b\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u3092\u8ffd\u52a0\u3000------------------------------------------\n    register_sidebar(array(\n    'name' => '\u6295\u7a3f\u672c\u6587\u4e0b',\n    'id' => 'cta-widget',\n    'description' => '\u6295\u7a3f\u672c\u6587\u4e0b\u306b\u8868\u793a\u3055\u308c\u308b\u30a6\u30a4\u30b8\u30a7\u30c3\u30c8\u3002',\n    'before_widget' => '&lt;div class=\"cta-widget\">',\n    'after_widget' => '&lt;\/div>',\n    'before_title' => '',\n    'after_title' => '',\n    ));\n  \nadd_filter('the_content','my_content', 0);\n   \nfunction my_content($content){\n    if(!is_single()){\n        return $content;\n    }\n    $new_content = '';\n    $new_content .= $content;\n    $new_content .= get_dynamic_sidebar('\u6295\u7a3f\u672c\u6587\u4e0b');\n    return $new_content;\n}\n \nfunction get_dynamic_sidebar($index = 1){\n    $sidebar_contents = \"\";\n    ob_start();\n    dynamic_sidebar($index);\n    $sidebar_contents = ob_get_clean();\n    return $sidebar_contents;\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WordPress\u3067\u30d6\u30ed\u30b0\u3092\u904b\u55b6\u3057\u3066\u3044\u308b\u3068\u3001\u30a2\u30c9\u30bb\u30f3\u30b9\u3084\u5e83\u544a\u306a\u3069\u4f55\u304b\u3068\u631f\u307f\u305f\u304f\u306a\u308b\u3010\u6700\u521d\u306eh2\u4e0a\u3011\u3068\u3010\u8a18\u4e8b\u672c\u6587\u4e0b\u3011\u3002\u3053\u306e2\u304b\u6240\u306b\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u30a8\u30ea\u30a2\u3092\u5b9f\u88c5\u3057\u3066\u304a\u304f\u3053\u3068\u3067\u3001\u7c21\u5358\u306b\u5909\u66f4\u306e\u52b9\u304f\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u304c\u3067\u304d\u308b\u3002 \u65b9\u6cd5\u306f\u9a5a\u304f\u307b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":828,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2047","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it-literacy"],"_links":{"self":[{"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/posts\/2047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/comments?post=2047"}],"version-history":[{"count":0,"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/posts\/2047\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/media\/828"}],"wp:attachment":[{"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/media?parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/categories?post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xn--w8jyfva.jp\/box\/wp-json\/wp\/v2\/tags?post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}