Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active August 29, 2015 13:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/9479317 to your computer and use it in GitHub Desktop.
Save trepmal/9479317 to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: Category Home Page
* Plugin URI: trepmal.com
* Description:
* Version:
* Author: Kailey Lampert
* Author URI: kaileylampert.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* TextDomain: category-home-page
* DomainPath:
* Network:
*/
$category_home_page = new Category_Home_Page();
class Category_Home_Page {
function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
}
function admin_init() {
register_setting( 'reading', 'chp_category_home_page', 'intval' );
register_setting( 'reading', 'chp_disable_archive', 'intval' );
register_setting( 'reading', 'chp_use_category_template', 'intval' );
add_settings_field( '_category_home_page', '<label for="chp_category_home_page">'. __( 'Category for Home Page' , 'category-home-page' ) .'</label>' , array( $this, '_form_field' ) , 'reading', 'default' );
}
function _form_field() {
$category = get_option( 'chp_category_home_page', '' );
$dropdown = true;
if ( $dropdown ) {
wp_dropdown_categories( array(
'orderby' => 'name',
'id' => 'chp_category_home_page',
'name' => 'chp_category_home_page',
'hide_empty' => false,
'show_option_none' => __( 'None', 'category-home-page' ),
'selected' => intval( $category ),
) );
} else {
echo '<input type="text" name="chp_category_home_page" value="'. esc_attr( $category ) .'" /> '. __( 'Category ID (-1 to disable)', 'category-home-page' );
}
echo '<p>';
$disable = get_option( 'chp_disable_archive', 0 );
echo '<input type="hidden" name="chp_disable_archive" value="0" />';
echo '<label><input type="checkbox" name="chp_disable_archive" value="1" '. checked( $disable, 1, false ) .' /> '. __( 'Disable default archive page', 'category-home-page' ) .'</label>';
$template = get_option( 'chp_use_category_template', 0 );
echo ' <input type="hidden" name="chp_use_category_template" value="0" />';
echo '<label><input type="checkbox" name="chp_use_category_template" value="1" '. checked( $template, 1, false ) .' /> '. __( 'Use category template', 'category-home-page' ) .'</label>';
echo '</p>';
}
function pre_get_posts( $query ) {
if ( is_admin() ) return;
if ( ! $query->is_main_query() ) return;
if ( -1 == ( $category = get_option( 'chp_category_home_page', -1 ) ) ) return;
if ( $query->is_category( $category ) && get_option( 'chp_disable_archive', 0 ) ) {
status_header( 404 );
$query->set_404();
}
if ( is_home() ) {
$query->set( 'cat', $category );
if ( get_option( 'chp_use_category_template', 0 ) ) {
$query->is_home = false;
$query->is_category = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment