4

I have created a custom post type and added some custom metaboxes to the page edit screen. My objective was to provide specific metaboxes which a user can edit which in turn would allow a user to modify specific content areas of a websites homepage.

Everything described above works perfectly except that I seem to be unable to set the page created from this custom post type as the "static page" within the wordpress admin "settings" area.

It seems the dropdown which lets your select a static page only shows pages located within the default wordpress "page" post_type.

How would I be able to change these settings to achieve my goal?

1
  • I would suggest to alternatively create a custom post type that would allow single posts to represent the content and then creating a taxonomy to mark that content for specific areas on the site? I've posted on this before: vocecommunications.com/blog/2010/06/… Nov 11, 2010 at 13:22

4 Answers 4

3

Add a filter for 'wp_dropdown_pages'. If 'name' => 'page_on_front', fill the list with your custom post types.

Here is a very primitive example as plugin. I took the custom post type from the codex.

<?php
/*
Plugin Name: Custom Post Type as Front Page
Description: Adds your custom post type to the list of available front pages.
Version:     0.1
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL v2
*/

add_action('init', 'my_custom_init');
function my_custom_init()
{
    $labels = array(
    'name' => _x('Books', 'post type general name'),
    'singular_name' => _x('Book', 'post type singular name'),
    'add_new' => _x('Add New', 'book'),
    'add_new_item' => __('Add New Book'),
    'edit_item' => __('Edit Book'),
    'new_item' => __('New Book'),
    'view_item' => __('View Book'),
    'search_items' => __('Search Books'),
    'not_found' =>  __('No books found'),
    'not_found_in_trash' => __('No books found in Trash'),
    'parent_item_colon' => ''
    );
    $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','author','thumbnail','excerpt','comments')
    );
    register_post_type('book',$args);
}

//add filter to insure the text Book, or book, is displayed when user updates a book
add_filter('post_updated_messages', 'book_updated_messages');
function book_updated_messages( $messages ) {
    global $post, $post_ID;

    $messages['book'] = array(
    0 => '', // Unused. Messages start at index 1.
    1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Book updated.'),
    /* translators: %s: date and time of the revision */
    5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Book saved.'),
    8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'),
    // translators: Publish box date format, see http://php.net/date
    date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    );

    return $messages;
}

// Here we go!
add_filter('wp_dropdown_pages', 'add_book_page_to_dropdown', 10, 1);

function add_book_page_to_dropdown( $select )
{
    // Not our list.
    if ( FALSE === strpos( $select, '<select name="page_on_front"' ) )
    {
        return $select;
    }

    $books = get_posts( array( 'post_type' => 'book' ) );

    if ( ! $books ) // no books found.
    {
        return $select;
    }

    $book_options = walk_page_dropdown_tree($books, 0,
         array(
            'depth' => 0
         ,  'child_of' => 0
         ,  'selected' => 0
         ,  'echo' => 0
         ,  'name' => 'page_on_front'
         ,  'id' => ''
         ,  'show_option_none' => ''
         ,  'show_option_no_change' => ''
         ,  'option_none_value' => ''
        )
    );

    return str_replace( '</select>', $book_options . '</select>', $select );
}

Activate, write a book page, go to Reading Settings, and choose that page as frontpage.

Screenshot

Hit Save Changes. Smile.

7
  • @toscho - Would you mind modifying your answer by providing an example? Nov 11, 2010 at 14:02
  • @NetConstructor.com Okay. :)
    – fuxia
    Nov 11, 2010 at 15:08
  • @Toscho -- cool, let me give it a try shortly Nov 11, 2010 at 20:26
  • @Toscho -- sorry but for some reason this does not seem to work for me. I have copied the data from you post starting at // Here we go! and have modified just the code related to the custom post type being called but the static page dropdown still only shows me my list of regular pages, no custom post types from my definition are in there. Nov 11, 2010 at 20:41
  • Hard to tell it works in my setup. How does your post type registration look exactly?
    – fuxia
    Nov 12, 2010 at 2:11
2

Unless a solution can be provided on this topic I will assume for the time being that the best way to get this to work is to just create a "home.php" file within ones template and query the post ID directly.

I have accepted this as the answer for the time being but if anyone finds a solution after this answer is accepted please post it and I will accept that as the correct answer.

1

It works - but next time i go to the reading settings, the Front Page - selection box displays just the "-- Select --" entry, not the custom type page i've selected before.

0

The solution works but I ran into the same problem as the OP. On the reading page the select returns to -select- , and on the site homepage the url shows the complete url path (http://mysite.com/book/this-is-a-book-test)

I think the problems may be due to the function needing all three... show_on_front page_on_front page_for_posts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.