Hi @tinothepro ,
Carbon Fields do not automatically output anything to the front-end (header/footer scripts fields being the only exceptions) so you have to use the provided collection of getter methods (e.g. carbon_get_post_meta
, carbon_get_theme_option
etc.) to output values in your theme.
Thread Starter
Sean T.
(@tinothepro)
Hi @atanasangelovdev,
Thanks for replying so quickly. What you said makes complete sense. Are you aware of any example search templates using CF? I am just thinking in terms of how a normal WP search returns a title and an excerpt (pretty standard), if you have multiple CF metaboxes set up and maybe some pages have different ones, how you’d query for all these and their variances (It’d be more than just the_excerpt()). I appreciate any resources or help you can provide.
There isn’t an automatic way to query what fields belong to what post type. However, you can add your own logic in your search template loop based on get_post_type()
.
For example:
<?php
$post_type = get_post_type();
if ( $post_type === 'post' ) {
echo carbon_get_the_post_meta( 'crb_some_post_field' );
} else if ( $post_type === 'page' ) {
echo carbon_get_the_post_meta( 'crb_some_page_field' );
echo carbon_get_the_post_meta( 'crb_some_page_field_2' );
}
// etc ...
?>
Thread Starter
Sean T.
(@tinothepro)
Hi @atanasangelovdev,
I had one more additional question regarding this. I tried several variations of adding this into the loop but have been unsuccessful in retrieving any data.
<?php while ( have_posts() ) : the_post(); ?>
<div class="result">
<span class="search-post-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</span>
<p><?php echo carbon_get_the_post_meta( 'content_section_column1' ); ?></p>
<span class="search-post-excerpt"><?php the_excerpt(); ?></span>
</div>
<?php endwhile; ?>
These fields are located in a simple builder plugin. Is there something I’m missing with this? Following your example did not output anything. I realized I’m not able to pull any of the meta information from the plugin even from functions.php. Ideas?
-
This reply was modified 7 years, 7 months ago by Sean T..
Can you share your container and field definitions code as well?
Thread Starter
Sean T.
(@tinothepro)
@atanasangelovdev sure thing. Let me know if you need additional information. These container/field definitions are in my plugin and I have no problem displaying their contents when using echo carbon_get_the_post_meta( ‘crb_some_post_field’ ); from inside the plugin.
I’ll link a gist here that shows the definition, initialization, and implementation in case you need more frame of reference.
https://gist.github.com/tinothepro/d43611ae20c307b30a4c72c1f52fd023
I appreciate all your help!
The gist you shared only contains a single reference to carbon_get_*
and from what I can see it is used correctly and should return an array of section entries as long as it is used in The Loop.
As for your earlier snippet – carbon_get_the_post_meta( 'content_section_column1' )
– you cannot access fields inside a complex directly. You must get the root complex with carbon_get_*
and then access the property you are interested in using the returned array.