Experience of using custom fields in WordPress


Keyword Description: how to use the definition

I have been using WordPress for a while. In the process of building my blog, I have accumulated a lot of experience. I like the custom fields of WordPress very much. With the custom fields, I can complete many custom functions.

The custom fields will see each time the new log is published (as shown below). How to use this problem is very old. Most people will, a few new users can search on Google: lol one of my suggestions is to build a custom field as a new field in the data base and then call it in the theme. This article mainly introduces some smart uses of user-defined fields, which can also be said to be some summaries, plus some experience that you often use.

Define meta tags with WordPress custom fields (you can do a little SEO)

When publishing a log, define keywords and description in the name of the user-defined field (you can choose another name for yourself, of course), and fill in the keywords and description information of the article in the value. Then you can call.

Here are the references:
<?php if ( is_single() ) { ?>
<meta name=”keywords” content=”<?php $key=”keywords”; echo get_post_meta($post->ID, $key, true); ?>” />
<meta name=”description” content=”<?php $key=”description”; echo get_post_meta($post->ID, $key, true); ?>” />
<?php } ?>

This can be called, very simple, if you are not using SEO plug-ins, so there are certain benefits for SEO, of course, it can completely replace plug-ins.

Using custom fields to add a category theme picture to WordPress

Now there are some information websites or Blog websites with a theme picture (as shown in the figure below) on each news or log. This function can also be achieved through the custom field of WP. Let me take the following picture as an example.

First, create a new field “topic” img “in the custom field, and fill in the address of the picture you made in the value, and then call it in the WP topic.

Here are the references:
<?php if ( get_post_meta($post->ID, ‘pre_image’, true) ) : ?>
<?php $image = get_post_meta($post->ID, ‘pre_image’, true); ?>
<img src=”<?php echo $image; ?>” />
<?php else: ?>
<img src=”<?php bloginfo(‘template_directory’); ?>/images/blank.jpg” />
<?php endif; ?>

You will see the above code, line 5 is to define the default image displayed without input value, you can define the address yourself. Of course, you can also add width, border, or other styles to the picture, which can be done as you like.

Add anything you need with custom fields

If you want to build a non blog site with WP, you need other things for your published articles. For example, if you want to build a Taoke site, you need to add the price of the product, the promotion link or more you want, or the same thing. When you send the table, you need to build a custom field, and then call it in the topic. Not one by one.

When I write here, I suddenly have an idea, which may help my friends who want to place advertisements in their blogs. Some bloggers will place different advertisements according to the logs of different themes, which can also be realized by using the custom field, that is, to build a custom field of “advertisement”, and then it can be called to realize. This function can be used together with some advertisement plug-ins, and the default advertisement will be displayed when the custom field is not entered.

The custom fields of WordPress are really powerful, which can make you do a lot of complicated work without modifying the program, and make it easier to build a website.