php

Sort Posts by Category

php

February 16, 2016

When generating archive pages, it’s sometimes necessary to return all of the Posts (for a given Post Type), sorted by the Category for visitors to sort through easily.

Read More

Sourced here, this snippet can be inserted within single.php and used to isolate the previous and next navigation links when working with Custom Post Types that have multiple Taxonomies and Terms to allow for more streamlined navigation:

Read More

Use if statement found here that allows you to sort by the parent category, to show only relevant child categories on various landing pages:

Read More

Inherit Featured Image

php

December 10, 2015

If you’d like to continue using the_post_thumbnail in your theme, but do not want to use a fixed default, use this snippet to inherit images from parent pages.

Read More

Handy when listing date / event oriented posts on a feed. Sourced here.

Read More

Generate a list of WordPress posts, grouped by month, for the specified author.

This comes in handy when working with multiple authors, and needing to generate a list of archives for their content.

Read More

Stumbled across this bit of code here. Used to show the main navigation in a sidebar for all child and grandchild pages.

Read More

Alternative: echo wpautop(get_field(‘featured_summary’));

Read More

Single Post as Home Page

php

November 03, 2015

Add the following code to your functions.php in order to redirect your home.php template to the most recent single post

Read More

Adding Counters

php

October 08, 2015

Adding increasing increments to loops can help with styling elements, and revealing additional content. Here are few examples to implement counters with php.

Read More

An alternative to wp_list_categories() – using get_categories() with a loop can prove to be quite useful in terms of customizing category displays.

Read More

Sometimes transitional content is used in one area of a website, like to display hours in a Header or Sidebar. Instead of having the client (or yourself!) update this content every day, get your code to update it for you when the weekdays change.

Read More

Use this to grab the Featured Image from the post parent if the child page is not using a Feature Image.

Read More

I have dug all over the internet for a query like this one!

This will generate the get_archives_link needed to link to your Custom Post Type year/month url.

Read More

Drop the below if statement within a loop and add a count for each colour change that you need with the template.

This is a great example to reference if you’re building a scrolling website, and loading all pages in a single progression with alternating templates applied.

Read More

Follow this WordPress Codex example and this support forum comment, add this into your header.php file and style the resulting body class accordingly

Read More

Get Category by Name

php

June 26, 2015

Retrieve the ID of the first item in a loop:

$category = get_the_category(); 
echo $category[0]->cat_name;
Read More

For use with older browser version testing.

Read More

Create a menu that drills down to subpages as they exist.

Read More

Create two functions within functions.php. One will control the “if(has)” parameter that will be called within the page template, and one will contain the data that we want to display:

Read More

Follow these instructions to exclude custom post type category.

Read More
$top_page_url = get_permalink( end( get_ancestors( get_the_ID(), 'page' ) ) );

Use the above text in place of the_permalink(); in order to retrieve the link information for the topmost page ancestor.

Read More

Post Content by ID

php

May 13, 2015

echo apply_filters('the_content', get_post_field('post_content', $post_id));

Sourced here.

Update – July 28, 2015

This method can also be used to retrieve data from custom fields when working with plugins like Advanced Custom Fields:

echo apply_filters('widget_content', get_post_field('widget_content', $post->ID));
Read More

If page IDs or other classes are not appearing in the source code for a site that you’re working on, check the header.php file to see if body_class(); is being applied to the <body> tag.

Read More

Working with large sites that have cross-over content, sometimes it’s necessary to know how to bring multiple taxonomies into a single query.

Read More

PHP Redirect

WordPress

April 22, 2015

When creating parallax websites, or websites where all of the content is visible on one area, use the following to keep users from getting sidetracked from other areas of the website

Read More
<ul>
  <?php $n = 0; foreach($page->children() as $child): $n++; ?>
  <li class="<?php echo ($n%2) ? 'odd' : 'even' ?>">
    ...
  </li>
  <?php endforeach ?>
</ul>

You can change “odd” and “even” to whatever names you need.  Sourced here.

Read More

Using large sliding banners with linked content can be both a blessing and a curse.  This adds a beautiful level of style and information to the site, but also takes up a lot of space, and provides a lot of repetitive information that can be difficult for new visitors to navigate.

Read More

Auto-Updating Copyright Year

php

December 02, 2014

Using the_date(); this is very easy to achieve, all you need is  &copy; to get yourself setup. To take it a step further, you can include a date span, so your clients are aware of just how long you’ve been burning the midnight oil.

<p>&copy; 2013 <?php date('Y') == "2012" ? $year = "" : $year = "- ".date('Y'); echo $year; ?></p>

Sourced here.

Read More

Place this within the single.php template to grab the Featured Image and format it automatically inline with the content.

the_post_thumbnail('large', array('class' => 'alignright'));

This can also be used in the loop with alongside posts with featured images.

Read More

List Child Pages

php

September 30, 2014

Menus can be tricky. Rather than being locked into the just using menu sort order, or alphabetical order, combine the two using wp_list_pages(); You can style the drop downs to respond on hover using CSS.

Read More

I’ve run into problems several times where exclude and include do not work, or work selectively, and as a result, either all of the posts are included, or none of them are. I’ve found an answer that works both inside and outside of the loop.

Read More
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.