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
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.
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
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.
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
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
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
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
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
That's a bit of a different issue, so let's follow up in the other ticket I just created. Thank you!
Thanks Christian for all your help.