banner
胡鹤仙的Blockchain Blog

胡鹤仙的Blockchain Blog

twitter
github
telegram
medium

Learn a skill: Use ChatGPT to help me write code

I am one of the first group of people to use ChatGPT. I registered back in December last year. So far, I have used it to help me write surveys, daily assignments, and also to ask some strange questions. Yesterday, I used it to help me write a piece of WordPress code.

At first, I saw it on Lin Mumu's blog, where he wrote "Which book have I finished writing". I understood the principle, which is to get the word count of all the articles on the entire site, and then see in which range your word count falls, and output the corresponding book. However, he used Hugo, so I asked him how to modify it for use on WordPress. He gave me a modified version for Typecho, which you can see in "Total Word Count of Blog". This confused me, so I immediately sought help from ChatGPT.

First, I sent the code from Lin Mumu's blog to it and explained the code principle.

20230417084350.png

Then, I asked it to modify this code to make it usable for WordPress, but it seemed to have no effect. So I sent it the Typecho version for modification, and it also explained the code principle to me.

20230417084410.png

Finally, I asked it to modify the code based on the above code to make it usable for WordPress. Although it couldn't generate all the code at once due to the word limit, it was successful.

I posted the complete code and explained how to use it.

/*
 * Which book have I finished writing?
 * Original author: Lin Mumu
 * Modified by: Hu Hexian & ChatGPT
 */
function allwords() {
    global $wpdb;
    $chars = 0;
    $results = $wpdb->get_results("SELECT post_content FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post'");
    foreach ($results as $result) { $chars += mb_strlen(trim($result->post_content), 'UTF-8'); }
    if($chars<50000){
    echo 'The site has a total of '.$chars.' words, still working on updates.. Keep it up!';}
    elseif ($chars<70000 && $chars>50000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Little Prince" by Antoine de Saint-Exupéry!';}
    elseif ($chars<90000 && $chars>70000){
    echo 'The site has a total of '.$chars.' words, finished reading "Outcry" by Lu Xun!';}
    elseif ($chars<100000 && $chars>90000){
    echo 'The site has a total of '.$chars.' words, finished reading "Old Tales of the South" by Lin Haiyin!';}
    elseif ($chars<110000 && $chars>100000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Prince and the Pauper" by Mark Twain!';}
    elseif ($chars<120000 && $chars>110000){
    echo 'The site has a total of '.$chars.' words, finished reading "Hesitation" by Lu Xun!';}
    elseif ($chars<130000 && $chars>120000){
    echo 'The site has a total of '.$chars.' words, finished reading "To Live" by Yu Hua!';}
    elseif ($chars<140000 && $chars>130000){
    echo 'The site has a total of '.$chars.' words, finished reading "Thunderstorm" by Cao Yu!';}
    elseif ($chars<150000 && $chars>140000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Destiny of Writing" by Shi Tiesheng!';}
    elseif ($chars<160000 && $chars>150000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Secret Garden" by Frances Hodgson Burnett!';}
    elseif ($chars<170000 && $chars>160000){
    echo 'The site has a total of '.$chars.' words, finished reading "Sunrise" by Cao Yu!';}
    elseif ($chars<180000 && $chars>170000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Adventures of Tom Sawyer" by Mark Twain!';}
    elseif ($chars<190000 && $chars>180000){
    echo 'The site has a total of '.$chars.' words, finished reading "Border Town" by Shen Congwen!';}
    elseif ($chars<200000 && $chars>190000){
    echo 'The site has a total of '.$chars.' words, finished reading "Education of Love" by Romain Rolland!';}
    elseif ($chars<210000 && $chars>200000){
    echo 'The site has a total of '.$chars.' words, finished reading "Cold Night" by Ba Jin!';}
    elseif ($chars<220000 && $chars>210000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Convenience Store" by Keigo Higashino!';}
    elseif ($chars<230000 && $chars>220000){
    echo 'The site has a total of '.$chars.' words, finished reading "A Life" by Guy de Maupassant!';}
    elseif ($chars<250000 && $chars>230000){
    echo 'The site has a total of '.$chars.' words, finished reading "Pride and Prejudice" by Jane Austen!';}
    elseif ($chars<280000 && $chars>250000){
    echo 'The site has a total of '.$chars.' words, finished reading "Fortress Besieged" by Qian Zhongshu!';}
    elseif ($chars<300000 && $chars>280000){
    echo 'The site has a total of '.$chars.' words, finished reading "Ancient Ship" by Zhang Wei!';}
    elseif ($chars<310000 && $chars>300000){
    echo 'The site has a total of '.$chars.' words, finished reading "Midnight" by Mao Dun!';}
    elseif ($chars<320000 && $chars>310000){
    echo 'The site has a total of '.$chars.' words, finished reading "Dust Settles" by Alai!';}
    elseif ($chars<340000 && $chars>320000){
    echo 'The site has a total of '.$chars.' words, finished reading "Wuthering Heights" by Emily Brontë!';}
    elseif ($chars<350000 && $chars>340000){
    echo 'The site has a total of '.$chars.' words, finished reading "The Hunchback of Notre-Dame" by Victor Hugo!';}
    elseif ($chars<400000 && $chars>350000){
    echo 'The site has a total of '.$chars.' words, finished reading "Journey Under the Midnight Sun" by Keigo Higashino!';}
    elseif ($chars<1000000 && $chars>400000){
    echo 'The site has a total of '.$chars.' words, finished reading the four famous classics of our country!';}
    elseif ($chars>1000000){
    echo 'The site has a total of '.$chars.' words, finished reading "War and Peace" by Leo Tolstoy!';}
} 

Add the above code to the function.php file of the theme file, and add the code where it needs to be called.

<?php echo allwords(); ?>

I currently have it in the footer.php file, displayed at the end of the page. The above code can be freely modified according to the actual situation, such as word count, book titles, etc., and more word count ranges and book titles can be added. That's the process I used ChatGPT for. The effect is shown in the following figure.

20230417085513.png

You can visit my blog (青山绿水) and scroll to the bottom to view it.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.