admin管理员组

文章数量:1122832

I'm on wordpress 5.9+ and creating CPT's has become relatively easy. But having a landing page for a CPT is new grounds for me.

  • I have successfully created a custom post type called Career Finders
  • I have created an archive template for this CPT, named archive-career-finders.php
  • I can see that template being used from the browser when I go to my local URL my-site/career-finders

But how do we create either a page or post from WP-admin, that connects to this archive template, and "is" this landing page for the CPT?

I want editors to be able to add content to this landing page through wp-admin. This is where I am stuck...

My initial thought: On this new archive template, do I use the WP-Query loop to get that "1" page or post that represents the content needed to be displayed here, and also have that page set to Private? Is that the way to go?

Many thanks!

I'm on wordpress 5.9+ and creating CPT's has become relatively easy. But having a landing page for a CPT is new grounds for me.

  • I have successfully created a custom post type called Career Finders
  • I have created an archive template for this CPT, named archive-career-finders.php
  • I can see that template being used from the browser when I go to my local URL my-site.com/career-finders

But how do we create either a page or post from WP-admin, that connects to this archive template, and "is" this landing page for the CPT?

I want editors to be able to add content to this landing page through wp-admin. This is where I am stuck...

My initial thought: On this new archive template, do I use the WP-Query loop to get that "1" page or post that represents the content needed to be displayed here, and also have that page set to Private? Is that the way to go?

Many thanks!

Share Improve this question edited Mar 17, 2022 at 13:49 klewis asked Mar 15, 2022 at 20:42 klewisklewis 8991 gold badge14 silver badges29 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If I'm understanding your question properly, you are looking for templates. Copy your single.php or page.php from your theme and then rename it to single-career-finders.php.

This is the same thing you should have done for your archive page as well... Take the archive.php file from your theme, copy it and rename to archive-career-finders.php. It will have the appropriate query within it to pull your custom posts but you can alter the content as well. You don't have to add a separate WP_Query here.

Now edit this file and you'll have a custom presentation for your data separate from normal pages, posts, or other CPTs.

This page has a great explanation of how templates work within WordPress: https://developer.wordpress.org/themes/basics/template-hierarchy/

After some thinking, I'm taking the following approach

  • Under my CPT area in wp-admin/, create a public post that represents the content for my CPT landing page. This post is naturally located at my-site.com/career-finders/landing-content
  • From my child theme area, set code in place to hide this post from Archives pages, Search results, RSS feed, and Blog Roles. This given article inspired me on the process.
  • Within my archive-career-finders.php template, call the data from my /landing-content page with the help of setup_postdata($p);, inspired by this post
  • Now when a user goes to my-site.com/career-finders they will see landing page content for my CPT.

This seems to me as the easiest way to allow an wp-admin editor to add content to a post, that is tied to a custom Archive template, for a custom post type.

本文标签: How to add content to a post tied to an Archive template specifically for a custom post type