• I have an custom posts archive template. I am using this to display my custom post types. But I want to have a page displayed at the very top, before all the custom post types are displayed. Is there a way to have this page displayed with the template I have set for it, at the top of this archives page?

Viewing 8 replies - 1 through 8 (of 8 total)
  • I believe we’ll need some more information about how you’ve set this up before anyone can help you.

    You’ve got an archive template for your custom post-type. Would that be an archive-{custom-post-type}.php file that is picked up by the WordPress Template hierarchy, or is it a Page template that you’ve created? A

    Please let us know so we can help you 🙂

    Thread Starter kevinsikora

    (@kevinsikora)

    In this case, my custom post type is (events). So I have a file called archive-events.php. Then to display each event, I have a file called content-events.php.

    At the “top” of archive-events.php, I want to display a page. The way I do it now is that I use the following function and pass it the page ID:


    function echo_page_description_mp($page_id)
    {
    $page = get_page($page_id);

    echo '<article>';

    echo '<div class="page-description">';
    echo '<p>';
    echo $page->post_content;
    echo '</p>';
    echo '</div>';

    echo '</article>';
    }

    With that approach, I am not using the template that I have attached to that page. I’ve basically created a new template for it so that I could insert it at the top of the archive-events.php template.

    All the PHP files I created myself. They are picked up automatically though by WordPress.

    Thanks for clearing that up.

    Where did you put the function definition for echo_page_description_mp()? In functions.php?

    and you’re calling the function at the top of the archive-events.php template with a hard-coded $page_id?

    When you visit the permalink for the page you want to borrow content from… does the content show up there?

    Thread Starter kevinsikora

    (@kevinsikora)

    Yes, the code for the echo_page_… is in the functions.php. I do hard code the page ID. The content does show up properly. So basically, everything works. But, I just don’t know how to use the template I have attached to that page to display its content. I would like to use the template, because other archive pages that I have will also use pages at the top. I would like to use that same template with them also.

    The way I do it now doesn’t strike me as a very good solution.

    This might sound silly, but are you sure the page id is valid? The only reason I ask is because if the page is not found get_page will return a null value.

    I’ve been known to accidentally transpose a digit-or-two when hard-coding ID’s. After looking at your code, myself, I can’t figure out why it’s not working.

    Thread Starter kevinsikora

    (@kevinsikora)

    The page ID is correct because I display the text from the page properly. So everything is working. I’m just wondering how do I take a page ID and then apply the related template to it for displaying in the archive page.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Applying a template to page in a posts archive page’ is closed to new replies.