»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
preface
I saw an article on the website of boiled fish the other dayArticle on content templatesThe 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 wordGet the article content through IDIt'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 publishedarticle.
Code and use
Next, use the code to create a short code
//Add a short code to the articlefunction 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
Then call the short code in the corresponding article
Then look at the effect
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