Support

Account

Home Forums General Issues Custom post type category selector in a new page

Solving

Custom post type category selector in a new page

  • I have a site that has a custom post type: Testimonials. Inside it are a bunch of Advanced Custom Fields and such. The main component, of course, is a quote and attribution from someone, but it also has Category options.

    What I’m looking to set up is, when someone creates a new Page, there’s a dropdown/select option in there, too. The dropdown will list each Category from the Testimonials custom post type. Then, on the front-end, the testimonials from that category will display. So if someone chooses “yardwork” as a category from the dropdown in a new Page they’re making, when that page is viewed on the pubic front-end, it will be displaying only the testimonials from the “yardwork” category (extra bonus preference: most recent 3).

    I’m just unsure how to set this up. I saw that there’s a Taxonomy option in ACF (https://www.advancedcustomfields.com/resources/taxonomy/), and even saw a third-party plugin resource (https://wordpress.org/plugins/acf-advanced-taxonomy-selector/), too (although the latter was last updated over 4 years ago). I’m wondering how I would set up the ACF Field Group properly for what I want it to do, and then the second half would be the page template’s actual PHP that would basically say “Display (the most recent 3) testimonials only from the selected category.”

    Can anyone help me achieve this goal? I would very much appreciate it! Thank you in advance 🙂

  • There are 2 thing you need to do here. The first this would be to create the ACF taxonomy field and set it to load and set terms.

    The second thing has to do with showing the testimonials in each category. This is actually a core WP thing. You just need to create a template for the taxonomy. This works best when you create a custom taxonomy so that your testimonials do not get mixed into the standard post categories and visa-versa. https://codex.wordpress.org/Function_Reference/register_taxonomy. Then you create a template file named “taxonomy-YOUR-TAX-SLUG-HERE.php”

  • Thanks, John.

    The first part of what you said helped out a lot. I was able to set up a Field Group so that a dropdown appeared on a new Page and one of the Testimonial categories would be able to be selected.

    I’m a bit confused on the second part, however. I had created a new page template (and used it in my test page) so I could tinker with the correct syntax, but I can’t get it to appear. I’d think the syntax would be something like “if a testimonial category is chosen (ie not blank), display it” but it’s not really working out. I also tried using the Basic Display (single value) example syntax from https://www.advancedcustomfields.com/resources/taxonomy/ (even changing the ‘taxonomy field name’ to what I used in my field group), but nothing displays inside the H2 or p, so I know I’m doing something wrong.

    Let’s say my Testimonials custom post type has the following categories: Yardwork, Driveways, Windows, Doors, and Roofs. If I was trying to write the code block in a page template, what would the syntax for such a thing be?

    Or, if I’m making a template (using a slug as you say), can you give me an example of the code block syntax that would go in the template file as you referred to it?

  • If you’re using a page template to show testimonials then there is not automatic filtering of categories. In order to provide a select field or some other mechanism on this page you would need to build the page to filter by some type of url query string value. See the “Dynamic $_GET parameters” section of this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ Without doing this there is nothing passed to your page template that will let you filter the posts.

    If you use a custom taxonomy, for example let’s say you name it “testimonial-category” and you create a template for this taxonomy as I mentioned then WP will use this template files and automatically filter the testimonials for you and you simply need to link to that page. For example http://yoursite.com/tesimonial-category/doors/. This template file would work the same as the standard archives.php, category.php or tag.php file.

  • Hmm, perhaps I am approaching this from the wrong angle then.

    Remember those categories I mentioned? They have their own page on a site, about the category. And I gave an example of 5 but there’s really like a couple of dozen categories, with a page for each one.
    The way the current website is set up is that the testimonials are manually entered for each page. My thought was that, in a redesign, I could set up a Testimonials custom post type, give each a category, and then just use a page template in conjunction with ACF that would have a section at the bottom saying, “Pull in the testimonials that were selected on this page.” It also would be future-proof, in a way, if I had it set up to display the most recent 3, too; people wouldn’t have to manually go into the page and edit it as they do now.

    But if I have to create a taxonomy template for each of the dozen categories, and then additionally possibly change the URL structuring for them to get pulled in and display properly, that might not be worth all the effort.
    It’s also possible I’m just not following the suggestion, but my thoughts had originally been that I could simply edit a page template — even if it was THE page.php template, with a “if testimonials exist, display them from the selected category here” setup (with subsequent styling). But if making a several dozen templates to correspond to each selection is necessary to proceed, I don’t know if that’s the route to go for efficiency.

  • Sorry I did not get back to this sooner.

    But to try and answer the question….

    Even if I was going to add a field on a page to allow the client to select the testimonials that they want to show on that page I would still set up testimonials as a custom post type and create a custom taxonomy to allow categorization. I might even add multiple custom taxonomies depending on the clients needs. Why? Because this keeps things organized and allows for the most flexibility. If needed I can show all of the “posts” in an archive. I can show all of the posts in a specific category, etc. I could also build an interface that is use on pages with ACF to allow the client to select what testimonials to show, wither by category or allow them to select specific testimonials to display. If they wanted to show the most recent 5, I could give them a number field to determine how many to show. If they wanted to have “featured testimonials” shown on the home page, I could created a checkbox field on the testimonial to they could mark which ones they want featured and then I can display those. Using a custom post type and taxonomy removes any duplication of work, they just have to enter it once and there is freedom to do whatever is needed with the “Posts”.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Custom post type category selector in a new page’ is closed to new replies.