10 Tips for Using Custom Fields in WordPress
- Transfer
This article is an abridged and rather free translation of the article “Custom Fields Hacks For WordPress” published in Smashing Magazine (the link to the original is given at the end). There are 10 ways to use custom fields in WordPress in an interesting way.
1. Setting the end date of the post.
This technique will help set the end date for post publication if you need to display a post for a limited time.
In the subject, we replace the output loop with the following: When writing a post, a user field is created with the expiration key and time in the format mm / dd / yyyy 00:00:00 as a value. The post will no longer be displayed after the specified date and time. 2. Displaying posts on the main page.
This technique will help you choose how to display the post on the main page - in full or just the announcement. As in the previous method, you need to replace the standard output loop with the following: This code displays all posts in the form of announcements. To display the selected post completely, you need to create a field with the full key and set it to any value. 3. Display music and mood in the post. LiveJournal users have two convenient options when writing a post that are not yet available in WordPress - displaying current music and moods. Adding them to WP is quite simple - you need to add the following code in the single.php or index.php file of the current topic: In the post, create a field with the mood key and enter the mood as the value. 4. Adding a META description to posts.
Although META information is not as important for search engines as it used to be, it doesn’t hurt to have a META description for your posts.
In the header.php file, add this code anywhere between the tags and This technique uses WordPress tags to generate meta descriptions. The pages of categories, tags, archives and the main page use static meta-information. Edit lines 3, 7, and 9 in your code to define your own values. For entries, create a Metadescription field and enter the values. 5. Links to external resources. How to create a link in the heading to an external resource "directly", without a link to the post itself (as it happens on Habré - approx. Transl.)? There is such an aesthetic way. First, insert the following code into the functions.php file:
After that, in the index.php file, the standard header output code: replace with Now when writing a post you need to create a field with the value url1 or title_url or url_title and enter the link to the external resource as a value. The post title will not be a link to the post itself, as usual, but a link to an external resource (again, remember Habr - approx. Transl.). If the field value is not found, the title will traditionally lead to the post itself. 6. Built-in CSS styles. Sometimes you need to add extra CSS styles to your posts. Of course, you can always use the built-in (inline), but it is sometimes not very convenient. This technique will help you easily create additional CSS classes and embed them in your blog title.
In the header.php file anywhere between the tags and paste the code. When writing a record, create a css field and enter the desired CSS as a value. So simple! 7. Overriding on When writing a post, create a title field and enter the desired value. 8. Blocking search engines from indexing certain posts. Have you ever wanted to prevent a search engine from indexing certain entries (for example, something very personal)? But at the same time allow it to read to ordinary readers. It's simple enough with the help of ... you already understand what. First you need to find the post ID, which is to be hidden from the ubiquitous search engines. In the example, we use ID 17.
</b><br>
Тэг title чрезвычайно важен для SEO и привлечения траффика на ваш блог. И, кстати, большинство тем для WP поставляется без оптимизированного тэга title. Сторонние плагины, типа All in One SEO Pack могут помочь решить задачу, но так же этого можно достигнуть с помощью пользовательских полей.<br>
В файле header.php заменяем код в <title>
In the header.php file, add this code anywhere between the tags and the Custom noindex field and post ID as a value will prevent search engines from indexing the content of this post. 9. Get or print the value of any field. You are now using many custom fields. How about getting all the values automatically? The following code is inserted into the functions.php file: Now, to call the function and get the values of the fields, we use the following code: First we use the PHP function_exists () function to make sure that the get_custom_field_value function is defined in the topic. The first argument to the function is the name of the field (in this case featured_image
) and the second - getting the value (true) or calling it for further use (false) .
10. The output of the buttons "Digg This" only if necessary.
It is a very convenient and good idea to use the “Digg This” button to receive traffic from Digg. But is this button needed for all posts? It is hardly worth using Digg, for example, to announce changes to your site. User fields will help us again.
Open the single.php file and paste this wherever to where we want to see the “Digg This” button. A custom digg field with any value will display the “Digg This” button (the javascript used in the code will show the button provided by Digg itself). If there is no value, the button will not be shown.
Bonus Display thumbnails next to posts.
This is a fairly well-known technique and it has been used successfully by many. But some still don’t know how beautiful it is to show thumbnails next to the posts on the page.
1. Create a default image in an editor like PhotoShop or Gimp. The size in the given example is 200x200, but of course, it is at your discretion. Name the image default.gif and upload it to the images folder in the current theme.
2. In the index.php file, paste this code in the place where you want to see the thumbnails. 3. When writing, create a custom field with the post-img key and the path to the image that you would like to display as a thumbnail, as its value. "Original translation" on the blog ...
1. Setting the end date of the post.
This technique will help set the end date for post publication if you need to display a post for a limited time.
In the subject, we replace the output loop with the following: When writing a post, a user field is created with the expiration key and time in the format mm / dd / yyyy 00:00:00 as a value. The post will no longer be displayed after the specified date and time. 2. Displaying posts on the main page.
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example...
the_title();
the_excerpt();
}
endwhile;
endif;
?>
This technique will help you choose how to display the post on the main page - in full or just the announcement. As in the previous method, you need to replace the standard output loop with the following: This code displays all posts in the form of announcements. To display the selected post completely, you need to create a field with the full key and set it to any value. 3. Display music and mood in the post. LiveJournal users have two convenient options when writing a post that are not yet available in WordPress - displaying current music and moods. Adding them to WP is quite simple - you need to add the following code in the single.php or index.php file of the current topic: In the post, create a field with the mood key and enter the mood as the value. 4. Adding a META description to posts.
while (have_posts()) : the_post();
$customField = get_post_custom_values("full");
if (isset($customField[0])) {
//Custom field is set, display a full post
the_title();
the_content();
} else {
// No custom field set, let's display an excerpt
the_title();
the_excerpt();
endwhile;
endif;
?>
$customField = get_post_custom_values("mood");
if (isset($customField[0])) {
echo "Mood: ".$customField[0];
}
Although META information is not as important for search engines as it used to be, it doesn’t hurt to have a META description for your posts.
In the header.php file, add this code anywhere between the tags and This technique uses WordPress tags to generate meta descriptions. The pages of categories, tags, archives and the main page use static meta-information. Edit lines 3, 7, and 9 in your code to define your own values. For entries, create a Metadescription field and enter the values. 5. Links to external resources. How to create a link in the heading to an external resource "directly", without a link to the post itself (as it happens on Habré - approx. Transl.)? There is such an aesthetic way. First, insert the following code into the functions.php file:
}?>">
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!emptyempty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (emptyempty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo ''.$title.'
';
}
After that, in the index.php file, the standard header output code: replace with Now when writing a post you need to create a field with the value url1 or title_url or url_title and enter the link to the external resource as a value. The post title will not be a link to the post itself, as usual, but a link to an external resource (again, remember Habr - approx. Transl.). If the field value is not found, the title will traditionally lead to the post itself. 6. Built-in CSS styles. Sometimes you need to add extra CSS styles to your posts. Of course, you can always use the built-in (inline), but it is sometimes not very convenient. This technique will help you easily create additional CSS classes and embed them in your blog title.
< ?php the_title(); ?>
In the header.php file anywhere between the tags and paste the code. When writing a record, create a css field and enter the desired CSS as a value. So simple! 7. Overriding on When writing a post, create a title field and enter the desired value. 8. Blocking search engines from indexing certain posts. Have you ever wanted to prevent a search engine from indexing certain entries (for example, something very personal)? But at the same time allow it to read to ordinary readers. It's simple enough with the help of ... you already understand what. First you need to find the post ID, which is to be hidden from the ubiquitous search engines. In the example, we use ID 17.
$css = get_post_meta($post->ID, 'css', true);
if (!emptyempty($css)) { ?>
<br>
<?php if (is_home () ) { <br>
bloginfo('name'); <br>
} elseif ( is_category() ) { <br>
single_cat_title(); echo ' - ' ; bloginfo('name'); <br>
} elseif (is_single() ) { <br>
$customField = get_post_custom_values("title"); <br>
if (isset($customField[0])) { <br>
echo $customField[0]; <br>
} else { <br>
single_post_title(); <br>
} <br>
} elseif (is_page() ) { <br>
bloginfo('name'); echo ': '; single_post_title(); <br>
} else { <br>
wp_title('',true); <br>
} ?><br>
In the header.php file, add this code anywhere between the tags and the Custom noindex field and post ID as a value will prevent search engines from indexing the content of this post. 9. Get or print the value of any field. You are now using many custom fields. How about getting all the values automatically? The following code is inserted into the functions.php file: Now, to call the function and get the values of the fields, we use the following code: First we use the PHP function_exists () function to make sure that the get_custom_field_value function is defined in the topic. The first argument to the function is the name of the field (in this case featured_image
ID, 'noindex', true);
if (!emptyempty($cf)) {
echo '/>';
}
?>
function get_custom_field_value($szKey, $bPrint = false) {
global $post;
$szValue = get_post_meta($post->ID, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;
}
get_custom_field_value('featured_image', true);
} ?>
) and the second - getting the value (true) or calling it for further use (false) .
10. The output of the buttons "Digg This" only if necessary.
It is a very convenient and good idea to use the “Digg This” button to receive traffic from Digg. But is this button needed for all posts? It is hardly worth using Digg, for example, to announce changes to your site. User fields will help us again.
Open the single.php file and paste this wherever to where we want to see the “Digg This” button. A custom digg field with any value will display the “Digg This” button (the javascript used in the code will show the button provided by Digg itself). If there is no value, the button will not be shown.
ID, 'digg', true);
if (!emptyempty($cf)) {
echo 'http://digg.com/tools/diggthis.js" type="text/javascript">'}
?>
Bonus Display thumbnails next to posts.
This is a fairly well-known technique and it has been used successfully by many. But some still don’t know how beautiful it is to show thumbnails next to the posts on the page.
1. Create a default image in an editor like PhotoShop or Gimp. The size in the given example is 200x200, but of course, it is at your discretion. Name the image default.gif and upload it to the images folder in the current theme.
2. In the index.php file, paste this code in the place where you want to see the thumbnails. 3. When writing, create a custom field with the post-img key and the path to the image that you would like to display as a thumbnail, as its value. "Original translation" on the blog ...
ID, 'post-img', true);
if ($postimageurl) {
?>
< ?php } ?>