Jump to:

The learning path for beginners in WordPress programming

I believe that many people, like me, can’t help but use the power of code when the website needs to be customized. Although it is not commonly used, it is so delicious to use code to solve problems, so you don’t have to look for plug-ins.

At the same time, I am also very clear that it is really difficult to remember all WordPress functions. After three months, I basically forget all of them, and I can only Google from scratch.

So in order to let you use the code of WordPress proficiently, this post will summarize the programming method of WordPress and the most commonly used functions.

Remember, to understand this article, you’d better have a little programming foundation, at least a little PHP code knowledge, otherwise it will look obscure.

This article will revolve around the following outline:

Important Concepts of WordPress Programming

First of all, if you want to understand WordPress programming, you must understand that there are only two post types in WordPress, which are:

  • post
  • page

In Xiaobai’s eyes, the difference between the two before is very big, but if you pass through the Xiaobai stage, you will find that post and page are sometimes silly and unclear.

For the similarities and differences between post and page, you can check out this article: Posts vs. Pages.

In addition to the above two article types, you can also manually register custom article types, such as books, portfolios, movies, etc., which are collectively referred to as “CPT” in WordPress programming.

If you want to manually register CPT, you need to use it in functions.phpWrite function in the middle, the specific method can be found in this official document.

If you don’t want to write functions manually, you can download Custom Post Type UIPlugins or PodsPlug-ins, these two plug-ins are very easy to use, and I basically have to install one of them for every website.

After understanding the concept of post type, you can understand another concept on this basis: Field, field.

By default, each post type will have default fields, such as title, text, abstract, and main image. The difference between the aforementioned post and page is mainly due to the different fields, such as Post has the concept of category and publish date, but page does not.

Correspondingly, of course, each post type can also customize the Field, and the customized field is the so-called custom field.

WordPress naturally supports adding custom fields, and how to add them can refer to this videoTo do it, but I generally don’t use the built-in functions of WordPress, but use a plugin, the name of the plugin is “Advanced Custom Field”, yes, the famous “ACF” plugin.

One more sentence: Even if you don’t touch WordPress programming, I strongly recommend you to install CPT and ACF plug-ins. With CPT+ACF, we can use WordPress to create any type of article, and at the same time create any customization we want in each article type field, very powerful.

After you can understand the concepts of Post Type and Field, the rest of the questions are easy, because as long as you solve the most basic “how to present the data”, you can learn WordPress almost.

So the problem is reduced to:

  • How to render the article type Post Type?
  • How to render the field Filed in the article type?

On this basis, two of the most important concepts in WordPress programming were born:

  • loop – loop
  • function – function

After my actual combat, I found that I only need to clarify the above two concepts, and the others are all corners and corners, which can be solved through Google or Stack Overflow.

Understanding Loops

Official documents can be saved before official reading, the so-called cycle is used to batch render Post Type.

To fully understand the cycle, you need to understand what is called “Query”. You can save this official document first about the query..

By default, WordPress will fetch data from the database according to the current URL. If I am on a single page (post singular), it will fetch data from the database related to the single page. If I am on a post archive page (post archive), It will fetch data from the database related to the article archive.

After you can understand the query, many important and difficult aspects of WordPress programming will be solved.

For example, to display all Post Types currently queried on the website, you can use the following code:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//The body that needs to be rendered in a loop
<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>

The above is the default situation. If you want to customize the current query, for example, if you want to recycle another list outside the first loop, you can use the following code:

$my_query = new WP_Query( "cat=3" );
   if ( $my_query->have_posts() ) { 
       while ( $my_query->have_posts() ) { 
           $my_query->the_post();
           the_content();
       }
   }
   wp_reset_postdata();

Among them, “new WP_Query” is the soul of the above code, that is, the query object we created. In the custom query, the things we loop out will not be limited by the current URL.

Understanding functions

After understanding the loop, we can extract the fields under any query. If we want to extract the corresponding Filed in the Post Type, we need to use a function.

The function is somewhat similar to a “rule black box”. For example, a cow in reality is a regular black box. We feed it grass, and it will produce milk through its own very complicated digestive system.

We don’t need to care about the digestive system of this cow, how it works, what is the principle, we just need to know that if we feed it grass, it will produce milk.

The same is true for functions. We don’t need to care about how the function works internally. We only need to care about giving him some data, which can return the data we need after digestion by its own “rule black box”.

Regardless of custom fields and custom functions, WordPress has the following default functions, which are very commonly used and will be used frequently.

get_the_title()

This function can get the title of the current Post Type from the database and return it as a string.

Parameters: You can pass the ID or article object of the article into it.

To be continued…