Nowadays, there are endless possibilities to dynamically load content. Not only is it possible, it’s being used on more sites than ever before, because it can help you to deliver more targeted content to a more targeted audience. Here’s a simple example of what you can do with custom fields

Using Custom Fields And Adding Custom Fields Programmatically

Adding Custom Fields To WordPress Programmatically Or Manually

Custom fields, if you’ve never heard of them are basically meta fields that you can add to each single WordPress post or page, either manually or preferably programmatically. Here are two functions you may want to look up on the WordPress Codex if you’re new to using Custom fields:

$metadd = add_post_meta($postid, 'meta_field', $myvar, true); 

Before you can do that outside the loop you obvviously need the $postid. Here’s how to fetch that:

global $post; $ID = $post->ID;  

Retrieve Post Meta And Change Post Titles Dynamically

Alright, now let’s assume you wrote a little script that added a custom field to each post that included a file attachment (e.g. a ZIP file) and stored the name of this particular file in the field . This is actually a bad example of using custom fields, because you could simply run a query to fetch posts with a file-attachment, but I hope you get the idea. Let’s further assume that you want to add either the filename or a simple “Download” to the title of your post.

1. Open up your FTP program

2. Open up /wp-content/themes/your-theme

3. Open up single.php or header.php (IMPORTANT: In some cases, templates use a file header.php. If your theme uses such a file, then use that)

4. Now at the VERY top (directly before anything else) we will first establish some rules to check if we’re on an actual post page and that the post does actually have a custom field. The latter option is helpful if you want to retrieve multiple fields.

if (is_single()){global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid);
if ($meta !== ''){

5. Next, directly after the opening loop we are going to add a variable that fetches the custom field. This will store the value of the field zip_files in the variable $zip, so we can easily manipulate this string

$zip = get_post_meta($postid, 'zip_files', true);

(don’t forget to close the loop!)

6. Now we could either echo $zip directly in the title or we could simply add a “Download ZIP” to the title of your post. For the latter option, let’s simply set a variable called $files to 1

if (!empty( $zip )){$files = 1;}

7. Search for the title opening tag in the single.php or header.php file

8. It should say “wp_title(“”,true);} “ somewhere – directly after that we are going to add our “Download ZIP” title

9.

 if ($files == 1){echo "- Download ZIP"; }  

The Final Code

Directly at the top of single.php/header.php

if (is_single()){global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid);
if ($meta !== ''){
$zip = get_post_meta($postid, 'zip_files', true); 
if (!empty( $zip )){$files = 1;}

Further down between the title tags

 if ($files == 1){echo "- Download ZIP"; }  

Further Enhancements And Thoughts

Ok, by now you should now how to add post meta dynamically, how to retrieve the custom field values and how to dynamically prepare your title tags. This is a first step towards dynamic pages. The next step is to take a look at what information your users provide that you can use to further optimize your pages

Let’s assume you are running a site about browsers and browser optimization. When a user visits your site, his browser will gladly share the user agent with you and all sorts of interesting information like screen resolution, operating system, IP, etc.

All of those information can be used to enhance your site. For example you may want to dynamically highlight the category “Firefox” if the user is using a Firefox browser.

Of course, there are information that are not so easy to get and depending on your niche you may actually have to ask your user to get information that are useful to you, but there’s a great deal of information you can use right away. For example if visitors from the UK tend to like one specific section of your site a lot (look it up in Google Analytics or add a social metrics plugins to analyze the number of shares per country), why not highlight this section for all visitors from the UK?

In a nutshell, you need to think about your audience, their goals and what it is they are looking for and then you can do all sorts of optimizations that will benefit the user, your revenues and the overall user experience.