PHP Logo

Map A Value Between Scales

14th August 2020 - 4 minutes read time

A common technique when creating graphics or visual representations of data is to map a value between two scales. This is useful when working on a set of values and you need to map them to a different set of values in order to show them on a graph.

The maths involved here is essentially figuring out the relationship between the value (v) in our initial scale (x, y) and multiplying this by the maximum range of the second scale (a, b).

a + (b - a) * ((v - x) / (x - y))

Taking the value of 0.5, which is in the scale 0 to 1. Mapping this to the scale 0 to 100 means we make the following calculations.

0 + (100 − 0) × ((0.5 − 0) ÷ (1 − 0))

This results in the value of 50.

Converting this maths into a function in PHP we get the following.

Read the full article

WordPress Logo

The Correct Way To Load A Template File In WordPress

18th December 2010 - 5 minutes read time

Since WordPress 3.0 there has been a funcion called get_template_part(), which has been used quite a bit in the new Twenty Ten default WordPress theme. This is an evolution of the usual way to include parts of themes by using functions like get_header() to include the header.php file. This function helps code resuse by including the same files multiple files, but also allows child themes to override parent themes.

The function takes a required slug and an optional name as parameters, the two parameters being used together to select the correct template file to include. The best way to describe how this function works is to show how it would work in certain situations. Take the following call to get_template_part() using only the slug parameter.

Read the full article

WordPress Logo

Wordpress Breadcrumbs

19th December 2009 - 8 minutes read time

I have been playing about with breadcrumb trails in Wordpress recently so I thought I would post the code here in case anyone else finds it useful. The basic idea is to try to find out where the current page is situated within the site. This means checking for pages and categories with their parents. Once we get down to the post level we need to try some things in order to find out where the current post is within the site.

Once the trail array has been filled with items it is looped through and converted into a string.

To use this function put the following code into your functions.php file in your template.

Read the full article