Skip to content

Instantly share code, notes, and snippets.

@teolopez
Created May 13, 2014 01:16
Show Gist options
  • Save teolopez/6043d83731b0eba03259 to your computer and use it in GitHub Desktop.
Save teolopez/6043d83731b0eba03259 to your computer and use it in GitHub Desktop.
/**
*
* Generate Multiple Pages – Child Pages
*
**/
$boysTeams = array(
'Basketball',
'Baseball',
'Cross Country',
'Football',
'Golf',
'Soccer',
'Tennis',
'Track & Field',
'Volleyball',
'Water Polo',
'Wrestling',
);
foreach ($boysTeams as &$value) {
programmatically_create_child_pages($value, 'boys-teams');
}
// $boysTeams is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
/* Create Child Pages */
function programmatically_create_child_pages($titleName, $parentPage) {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
// Setup the author, slug, and title for the post
$author_id = 1;
$pageTitles = $titleName;
$spaceDash = str_replace(' ', '-', $pageTitles);
$slug = strtolower($spaceDash);
$title = $pageTitles;
$ID = ouhsd_college_career_get_ID_by_slug('athletics/'. $parentPage);
// Determine whether a variable is empty
if (!empty($titleName)) {
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'page',
'post_content' => 'The content goes here for the page',
'post_parent' => $ID,
)
);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
}
} // end programmatically_create_child_pages
add_filter( 'after_setup_theme', 'programmatically_create_child_pages' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment