📜 ⬆️ ⬇️

10 Tricks for Using Custom Fields in WordPress

This article is an abbreviated and fairly free translation of the article “Custom Fields Hacks For WordPress” published in Smashing Magazine (the link to the original is given at the end). Listed 10 ways to interesting use custom fields (custom fields) in WordPress.


1. Set the end date of the post.
This trick will help set the posting end date for a post if you need to display a post for a limited time.
In the subject, replace the output cycle with the following:

<?php
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;
?>

When writing a post, a custom field is created with the expiration key and time in the format mm / dd / yyyy 00:00:00 as a value. The post will cease to be displayed after the specified date and time.
')
2. Display 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, it is necessary to replace the standard output cycle with the following:

<?php if (have_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;
?>

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. Displaying music and mood in the post.
LJ users have two convenient options when writing a post that are not yet available in WordPress - displaying current music and mood. 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 theme:

$customField = get_post_custom_values("mood");
if (isset($customField[0])) {
echo "Mood: ".$customField[0];
}

In the post we create a field with the key mood and enter the mood as a value.

4. Adding 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 records.
In the header.php file, add this code anywhere between the tags <head> and </ head>

<meta name="description" content="
<?php if ( (is_home()) || (is_front_page()) ) {
echo ('Your main description goes here');
} elseif(is_category()) {
echo category_description();
} elseif(is_tag()) {
echo '-tag archive page for this blog' . single_tag_title();
} elseif(is_month()) {
echo 'archive page for this blog' . the_time('F, Y');
} else {
echo get_post_meta($post->ID, "Metadescription", true);
}?>">
</meta>

This technique uses WordPress tags to generate meta descriptions. Static meta-information is used on categories, tags, archives and main pages. Edit lines 3, 7 and 9 in the code to define your own values. For entries, create a Metadescription field and enter values.

5. Links to external resources.
How to create a link in the header to an external resource "directly", without a link to the post itself (as it happens on Habré - approx. Transl.)? For this there is such an aesthetic way. First, we 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 '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}

After that, in the index.php file, the standard output header code:

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <v?php the_title(); ?>">< ?php the_title(); ?></a></h2>


replace with
<?php print_post_title() ?>

Now, when writing a post, you need to create a field with the value url1 or title_url or url_title and enter a link to an external resource as a value. The title of the post in this case will not be a link to the post itself, as usual, but a link to an external resource (again, recall Habr - approx. Transl.). If the value of the field is not found, the title will traditionally lead to the post itself.

6. Built-in CSS styles.
Sometimes you need to add additional CSS styles to your entries. Of course, you can always use inline (inline), but it is sometimes not very convenient. This technique will help you easily create additional CSS classes and embed them in the blog header.
Insert the code in the header.php file anywhere between the tags <head> and </ head>
<?php if (is_single()) {
$css = get_post_meta($post->ID, 'css', true);
if (!emptyempty($css)) { ?>
<style type="text/css">
<?php echo $css; ?>
</style><style>
< ?php }
} ?>
</style>

When writing a record, create a css field and enter the desired CSS as a value. That's so easy!

7. Override <TITLE>
The title tag is extremely important for SEO and attracting traffic to your blog. And by the way, most WP themes are shipped without an optimized title tag. Third-party plug-ins such as the All in One SEO Pack can help solve the problem, but this can also be achieved using custom fields.
In the header.php file, replace the code in <title> </ title> with

<title>
<?php if (is_home () ) {
bloginfo('name');
} elseif ( is_category() ) {
single_cat_title(); echo ' - ' ; bloginfo('name');
} elseif (is_single() ) {
$customField = get_post_custom_values("title");
if (isset($customField[0])) {
echo $customField[0];
} else {
single_post_title();
}
} elseif (is_page() ) {
bloginfo('name'); echo ': '; single_post_title();
} else {
wp_title('',true);
} ?>
</title>

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 him to read simple readers. It's quite simple with the help ... you already understand what.
First you need to find the ID of the post, which is to be hidden from the ubiquitous search engines. In the example we use ID 17.
In the header.php file, add this code anywhere between the tags <head> and </ head>

<?php $cf = get_post_meta($post->ID, 'noindex', true);
if (!emptyempty($cf)) {
echo '/>';
}
?>

The user field noindex and post ID as a value will prevent search engines from indexing the content of this post.

9. Get or display the value of any field.
You are now using many custom fields. How about automatically getting all the values? The following code is inserted into the functions.php file:

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;
}


Now to call the function and get the value of the fields, use the following code:

<?php if ( function_exists('get_custom_field_value') ){
get_custom_field_value('featured_image', true);
} ?>

First, the PHP function_exists () function is used to make sure that the get_custom_field_value function is defined in the theme. The first argument of the function is the name of the field (in this case, featured_image ) and the second is getting the value (true) or calling it for further use (false) .

10. Withdrawing the “Digg This” button only when necessary.
A very convenient and good idea to use the “Digg This” button to get traffic from Digg. But does this button need to all posts? It is hardly worth using Digg, for example, to announce changes on your site. We will again rescue custom fields.
Open the file single.php and paste this where to where we want to see the button "Digg This".

<?php $cf = get_post_meta($post->ID, 'digg', true);
if (!emptyempty($cf)) {
echo 'http://digg.com/tools/diggthis.js" type="text/javascript">'}
?>

A custom digg field with any value displays 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 displayed.

Bonus Thumbnails displayed next to posts.
This is a well-known technique and it has been successfully used by many. But some still do not know how beautifully to show thumbnails next to posts on the page.

1. Create a default image in an editor like PhotoShop or Gimp. The size in this example is 200x200, but of course, it is up to you. 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 into the place where you want to see the miniatures.

<?php $postimageurl = get_post_meta($post->ID, 'post-img', true);
if ($postimageurl) {
?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php echo $postimageurl; ?/>" alt="Post Pic" width="200" height="200" /></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><img src="<?php bloginfo('template_url'); ?/>/images/wprecipes.gif" alt="Screenshot" width="200" height="200" /></a>
< ?php } ?>

3. When writing, create a custom field with the key post-img and the path to the image that you would like to display as a thumbnail, as its value.

"Original translation" on the blog ...

Source: https://habr.com/ru/post/59535/


All Articles