Categories: General

Genetics and Prejudice

clean-coding-best-practicesclean-coding-best-practices

Ever wanted to take a piece of
WordPress
functionality and put it in a non-WordPress portion of your website? Like,
for example, pulling your WordPress Sidebar and dropping it into a custom
page of yours?

Well, here’s how to make it work.

The Wrong Way

Many have tried doing this simple include below and have met with failure.
What they did was add the include by itself, like so:

wp_get_archives('type=monthly'); 

This ends in a fail whale because PHP doesn’t know what to do with that
request. Within a WordPress page it’s fine because you already have the
required headers loaded, but in your own custom web site they aren’t there.

So we’ll add them.

The Right Way

// turn off WordPress themes and include the WordPress core:
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');
wp_get_archives('type=monthly'); ?>

If you include the WordPress blog header first (wp-blog-header.php) and
then invoke the piece of code you want, you’ll be able to add
WordPress functionality anywhere you want on your custom pages!

The code above (get_archives) allows me to display my entire blog archive on any page of mine I want —
even outside of a WordPress-generated page.

Get a weekly breakdown of what’s happening in security and tech—and why it matters.

This lets you get tons of functionality from WordPress, including:

  1. Get a list of all your links

  2. Pull your tag cloud from your posts

  3. Pull your WordPress sidebar into a page

…and there are dozens more
here.

This is how I have created the page you’re reading this on. The entire page
is custom code; I only have a couple of pieces of WordPress embedded within
it —
the loop, and my archives toward the bottom on the right. This gives me the ability
to seamlessly blend my WordPress content with my custom content at
danielmiessler.com/study
and
danielmiessler.com/writing.

It’s the perfect combination — custom control + WordPress functionality.

Ping me at
daniel@danielmiessler.com
with any questions…

Links

[
WordPress’s get_archives | wordpress.org
][
WordPress Template Tags | wordpress.org
][
The WordPress Loop | wordpress.org
][
The WordPress Codex | wordpress.org
]

Gerald Businge

Leave a Comment
Share
Published by
Gerald Businge

Recent Posts

Betrayal in the City by Francis Imbuga

1. Introduction to the Text Title: Betrayal in the City Author: Francis Imbuga (Kenya) Genre:…

2 months ago

The Moon also sets by Osi Ogbu

Brief Context Author & Publication: Written by Nigerian author Osita (Osi) Ogbu, the novel was…

2 months ago

Meet Musinguzi John Paul – Your AI Teacher of Christian Religious Education for O-Level in Uganda

Step into a deeper understanding of faith, values, and biblical truth with Musinguzi John Paul,…

2 months ago

Meet Mbabazi Bridget – Your AI Teacher of Entrepreneurship for O-Level in Uganda

Are you ready to unlock your business potential and become a future innovator, job creator,…

2 months ago

Meet Alinda Violet – Your AI Agriculture Teacher for O-Level in Uganda!

Step into the future of farming, food security, and environmental stewardship with Alinda Violet, your…

2 months ago

Meet Aheebwa Joseph – Your AI Mathematics Teacher for O-Level Uganda!

Welcome to the world of numbers, logic, and problem-solving with Aheebwa Joseph, your intelligent and…

2 months ago