Several lines of small code to implement the function of a WordPress content template

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

Many people have some requirements for inserting specific content when using WordPress. No matter what you call it, advertisement or template, it's just that thing anyway. We can use a piece of code to implement the function of content template

几行小代码实现一个WordPress内容模板的功能-极客公园

preface

I saw an article on the website of boiled fish the other day Article on content templates The big god is the big god. All kinds of functions come at your fingertips. Think about it, we can't figure it out. But as a wper with two lines of code, we can understand the core of this function from the surface. In a word Get the article content through ID It's so simple, let's do it

thinking

We want to think about the idea that wpjam first creates a post type to create a template, but there may not be so many templates in actual use, so we can use normal articles, but as a template it is not suitable for direct display. Let's set it as a private article. The specific call can use short code, referring to the previously published article .

Code and use

Next, use the code to create a short code

 //Add a 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 as a private article
几行小代码实现一个WordPress内容模板的功能-极客公园

Then call the short code in the corresponding article
几行小代码实现一个WordPress内容模板的功能-极客公园

Then look at the effect
几行小代码实现一个WordPress内容模板的功能-极客公园

Postscript

Now, this function is not difficult, but it is slightly difficult to implement those interface functions gracefully. If only this function is needed, it is still very simple. When encountering problems, you need to consider what functions this function actually needs. This function can insert advertisements or some reusable content

--End--

Post reply

Your email address will not be disclosed. Required items have been used * tagging

3 Replies to "Several lines of small code realize the function of a WordPress content template"