階層表示の数字が表示されない(ショートコード)
-
現在、ショートコードを用いて大中小の階層がある見出しを作成しようとしています。
大中小の区別は、h3~h5を用いて差別化しています。
各階層要素の左側には、階層を示す数字を表示させたいと考えています。【理想形イメージ】
1.大見出し1
1-1中見出し1
1-2中見出し2
1-2-1小見出し1そこで、下記のショートコードを使用してるのですが、大見出ししか階層を示す数字が表示されません。(現状の写真)
//以下ショートコード function mokuji_contents() { $sr_data = get_post_meta(get_the_ID(), 'custom_mokuji'); $custom_mokuji_data = unserialize($sr_data[0]); $comment = $_POST["title"]; //var_dump($comment); if($custom_mokuji_data['mokuji_ck']) { $script = '<script>' . PHP_EOL .'jQuery(window).on("load", function(){' . PHP_EOL; $script .= 'var contents_html = jQuery("div#contents").contents();' . PHP_EOL; $script .= 'var loop_content = contents_html.find("article div.subpagecontents").children();' . PHP_EOL; $script .= 'var new_list_html = "", main_html = "", id_text = "h", h3id = 0, h4id = 0, h5id = 1, h3ul = 0, h4ul = 0, h5ul = 0;' . PHP_EOL; $script .= 'jQuery.each(loop_content, function(index, elem) {' . PHP_EOL; $script .= 'if(jQuery(elem).is("h3") || jQuery(elem).is("h4") || jQuery(elem).is("h5")) { jQuery(elem).removeAttr("id"); }' . PHP_EOL; //大見出し if($custom_mokuji_data['mokuji_o']) { $script .= 'if(jQuery(elem).is("h3")){' . PHP_EOL; $script .= 'if (h5ul == 1) { new_list_html = new_list_html + "</ul>\n";' . PHP_EOL;//直前がh5タグ $script .= 'h5ul = 0};' . PHP_EOL; $script .= 'if (h4ul == 1) { new_list_html = new_list_html + "</li>\n</ul>\n";' . PHP_EOL;//直前がh4タグ $script .= 'h4ul = 0};' . PHP_EOL; $script .= 'h3id++;' . PHP_EOL; $script .= 'jQuery(elem).attr("id", id_text + h3id);' . PHP_EOL;//大見出しにid付与 $script .= 'if (h3id == 1) { new_list_html = new_list_html + "<ul>\n";' . PHP_EOL; $script .= 'h3ul = 1}else { new_list_html = new_list_html + "</li>\n"}' . PHP_EOL; $script .= 'new_list_html = new_list_html + "<li><a href=\"#"+id_text+h3id+"\">" + jQuery(elem).text() + "</a>"' . PHP_EOL; $script .= 'h4id = 0, h5id = 1;' . PHP_EOL; $script .= '};' . PHP_EOL; } //中見出し if($custom_mokuji_data['mokuji_tyu']) { $script .= 'if(jQuery(elem).is("h4")){' . PHP_EOL; $script .= 'if (h5ul == 1) { new_list_html = new_list_html + "</ul>\n";' . PHP_EOL;//直前がh5タグ $script .= 'h5ul = 0};' . PHP_EOL; $script .= 'h4id++;' . PHP_EOL; $script .= 'jQuery(elem).attr("id", id_text + h3id + "-" + h4id);' . PHP_EOL;//中見出しにid付与 $script .= 'if (h4id == 1) { new_list_html = new_list_html + "<ul>\n";' . PHP_EOL; $script .= 'h4ul = 1}else { new_list_html = new_list_html + "</li>\n"}' . PHP_EOL; $script .= 'new_list_html = new_list_html + "<li><a href=\"#"+id_text+h3id+"-"+h4id+"\">" + jQuery(elem).text() + "</a>"' . PHP_EOL; $script .= 'h5id = 1;' . PHP_EOL; $script .= '};' . PHP_EOL; } //小見出し if($custom_mokuji_data['mokuji_sho']) { $script .= 'if(jQuery(elem).is("h5")){' . PHP_EOL; $script .= 'jQuery(elem).attr("id", id_text + h3id + "-" + h4id + "-" + h5id);' . PHP_EOL;//小見出しにid付与 $script .= 'if (h5id == 1) { new_list_html = new_list_html + "<ul>\n";' . PHP_EOL; $script .= 'h5ul = 1};' . PHP_EOL; $script .= 'new_list_html = new_list_html + "<li><a href=\"#"+id_text+h3id+"-"+h4id+"-"+h5id+"\">" + jQuery(elem).text() + "</a></li>\n"' . PHP_EOL; $script .= 'h5id++;' . PHP_EOL; $script .= '};' . PHP_EOL; } $script .= '});' . PHP_EOL;//ループ終了 //足りない閉じタグ追加 $script .= 'if (h5ul == 1) { new_list_html = new_list_html + "</ul>\n"; }' . PHP_EOL; $script .= 'if (h4ul == 1) { new_list_html = new_list_html + "</li>\n</ul>\n"; }' . PHP_EOL; $script .= 'if (h3ul == 1) { new_list_html = new_list_html + "</li>\n</ul>\n"; }' . PHP_EOL; $script .= 'if( new_list_html != "" ){' . PHP_EOL; $script .= 'new_list_html = "<div id=\"index_list\">\n" + new_list_html + "</div>\n";' . PHP_EOL; $script .= '};' . PHP_EOL; //数字形式に置き換え if($custom_mokuji_data['list_type'] == "数字形式"){ $script .= 'new_list_html = new_list_html.replace( /ul/g, "ol" );' . PHP_EOL; } //目次追加 $script .= 'if( new_list_html != "" ){' . PHP_EOL; $script .= 'var subpagecontents = contents_html.find("article div.subpagecontents").html();' . PHP_EOL; $script .= 'subpagecontents = new_list_html + "\n" + subpagecontents;' . PHP_EOL; $script .= 'contents_html.find("article div.subpagecontents").html( subpagecontents );' . PHP_EOL; $script .= '};' . PHP_EOL; if($custom_mokuji_data['list_type'] == "数字形式"){ $script .= 'var style = "";' . PHP_EOL; $script .= 'style += "<style type=\"text/css\" id=\"StyleId\">";' . PHP_EOL; $script .= 'style += "ol { counter-reset: item; }";' . PHP_EOL; $script .= 'style += "li { display: block; }";' . PHP_EOL; $script .= 'style += "ol>li:before { counter-increment : item; content: counters(item, \"-\") \". \"; }";' . PHP_EOL; $script .= 'style += "</style>";' . PHP_EOL; $script .= 'jQuery("head").append(style);' . PHP_EOL; } $script .= '})' . PHP_EOL; $script .= '</script>' . PHP_EOL; } return $script; } add_shortcode('mokuji', 'mokuji_contents');liタグ、olタグの位置がおかしいのでしょうか?
自分で作成したコードではないため、原因がわからず行き詰っております。
そのため、この問題についての解決方法をご教示頂きたいです。
1件の返信を表示中 - 1 - 1件目 (全1件中)
1件の返信を表示中 - 1 - 1件目 (全1件中)
トピック「階層表示の数字が表示されない(ショートコード)」には新たに返信することはできません。