Skip to content

Instantly share code, notes, and snippets.

@tjhole
Created July 23, 2014 11:04
Show Gist options
  • Select an option

  • Save tjhole/a1b35036c9481cc10235 to your computer and use it in GitHub Desktop.

Select an option

Save tjhole/a1b35036c9481cc10235 to your computer and use it in GitHub Desktop.
WORDPRESS: Custom Post Type Front Page
/**
* Adds CPTs to the list of available pages for a static front page.
*
* @param string $select Existing select list.
* @return string
*/
function add_cpt_to_front_page_dropdown( $select )
{
if ( FALSE === strpos( $select, '<select name="page_on_front"' ) )
{
return $select;
}
$cpt_posts = get_posts(
array(
'post_type' => 'YOUR_POST_TYPE'
, 'nopaging' => TRUE
, 'numberposts' => -1
, 'order' => 'ASC'
, 'orderby' => 'title'
, 'posts_per_page' => -1
)
);
if ( ! $cpt_posts ) // no posts found.
{
return $select;
}
$current = get_option( 'page_on_front', 0 );
$options = walk_page_dropdown_tree(
$cpt_posts
, 0
, array(
'depth' => 0
, 'child_of' => 0
, 'selected' => $current
, 'echo' => 0
, 'name' => 'page_on_front'
, 'id' => ''
, 'show_option_none' => ''
, 'show_option_no_change' => ''
, 'option_none_value' => ''
)
);
return str_replace( '</select>', $options . '</select>', $select );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment