Custom Post Types


Hi Thierry, I'm having a look at custom post types and decided to do that with Beans. Maybe not advisable as I am very new to Beans.

I've set up my test custom post type (album) and entered a couple.

I'm having trouble displaying them. Some tutorials suggest grabbing archive.php from the parent theme and renaming it to archive-album.php but Beans doesn't appear to have an archive.php.

Could you point me in the right direction? An example of some code that would go in a template file? Something like that?

thanks, Steve.


Hey Steve,

Beans doesn't have an archive.php which means WP automatically falls back to index.php (more about WP template files). Beans makes it very easy for you to create template files and the only thing you need to add is beans_load_document(), always right at the bottom of the template file. Then you may add all your modifications from changing a simple class to drastically modifying the page. All changes must go above beans_load_document().

So for example you can add your archive-album.php in your child theme. Here is an example of the content:

<?php

// Your modifications if needed.
beans_remove_action( 'beans_post_image' ); // Example modification.

// Load the document which is always needed at the bottom of template files.
beans_load_document();

This really enables you to customize each template files and create custom page template. The difference with most themes is that you don't need to copy and paste the entire template page. All global modifications go to functions.php while al template specific goes to template files. For small modifications which wouldn't justify to add a page template, you may add it to functions.php wrapper in a if condition.

Note that your custom post type would load just find without adding a template file, it will look like the blog index but with your custom post type items.

Hope that helps,


Thanks Thierry, I'm not sure if it is my lack of experience with CTPs or Beans but I'm still having strange problems.

I have set up my CTP (album) with CPT UI and Advanced Custom Fields. The 3 files I have on the go at the moment are:

  • archive-album.php
  • single-album.php
  • templates/album-template.php

with none of those uploaded I am not getting the CPTs displayed when I use http://www.dallasdesign.uk/beans/albums/.

If I upload archive-album.php I still getting no CPTs displayed. However, if I upload templates/album-template.php I get the titles of the albums displayed. No other fields but album-template.php has to have

add_filter( 'beans_loop_query_args', 'beans_child_view_query_args' );

function beans_child_view_query_args() {

  return array(
    'post_type' => 'album',
    'paged' => get_query_var( 'paged' )
   );

}

within it or it does not display the titles.

I cannot get a single post type displayed with or without single-album.php being upoaded or not.


Hey Steve,

The templates/album-templates.php is incorrect. Before you work with friendly permalink, I would advise to set the links to main in Settings->Permalinks (this will also flush the urls which you might have not done yet). Once it is all working then you can tackle the permalinks. So you would access your post type using http://www.dallasdesign.uk/beans/?post_type=album for now.

The best way to see if your template file kicks in is to leave it blank or print something. You should only have archive-album.php and single-album.php. I notices the url you provided is albums and not album, did you register your post type with or without an "s"?

Thanks,

PS: when sharing code make sure to wrap it by selecting it and clicking the editor code icon.


That has helped. http://www.dallasdesign.uk/beans/?post_type=album now displays the titles (no other fields) but clicking on an album title link does call single-album.php but no fields are being displayed.

The CPT was registered as Post type slug = album


When you say "no other fields", do you mean your fields added via Advanced Custom Fields?



ACF handles the backend fields and makes it easy to retrieve data in the front end but they are not automatically added to the page. Here is how you can get fields value.

Then in you page template you would would add it using Beans hook. Pretty much all markup have dynamic action hooks firing around it as follow:

  • {$markup_id}_before_markup, fires before the opening markup.
  • {$markup_id}_prepend_markup, fires after the opening markup (not available for selfclosed markup).
  • {$markup_id}_append_markup, fires before the closing markup (not available for selfclosed markup).
  • {$markup_id}_after_markup, fires after the closing markup.

We have made it simple to find Markup IDs, you’ll need to first enable Beans development mode in your WordPress Admin->Appearance->Settings. Once the development mode is enabled, the Markup ID’s are output in a data-markup-id tag in the front-end, so you just have to inspect the page using your browser inspect and all markup ids will be displayed.

