Several lines of small code to achieve a WordPress content template function

» WordPress » Several lines of small code to achieve a WordPress content template function

Many people need to insert specific content when using WordPress. No matter what you call Ba, advertisement or template, we can use a piece of code to realize the function of content template



Two days ago, I saw an article on boiled fish website Articles on content templates The great God is the great God. All kinds of functions come at your fingertips. Think about it. But as a wper who can read two sentences of code, you can still understand the core of this function through the surface. In a word, it is Get article content by ID That's easy. Let's do it


We want to straighten out the idea. Wpjam first creates a post type to create a template. However, there may not be so many templates in actual use, so we can use normal articles. However, as templates, they are not suitable for direct display. Let's set them as private articles. For specific calls, you can use short code, referring to the previous ones article .

Code and usage

Now use the code to create a short code

 //Add short code to the article function gdk_ insert_ temp($atts, $content = null) { extract(shortcode_ atts(array( 'id' => '' ) , $atts)); $data = get_ post($id); $content = $data->post_ content; return $content; } add_ shortcode('temp', 'gdk_ insert_ temp');

Very simple code, and then we directly create an article, set up as a private article

Then call the short code in the corresponding article

Then look at the effect


Now, in fact, this function is not difficult, but if you want to implement those interface functions gracefully, it is a little difficult. If you just need this function, it is still very simple. If you encounter problems, you need to consider what the function actually needs. This function can insert advertisements or insert some reusable content



Your email address will not be disclosed. Required item used * tagging

3 replicates to "realize the function of a WordPress content template with a few lines of code"