Skip to content

Instantly share code, notes, and snippets.

@blogjunkie
Created August 21, 2019 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blogjunkie/e3dbcedbd6cc0abf24e785a57c7052b0 to your computer and use it in GitHub Desktop.
Save blogjunkie/e3dbcedbd6cc0abf24e785a57c7052b0 to your computer and use it in GitHub Desktop.
<?php
/**
* Template for ClickWP knowledgebase post type
* @since 3.0.5
*/
/**
* TODO
* 1. Popular posts section - KB articles tagged 'popular'
* 2. Order posts in category by popularity - how?
* 3. Add taxonomy to post class for styling hooks - http://stackoverflow.com/a/18069231
*/
//* Force full width content layout to remove sidebar
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Add custom body class to the head
add_filter( 'body_class', 'click_kb_archive_body_class' );
function click_kb_archive_body_class( $classes ) {
$classes[] = 'kb-archive';
return $classes;
}
//* Ensure flat page header (remove angle header classes)
add_filter( 'body_class', 'click_kb_archive_body_class_page_header', 10, 2 );
function click_kb_archive_body_class_page_header( $wp_classes, $extra_classes ) {
// List the classes to be removed
$blacklist = array( 'header-angle-left', 'header-angle-right' );
// Filter the body classes
$wp_classes = array_diff( $wp_classes, $blacklist );
// Add the classes back
return array_merge( $wp_classes, (array) $extra_classes );
}
//* Output page header
add_action( 'clickwp_page_header', 'clickwp_kb_page_header_content' );
function clickwp_kb_page_header_content() {
genesis_markup( array(
'open' => '<h1 %s>',
'close' => '</h1>',
'content' => __( 'WordPress Knowledgebase', 'clickwp' ),
'context' => 'entry-title',
) );
}
//* Display a customized search form
add_filter( 'genesis_search_form', 'click_search_kb_only', 10, 3 );
add_action( 'clickwp_page_header', 'click_kb_search_form' );
function click_search_kb_only( $form, $search_text, $button_text ) {
$search_text = 'Search Knowledge Base';
$button_text = 'Go';
$kb_search_form = '
<form method="get" class="search-form" action="' . get_option('home') . '/" role="search">
<input type="search" name="s" placeholder="'. $search_text .'" />
<input type="submit" value="'. $button_text .'" />
<input type="hidden" name="post_type" value="knowledgebase" />
</form>
';
return $kb_search_form;
}
function click_kb_search_form() {
echo '<div class="kb-search">';
get_search_form( true );
echo '</div>';
}
//* Replace loop
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'KB_articles' );
function KB_articles(){
$taxonomy_name = 'knowledgebase_category';
$tax_terms = get_terms(
$taxonomy_name,
array(
'orderby' => 'slug',
// 'order' => 'DESC',
)
);
?>
<ul class="kb-cats">
<?php
// check if transient with page fragment exists
// if transient doesn't exist, proceed with query
$kb_archive_page_content = get_transient( 'click_kb_archive_content' );
if ( false === $kb_archive_page_content ) :
if( $tax_terms ) :
// start the output buffer to save contents of foreach loop
ob_start();
$ctr = 0;
foreach( $tax_terms as $tax_term ) :
wp_reset_query();
$category_link = get_term_link( $tax_term );
$category_name = $tax_term->slug;
$ctr++;
?>
<li><section class="kb-category">
<h3><a href="<?php echo esc_url( $category_link ); ?>"><?php echo $tax_term->name;?> <span>&rsaquo;</span></a></h3>
<?php
$args = array(
'posts_per_page' => 5,
'post_type' => 'knowledgebase',
'orderby' => 'date',
'order' =>'DESC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => $category_name
)
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
echo '<ul class="no-bullet">';
while( $query->have_posts() ) : $query->the_post();
$kb_article_title = get_the_title();
?>
<li <?php post_class(); ?>><a href=<?php the_permalink();?>><?php echo $kb_article_title; ?></a></li>
<?php
endwhile;
echo '</ul>';
else :
?>
<p>No available articles yet</p>
<?php
endif;
$count = $tax_term->count;
if($count != 0) :
?>
<p><a class="more-link" href="<?php echo esc_url( $category_link ); ?>">See all <?php echo $tax_term->count;?> articles</a></p>
<?php
endif;
?>
</section></li>
<?php
endforeach;
// save the output buffer contents in a variable
$kb_archive_page_content = ob_get_contents();
// clean the buffer as we will be using the variable from now on
ob_end_clean();
// set transient to last for 12 hours
set_transient('click_kb_archive_content', $kb_archive_page_content, 12 * HOUR_IN_SECONDS );
endif;
endif;
// output the new created fragment if transient does not exist.
echo $kb_archive_page_content;
echo '</ul><!-- end block grid -->';
}
wp_reset_postdata();
genesis();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment