Advertisement
Business

WordPress as a Knowledge Base

by

At work we previously used KBPublisher to manage our knowledge base. It costed money, it was hard to style, code is encrypted with ion cube and so on, basically very hard to maintain. WordPress can do the same things and even better.

This tutorial will show you how to use custom taxonomies for knowledge base sections and custom posts for knowledge base articles.


Step 1 Administration

Knowledge base sections and articles need to be managed. WordPress makes this easy to do with custom taxonomies and custom post types.

Just register the new taxonomy and post type. Add the following into functions.php from your theme.

For more information on the ins and outs of this functionality, read our other custom post type and custom taxonomy articles.

function register_kb() {
	register_post_type( 'knowledgebase',
		array(
			'labels' => array(
				'name' => 'Knowledge Base',
				'menu_name' => 'Knowledge Base',
				'singular_name' => 'Article',
				'all_items' => 'All Articles'
			),
			'public' => true,
			'publicly_queryable' => true,
			'show_ui' => true,
			'show_in_menu' => true,
			'show_in_nav_menus' => true,
			'menu_position ' => 20,
			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'post-formats', 'revisions' ),
			'hierarchical' => false,
			'taxonomies' => array( 'section' ),
			'has_archive' => true,
			'rewrite' => array( 'slug' => 'knowledgebase', 'hierarchical' => true, 'with_front' => false )
		)
	);
	
	register_taxonomy( 'section', array( 'knowledgebase' ),
		array(
			'labels' => array(
				'name' => 'Sections',
				'menu_name' => 'Sections',
				'singular_name' => 'Section',
				'all_items' => 'All Sections'
			),
			'public' => true,
			'hierarchical' => true,
			'show_ui' => true,
			'rewrite' => array( 'slug' => 'knowledgebase-section', 'hierarchical' => true, 'with_front' => false ),
		)
	);
}
add_action( 'init', 'register_kb' );

function kb_rewrite_rules( $wp_rewrite ) {
	$new_rules = array( 'knowledgebase/(.*)/(.*)' => 'index.php?post_type=knowledgebase§ion=' . $wp_rewrite->preg_index( 1 ) . '&knowledgebase=' . $wp_rewrite->preg_index( 2 ) );
	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'kb_rewrite_rules' );

This will run after WordPress has finished loading but before any headers are sent, registering the post type and taxonomy. Also, it will add the rewrite rules for the permalinks of the taxonomy and post type.

The register_post_type registers the custom post type, this is used for the KB articles. The register_taxonomy registers the custom taxonomy, this is used for the KB sections. The articles won't be hierarchical but the sections will be, so it gives the possibility to create a tree structure.

Also it would be nice to show the sections that an article is assigned to.

function kb_columns( $defaults ) {
	$defaults['section'] = 'Sections';
	return $defaults;
}
add_filter( 'manage_knowledgebase_posts_columns', 'kb_columns' );

function kb_custom_column( $column_name, $post_id ) {
	$taxonomy = $column_name;
	$post_type = get_post_type( $post_id );
	$terms = get_the_terms( $post_id, $taxonomy );
	if ( !empty( $terms ) ) {
		foreach ( $terms as $term ) {
			$post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . '</a>';
		}
		echo join( ', ', $post_terms );
	}
	else echo '<i>No terms.</i>';
}
add_action( 'manage_knowledgebase_posts_custom_column', 'kb_custom_column', 10, 2 );

Now add some sections and some articles so there is something to show.


Step 2 Showing the Sections

Add the following into functions.php from your theme.

function kb_sections( $sections = array(), $active_section = null ) {
	$taxonomy = 'section';
	$link_class = '';
	if ( empty( $sections ) ) {
		$link_class = 'root';
		$sections = get_terms( $taxonomy, array( 'parent' => 0, 'hide_empty' => 0 ) );
		$active_section = kb_active_section();
		echo '<ul id="kb-sections" class="unstyled">';
	}
	if ( empty( $active_section ) ) {
		$active_section = '';
	}
	foreach ( $sections as $section ) {
		$toggle = '';
		$section_children = get_terms( $taxonomy, array( 'parent' => $section->term_id, 'hide_empty' => 0 ) );
		if ( !empty( $section_children ) && $link_class != 'root' ) {
			$toggle = '<i class="toggle"></i>';
		}
		echo '<li class="' . ( $section->term_id == $active_section ? 'active' : '' ) . '">';
		echo '<a  href="' . get_term_link( $section, $taxonomy ) . '" class="' . $link_class . '" rel="' . $section->slug . '">' . $toggle . $section->name . '</a>';
		
		if ( !empty( $section_children ) ) {
			echo '<ul id="' . $section->slug . '" class="children">';
			kb_sections( $section_children, $active_section );
		}
		echo "</li>";
	}
	echo "</ul>";
}

function kb_active_section() {
	$taxonomy = 'section';
	$current_section = '';
	if ( is_single() ) {
		$sections = explode( '/', get_query_var( $taxonomy ) );
		$section_slug = end( $sections );
		if ( $section_slug != '' ) {
			$term = get_term_by( 'slug', $section_slug, $taxonomy );
		} else {
			$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
			$term = $terms[0];
		}
		$current_section = $term->term_id;
	} else {
		$term = get_term_by( 'slug', get_query_var( $taxonomy ), get_query_var( 'taxonomy' ) );
		$current_section = $term->term_id;
	}
	return $current_section;
}

In the theme folder create a file called sidebar-sections.php where your kb_sections function will be called outputting an unordered and nested list of sections.

<?php kb_sections(); ?>

Like this, the KB sections can be shown everywhere as desired by including the sidebar.

<?php get_sidebar( 'sections' ); ?>

Step 3 Showing the Articles

Add the following into functions.php from your theme.

function kb_article_permalink( $article_id, $section_id ) {
	$taxonomy = 'section';
	$article = get_post( $article_id );
	$section = get_term( $section_id, $taxonomy );
	$section_ancestors = get_ancestors( $section->term_id, $taxonomy );
	krsort( $section_ancestors );
	$permalink = '<a href="/knowledgebase/';
	foreach ($section_ancestors as $ancestor):
		$section_ancestor = get_term( $ancestor, $taxonomy );
		$permalink.= $section_ancestor->slug . '/';
	endforeach;
	$permalink.= $section->slug . '/' . $article->post_name . '/" >' . $article->post_title . '</a>';
	return $permalink;
}

Note: this method is necessary because an article can be linked to multiple sections.

This will generate a hierarchical permalink structure.

Like: /knowledgebase/section-slug/sub-section-slug/another-sub-section-slug/article-slug

In the theme folder then create these files: archive-knowledgebase.php, single-knowledgebase.php, content-knowledgebase.php, taxonomy-section.php.

In archive-knowledgebase.php add the following to show the sections and recent articles.

<?php
get_header();
get_sidebar( 'sections' );
while ( have_posts() ) : the_post();
	$terms = wp_get_post_terms( $post->ID, 'section' );
	$term = $terms[0];
	echo kb_article_permalink( $post->ID, $term->term_id );
endwhile;
get_footer();
?>

In single-knowledgebase.php add the following.

<?php
get_header();
while ( have_posts() ) : the_post();
	get_template_part( 'content', 'knowledgebase' );
endwhile;
get_footer();
?>

In content-knowledgebase.php add the following.

<?php
get_sidebar( 'sections' );
the_title();
the_content();
?>

In taxonomy-section.php add the following to show a list of articles from a section.

<?php
global $wp_query;
$args = array_merge( $wp_query->query, array( 'posts_per_page' => 100 ) );
query_posts( $args );
$term = get_term_by( 'slug', get_query_var( 'term' ), 'section' );

get_header();
get_sidebar( 'sections' );
while ( have_posts() ) : the_post();
	echo kb_article_permalink( $post->ID, $term->term_id );
endwhile;
get_footer();
?>

This can be structured and styled as desired.


Example

A real life example of how this works and how it can be used: syneto.net


References

Related Posts
  • Code
    Creative Coding
    Advanced Use of Attachments in WordPress: Creating Custom Queries for AttachmentsAdvanced use of attachments in wordpress 400
    This tutorial is the second in a four part series in which you'll learn some techniques for working with images in attachments in WordPress which give you advanced options.Read More…
  • Code
    Creative Coding
    Advanced Use of Attachments in WordPress: Assigning Categories and Taxonomy Terms to AttachmentsAdvanced use of attachments in wordpress 400
    This tutorial is the first in a four part series in which you'll learn some techniques for working with images in attachments in WordPress which give you advanced options. Read More…
  • Code
    WordPress
    Quick Tip: Post Types, Taxonomies and PermalinksPost types taxonomies urls
    Custom Post Types and taxonomies are two powerful features of WordPress. Unfortunately, they can have a tendency to cause problems if developers aren't familiar with how permalinks, URLs, and rewriting works within WordPress. In this quick tip, we aim to cover the topic very briefly to make sure you know all you need to know about WordPress URLs, custom post types, taxonomies, and how they all relate.Read More…
  • Code
    Theme Development
    How to Create a Homepage With Multiple Listings Using Custom QueriesFrontpage custom queries preview 400
    WordPress gives you a couple of built-in options for your website's front page: a display of your most recent blog posts or a static page of your choice. But what if you want to create something a bit more interesting than that? If your site has a lot of data using categories, taxonomies or custom post types to organize it, you might want to display data of more than one kind on your homepage. The good news is that you can do this using a custom template for the homepage. In this template you write a number of custom queries to list the data in the way you want to. In this tutorial you'll learn how to do the following: Create a custom homepage template for use by your theme Include four queries in that template to display different kinds of data, including three custom queries The queries will list standard posts plus one custom post type registered for this project, called 'animal'. You can find the template file with the four queries in the code bundle.Read More…
  • Code
    Theme Development
    Creating an Image-Based Archive Page: Getting StartedCreating an image based archive page
    Using images when displaying an archive listing in WordPress can be very powerful. It can increase the visual appeal of your archive pages and your site overall and it can help to illustrate what your site is all about.Read More…
  • Code
    Plugins
    Creating Client Testimonials With Custom Post TypesCreating client testimonials with custom post types
    If you're running any type of business online, it's a good idea to get your clients' opinions on what they thought about the services you provided. First of all, this can benefit you by giving you feedback on how you can improve aspects of your business, but most of all, it can give you great testimonials, which can help to persuade potential clients to use your services.Read More…