Skip Navigation

[Resolved] How to add custom fields to PHP – Currently not displaying

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display custom fields in PHP using types_render_field, but it doesn't seem to be working.

Solution: Update to Types 3.0.7, use a specific post ID, and do not use the wpcf- field prefix.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by davidR-12 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1089321

I am trying to add custom fields to a pre-existing PHP file that displays popular downloads for a product. The current code is :

$title = apply_filters( 'the_title', $download->post_title, $download->ID );

The code to display it is :

  $out .= sprintf( $link, get_permalink( $download->ID ), esc_attr( $title_attr ), 'widget-download-title', $title );

I have changed this to :

$title = types_render_field("wpcf-resource-short-description", array( ));

However, this doesn't display anything.

Is there something I am missing here?

Regards,

David

#1089557

Hi, there were some issues in Types 3.0.6 related to the types_render_field function, and those should be resolved in Types 3.0.7, so please be sure you're up-to-date or have applied the patch file here: https://toolset.com/errata/types-fields-are-not-displayed-properly-using-types_render_field/

Another thing - you should not use the wpcf- prefix in types_render_field function calls. If that does not solve the problem immediately, try adding an explicit post ID to the arguments array to specify which post contains the custom field:

$title = types_render_field("resource-short-description", array( 'id' => $download->ID));

That's like adding the "id" attribute in a Types field shortcode. If none of these solves the problem, I'll need to take a closer look.

#1091273

Hi Christian,

Thank you that works perfectly for custom fields.

I also need to display the terms of a custom category for each post

>[wpv-post-taxonomy type="age-category" separator=" "]

How do I do this? I have tried echo do_shortcode :

[php]echo do_shortcode( '[wpv-post-taxonomy type="age-category" separator=" "]' );<?code>

But this doesnt display anything. I am presuming it doesnt work as it has no ID.

Regards,

David

#1092344

There's no specific Toolset API for displaying taxonomy terms, so do_shortcode with wpv-post-taxonomy would be required. Add the ID attribute like this:

echo do_shortcode( '[wpv-post-taxonomy type="age-category" separator=" " id="$download->ID"]' );

The other alternative is to bypass Views and Toolset and just use native WP code with wp_get_post_terms:

$term_list = wp_get_post_terms($download->ID, 'age-category');

Then iterate over the $term_list array and output whatever you want for each term.

#1094001

Hi Christian,

Thanks for your help. I tried the do_shortcode code but it didnt output anything at all.

The second option just displays the word Array. How do I display the content of the array

Regards,

David

#1094046

You can iterate over an array using foreach:

$term_list = wp_get_post_terms($download->ID, 'age-category');
foreach( $term_list as $term ) {
  // do something with each $term
  print_r($term, true);
}

http://php.net/manual/en/control-structures.foreach.php

#1094056

Hi Christian,

I have added that code and then used:

echo "<span>".$term_list."</span>";

but it still just outputs the word 'Array'

Regards,

David

#1094057

What exactly do you want to echo for each term? Here is an example that echoes each term name:

$term_list = wp_get_post_terms($download->ID, 'age-category');
foreach( $term_list as $term ) {
  // do something with each $term
  echo "<span>" . $term->name . "</span>";
}

http://php.net/manual/en/control-structures.foreach.php

#1095734

Hi Christian,

Thats great and works well. Perhaps you can help me with one more related thing. I have a view designed to display category terms. I am using a view instead of a category as I need to be able to colour each term depending on a color-picker custom field color assigned to it. This is working well but isnt working in my PHP file. The code I have is a simple shortcode to display the view:

echo do_shortcode ('[wpv-view name="module-terms-view"]');

It just displays "No items found". the view itself has a query filter of "Taxonomy is set by the current post"

Regards,

David

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/taxonomy-view-rendered-in-php-shows-no-results-found/

#1096216

That's a bit of a different issue, so let's follow up in the other ticket I just created. Thank you!

#1098461

Thanks Christian for all your help.