Skip to content

Instantly share code, notes, and snippets.

@billerickson billerickson/archive.php Secret
Last active Apr 30, 2019

Embed
What would you like to do?
<?php
/**
* Archive
*
* @package ClientName
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
/**
* Archive landing modules
*
*/
function ea_archive_landing_modules() {
if( ! ( is_category() || is_tag() ) || get_query_var( 'paged' ) )
return;
$taxonomy = is_category() ? 'category' : 'post_tag';
$loop = new WP_Query( array(
'post_type' => 'archive_landing',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( get_queried_object_id() ),
'include_children' => false,
)
)
));
if( ! $loop->have_posts() )
return;
$post_id = $loop->posts[0]->ID;
ea_modules( $post_id );
// Only remove archive header if the 'header' block is used
if( ea_has_module( 'header', $post_id ) )
remove_action( 'genesis_before_loop', 'genesis_do_taxonomy_title_description', 15 );
}
add_action( 'genesis_after_header', 'ea_archive_landing_modules' );
genesis();
<?php
/**
* Archive Landing
*
* @package CoreFunctionality
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
class EA_Archive_Landing {
/**
* Initialize all the things
*
* @since 1.2.0
*/
function __construct() {
add_action( 'init', array( $this, 'register_cpt' ) );
add_action( 'template_redirect', array( $this, 'redirect_single' ) );
}
/**
* Register the custom post type
*
* @since 1.2.0
*/
function register_cpt() {
$labels = array(
'name' => 'Landing Pages',
'singular_name' => 'Landing Page',
'add_new' => 'Add New',
'add_new_item' => 'Add New Page',
'edit_item' => 'Edit Page',
'new_item' => 'New Page',
'view_item' => 'View Page',
'search_items' => 'Search Pages',
'not_found' => 'No Pages found',
'not_found_in_trash' => 'No Pages found in Trash',
'parent_item_colon' => 'Parent Page:',
'menu_name' => 'Archive Landing',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'revisions' ),
'taxonomies' => array( 'post_tag', 'category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => false,
'menu_icon' => 'dashicons-editor-table', // https://developer.wordpress.org/resource/dashicons/
);
register_post_type( 'archive_landing', $args );
}
/**
* Redirect single
*
*/
function redirect_single() {
if( ! is_singular( 'archive_landing' ) )
return;
$taxonomy = get_post_meta( get_the_ID(), 'ea_connected_taxonomy', true );
$category = get_post_meta( get_the_ID(), 'ea_connected_category', true );
$tag = get_post_meta( get_the_ID(), 'ea_connected_tag', true );
$term_id = 'category' == $taxonomy ? $category : $tag;
if( empty( $term_id ) || empty( $taxonomy ) ) {
$redirect = home_url();
} else {
$term = get_term_by( 'term_id', $term_id, $taxonomy );
$redirect = get_term_link( $term, $taxonomy );
}
wp_redirect( $redirect );
exit;
}
}
new EA_Archive_Landing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.