So for example if you have a field called my_field_name and you want to add it after the title, the code would be:

add_action( 'beans_post_title_after_markup', 'example_additional_content' );

function example_additional_content() {

 echo '<p>' .  get_field( 'my_field_name' ) . '</p>';

}

In your HTML you may use UIkit attributes which will make it easy to achieve pretty cool stuff.

Hope that helps,


It certainly does! Brilliant. Thanks so much Thierry.


Hi Thierry, I'm having a similar problem, and the above steps don't quite solve for me.

My new CPT successfully displays its single page, and I can access the single-client.php template file I put in my theme directory, but my template file can't successfully retrieve any field data. It can add static text with the location-specific action hooks you describe, but anytime I try to pull in any field data, it just causes the rest of the page load to fail. My browser gets a page that's truncated, in this case immediately after the page-header.

I changed the default hyphens in WP's field naming to underscores, but that didn't make it happier.

Here is a page where I would like to add my CPT fields client_tagline and client_website http://test2.infotamers.com/client/freydas-hands/

Here is my code that doesn't work (except for the "foo"):

add_action( 'beans_post_body_prepend_markup', 'add_custom_fields' );
function add_custom_fields() {
//    echo '<p>' .  get_field('client_tagline') . '</p>';
//    echo '<p>' .  get_field('client_website') . '</p>';
      echo '<p>foo</p>';
}

Can you tell me what I'm missing? Thanks, lydia


Hey Lydia,

Do you have WP_DEBUG set to true (see documentation here). If yes what is the error you are seeing?

Just to confirm, you are using ACF plugin on your install? I am asking that because I am suspecting that the error you are getting is caused by get_field() which is an ACF function, which would throw and error if you are not using ACF.

Thanks,


Hey Lydia,

Do you have WP_DEBUG set to true (see documentation here). If yes what is the error you are seeing?

Just to confirm, you are using ACF plugin on your install? I am asking that because I am suspecting that the error you are getting is caused by get_field() which is an ACF function, which would throw and error if you are not using ACF.

Thanks,


Thanks for your help! It's true, I was missing ACF -- oops. So I installed it, and recreated my custom fields there, and now I can retrieve a custom text field as expected. But not a URL (even when I try my best to treat it just as text) nor an image. Instead what I get is nothing at all where the custom field should retrieve, though any tags I add around it appear fine.

I'm looking at the ACF docs and your explanation above and I don't find what's different. Can't find anything wrong with the field names, which are parallel (with the text one that works) and super-simple. I turned on debugging + debug logging and still don't seem to have a debug.log; i installed Debug Bar and Query Monitor and still haven't found any errors to look at.

Obviously it thinks there isn't a value for that field. But there is. I'm stumped.


Update: solved for images. I just needed a way to get at the array.

    $image = get_field('client_screenshot');
    $src = $image['url'];
    echo '<div class="screenshot"><img src="' .  $src . '" alt="client screenshot" /></div>';

Now to figure out what's up with a text field containing a URL.


Now also solved for links. Unclear why adding this extra step seems to be necessary, but it does work.

    $url = get_field('client_website');
    echo '<p class="clientsite"><span class="label">Visit client site at:</span> <a href="' .  $url . '">' . $url . '</a>';

Sorry to bother you. Grateful for your help; hoping these snippets may help someone else in future.


Hey Lydia, I am glad to hear you found the solution.

For good practices, take a look at WordPress escaping. It is a wide topic often forgotten by developers, though it is very important. I cannot really go into details but there is a zillion articles out there talking about WordPress Validation and Escaping 🙂

Happy coding,


Thanks for the pointer. 🙂 (Though, umm, this is just fielded data that I've entered myself. Not actually requiring escaping.)

Write a reply

Login or register to write a reply, it's free!