0 points

Add album post type to functions.php


dashboard

Fri Mar 06 2020 19:31:17 GMT+0000 (UTC)

Posted by @lbrand #php

/**
 * Add Album post type
 * 
 */
function create_posttype() {
 
    register_post_type( 'album',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'album' ),
                'singular_name' => __( 'album' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'album'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

/**
 * Show Album post type with Blog pages
 * 
 */
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'album') );

	return $query;
}
content_copy

I can't recall where I found the code above online, but I customized a bit for my application. I'll happily credit where it's due - LMK.



>> Browse more code snippets

more_vert