WordPress Custom Fields Primer Tutorial

WordPress Custom Fields Primer Tutorial

WordPress Custom Fields Primer Tutorial

I spy with my little eye something beginning with … 💡
Yes, 💡
Up above, 💡


Aw ... what a bright idea?! How come I click on the light bulb (💡) and it has dropdown options unique to this blog post?

Glad you asked. And all because …

Let’s talk about WordPress blog Custom Fields, and an associated great link we stumbled upon. We followed its advice to change Theme code … yay!!!! Except this time, it wasn’t (good ol’) header.php that changed for our Custom Field foray (we’ve codenamed “Operation 4A” … tee hee).

Working to a plan like this, often the first question is (especially with event-driven programming code) “Where is the intervention point?”.

You will need to enter your custom fields code inside the WordPress loop.
Look for the line that looks like this:

<?php while ( have_posts() ) : the_post(); ?>
You want to make sure that you add your code before the following line:

<?php endwhile; // end of the loop. ?>

… good advice, and looking in the themes “twentyten” folder several PHP files had these two. But trial and error (just placing <?php echo ‘yoo hoo!'; ?>) we found that “single.php” could be tweaked to change the WordPress blog webpage here, and intervene. Place the intervention near “endwhile” and it goes down the bottom of the post’s content, and near “while ( have_posts() ) : the_post();” you get it up near the top of the post’s content (the placement we opted for).

Working to a plan like this, often the next question is “What do you want to do now that you know where to intervene?” and here we decided we wanted a dropdown way to navigate to links that are related to the content in one of 5 ways …

  • a number corresponds to a posting number webpage at the WordPress (TwentyTen themed) blog here
  • a “-” (no spaces) delimited single “slug” word corresponds to this WordPress blog permalink
  • “tag/[tag-words]” corresponds to this WordPress [tag-words] tag lookup
  • “category/[category-words]” corresponds to this WordPress [category-words] category lookup
  • absolute URL will add to dropdown and try to navigate there

… to allow the user to “read up” on the posting to follow, perhaps.

And so … “How come I click on the light bulb and it has dropdown options unique to this blog post?”

The intervention code in “single.php” directly under “<?php while ( have_posts() ) : the_post(); ?>” …


<?php
$encsuffix='';
$enclosurev = get_post_meta($post->ID, 'linksofuse', true);

if ($enclosurev) {
if (strpos($enclosurev, "-") !== false || strpos($enclosurev, ",") !== false || strpos($enclosurev, "//") !== false) {
$encs=explode(",", $enclosurev);
for ($iqs=0; $iqs<sizeof($encs); $iqs++) {
if ($encsuffix == "") { $encsuffix="<select id='linkhints' class='linkhints' title='Our link hints for this blog posting' style='margin-right:20px;float:right;display:inline-block;width:60px;' onchange=\"if (this.value.length > 0) { window.open(this.value,'_blank'); }\"><option value=''>&#128161;</option></select>"; }
if (strpos($encs[$iqs], "//") !== false) {
$enctitle="URL " . $encs[$iqs];
$enccont=@file_get_contents("//" . explode("//", $encs[$iqs])[1]);
if (strpos($enccont, "</title>") !== false) {
$enctitle=explode(">", explode("</title>", $enccont)[0])[-1 + sizeof(explode(">", explode("</title>", $enccont)[0]))];
}
$encsuffix=str_replace("</select>", "<option value='" . $encs[$iqs] . "'>" . $enctitle . "</option></select>", $encsuffix);
} else if (strpos($encs[$iqs], "-") !== false) {
$encsm=explode("-", $encs[$iqs]);
$encsuffix=str_replace("</select>", "<option value='//www.rjmprogramming.com.au/ITblog/" . $encs[$iqs] . "'></option></select>", $encsuffix);
$enctitle=strtoupper(substr($encsm[0],0,1)) . strtolower(substr($encsm[0],1));
for ($jqs=1; $jqs<sizeof($encsm); $jqs++) {
$enctitle.=" " . strtoupper(substr($encsm[$jqs],0,1)) . strtolower(substr($encsm[$jqs],1));
}
$encsuffix=str_replace("></option></select>", ">" . $enctitle . "</option></select>", $encsuffix);
} else {
if (substr($encs[$iqs],0,1) >= '0' && substr($encs[$iqs],0,1) <= '9') {
$enctitle="Blog Posting " . $encs[$iqs];
$enccont=@file_get_contents("//www.rjmprogramming.com.au/ITblog/?p=" . $encs[$iqs]);
if (strpos($enccont, "</title>") !== false) {
$enctitle=explode(">", explode("</title>", $enccont)[0])[-1 + sizeof(explode(">", explode("</title>", $enccont)[0]))];
}
$encsuffix=str_replace("</select>", "<option value='" . "//www.rjmprogramming.com.au/ITblog/?p=" . $encs[$iqs] . "'>" . $enctitle . "</option></select>", $encsuffix);
} else if (strpos($encs[$iqs], "/tag/") !== false) {
$enctitle="Blog Tag " . str_replace("-"," ",str_replace("/tag/","",$encs[$iqs]));
$enccont=@file_get_contents("//www.rjmprogramming.com.au/ITblog" . $encs[$iqs]);
$encsuffix=str_replace("</select>", "<option value='" . "//www.rjmprogramming.com.au/ITblog" . $encs[$iqs] . "'>" . $enctitle . "</option></select>", $encsuffix);
} else if (strpos($encs[$iqs], "/category/") !== false) {
$enctitle="Blog Category " . str_replace("-"," ",str_replace("/category/","",$encs[$iqs]));
$enccont=@file_get_contents("//www.rjmprogramming.com.au/ITblog" . $encs[$iqs]);
$encsuffix=str_replace("</select>", "<option value='" . "//www.rjmprogramming.com.au/ITblog" . $encs[$iqs] . "'>" . $enctitle . "</option></select>", $encsuffix);
} else if (strpos($encs[$iqs], "tag/") !== false) {
$enctitle="Blog Tag " . str_replace("-"," ",str_replace("tag/","",$encs[$iqs]));
$enccont=@file_get_contents("//www.rjmprogramming.com.au/ITblog/" . $encs[$iqs]);
$encsuffix=str_replace("</select>", "<option value='" . "//www.rjmprogramming.com.au/ITblog/" . $encs[$iqs] . "'>" . $enctitle . "</option></select>", $encsuffix);
} else if (strpos($encs[$iqs], "category/") !== false) {
$enctitle="Blog Category " . str_replace("-"," ",str_replace("category/","",$encs[$iqs]));
$enccont=@file_get_contents("//www.rjmprogramming.com.au/ITblog/" . $encs[$iqs]);
$encsuffix=str_replace("</select>", "<option value='" . "//www.rjmprogramming.com.au/ITblog/" . $encs[$iqs] . "'>" . $enctitle . "</option></select>", $encsuffix);
}
}
}
}
}
?>

… looks for an optionally filled in blog posting Custom Field called “linksofuse” for a comma separated list of links as per the 5 types of definitions above, and shows these as dropdown option values under that light bulb (💡) under the blog posting title before the blog posting content (that includes the blog posting image we always have).

If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>