Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active May 17, 2017 13:18
Show Gist options
  • Save morgyface/9d6c6eb67de509d419272acb12763354 to your computer and use it in GitHub Desktop.
Save morgyface/9d6c6eb67de509d419272acb12763354 to your computer and use it in GitHub Desktop.
WordPress | Grab content from a page with the same name as an archive
<?php
$archivetitle = post_type_archive_title('', false); // No prefix and false to display
$thepage = get_page_by_title( $archivetitle ); // Get page object using the archive title
$content = $thepage->post_content; // Get raw page content
if ( !empty( $content ) ) {
$content = apply_filters('the_content', $content); // Apply filters to add formating
echo $content;
if ( current_user_can( 'edit_pages' ) ) {
// Let us also add an edit link for ease during development
$pageid = $thepage->ID; // Get the page ID for the edit link
$editlink = get_edit_post_link( $pageid );
echo '<a href="' . $editlink . '" class="edit">Edit</a>';
}
}
?>
@morgyface
Copy link
Author

morgyface commented Jun 29, 2016

Where Page and Archive Collide

This is useful where you've created a custom post type which has the same name as a page. The permalink will take you to the archive and not the page. Here we address that by looking for and displaying content in a page with the same name as the archive. Add this PHP code to your themes existing archive.php file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment