{"id":584,"date":"2017-10-01T20:04:26","date_gmt":"2017-10-02T01:04:26","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=584"},"modified":"2017-10-05T09:46:26","modified_gmt":"2017-10-05T14:46:26","slug":"wordpress-how-to-create-a-table-of-contents-page-with-python","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/01\/wordpress-how-to-create-a-table-of-contents-page-with-python\/","title":{"rendered":"WordPress: How to create a Table of Contents page with Python"},"content":{"rendered":"<p>What I wanted to create was a dynamic Table of Contents page where I could have a listing of all of my published articles, organized by category, and sorted such that categories with the most posts appear first, and the posts under each category are listed from most recent at the top to oldest at the bottom. I couldn&#8217;t find a WordPress plugin for this, so I thought I would try to create it myself using Python.<\/p>\n<p>I was able to accomplish this using a Python script that makes use of the MySQLdb module to query the MySQL database that WordPress uses to store all of my articles. Then on the Table of Contents page, I use an iframe to automatically load and launch the Python script that creates the ToC.<\/p>\n<p>Every time a new article is published and I load the Table of Contents page, the query is run and a new, possibly reordered Table of Contents is generated.<\/p>\n<p>First I had to figure out how to get Python to connect to my MySQL database to run queries. This was made more difficult due to the fact that my web host only has Python 2.5 and supports a very limited number of modules. One of the modules it does support though is MySQL-python. Here is an example of the code used in Python to connect using this module:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!\/usr\/bin\/python\r\nimport MySQLdb\r\n\r\ndef connect():\r\n    # Connect to the database\r\n    connection = MySQLdb.connect('mywebhost.com', 'my_username', 'my_password', 'my_databasename')\r\n    try:\r\n        cursor = connection.cursor()\r\n        cursor.execute(sql)\r\n        result = cursor.fetchall()\r\n    except:\r\n        print(\"Error: unable to fetch data\")\r\n    connection.close()\r\n    print_output(result)\r\n\r\nif __name__ == '__main__':\r\n    connect()<\/pre>\n<p>Here is the SQL code I needed in order to get the post titles, post dates, post name slugs (used for creating the static links), and categories. With those four things, I had everything I needed to create the ToC:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">SELECT p.post_title as 'Post Title',\r\n       t.name as 'Category',\r\n       p.post_date as \"Post Date\",\r\n       p.post_name as \"Post Name\"\r\nFROM wp_posts p,\r\n     wp_terms t,\r\n     wp_term_relationships tr,\r\n     wp_term_taxonomy tx\r\nWHERE p.post_type = 'post'\r\nAND p.post_status = 'publish'\r\nAND tx.taxonomy = 'category'\r\nAND p.ID = tr.object_id\r\nAND tr.term_taxonomy_id = t.term_id\r\nAND tx.term_id = t.term_id;\r\n<\/pre>\n<p>The last parts I needed to complete this project were:<\/p>\n<p>1. Creating the logic in the Python script to build the links and organize the ToC based on categories<br \/>\n2. The Python code needed to point to all of the CSS resources that my site uses<br \/>\n3. The iframe in the Table of Contents page in order to load the Python script<\/p>\n<p>The final result can be seen here:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"lXzjUJl7Pb\"><p><a href=\"http:\/\/bluegalaxy.info\/codewalk\/table-of-contents\/\">Table of Contents<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" title=\"&#8220;Table of Contents&#8221; &#8212; Chris Nielsen Code Walk\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/bluegalaxy.info\/codewalk\/table-of-contents\/embed\/#?secret=lXzjUJl7Pb\" data-secret=\"lXzjUJl7Pb\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What I wanted to create was a dynamic Table of Contents page where I could have a listing of all of my published articles, organized by category, and sorted such that categories with the most posts appear first, and the posts under each category are listed from most recent at the top to oldest at &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/01\/wordpress-how-to-create-a-table-of-contents-page-with-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">WordPress: How to create a Table of Contents page with Python<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[42,4,43,9],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-iframe","tag-python","tag-sql","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":13,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":2914,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/584\/revisions\/2914"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}