Last active
May 17, 2017 13:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.