Display custom post custom fields everywhere in frontend
- This topic has 7 replies, 2 voices, and was last updated 8 years, 3 months ago by
Anh Tran.
-
AuthorPosts
-
January 19, 2017 at 3:28 AM #4841
sergeysemenov
ParticipantHello.
I purchased your bundle, so I have all tools.
I created custom fields for custom posts. Now I need to display these fields not only on single-custompost.php, but everywhere in frontend. For example, get values in product taxonomy for each custom post block. Also I created Term Metaboxes with image_advanced and I want them in slider on my homepage. When new term is created and images are attached, they automatically appear on homepage. For now, I figured out how to display these fields by post_id. But I want them update by adding new posts/terms.Looking forward to your assistance.
January 19, 2017 at 8:34 AM #4842Anh Tran
KeymasterHi Sergey,
To get the post meta and show it in the frontend, you can use the
rwmb_meta
function with the last parameter for the post ID:$meta = rwmb_meta( 'field_id', $args, $post_id ); echo $meta;
Getting the post ID depends on your (probably custom) query/structure of the website. If you're using
WP_Query
class to query posts and use$query->the_post()
in the custom loop, then you can omit that parameter, like this:$query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3, ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $meta = rwmb_meta( 'field_id' ); echo $meta; // Other things in the loop that you want to display } }
But if you don't use the similar code, you need to look at your code to get the correct post ID.
It happens similarly for term meta, but in this case, you need to use
get_term_meta()
function and it requires the term ID:$meta = get_term_meta( $term_id, 'field_id', true ); echo $meta;
Again, the term ID depends on how you get the terms. If you're using get_terms function, then you might want to use this code:
$terms = get_terms( array( 'taxonomy' => 'category', 'number' => 3, ) ); if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { foreach ( $terms as $term ) { $meta = get_term_meta( $term->term_id, 'field_id', true ); echo $meta; // Echo something else } }
Hope this helps.
January 20, 2017 at 2:55 AM #4861sergeysemenov
ParticipantThank you, Anh Tran. I caught the idea. It works.
One more question about 'Post' Field.
The aim is to make clonable dropdown select in my custom post template admin area to manually select 'related' custom posts and retrieve thumbnail and title of these posts in frontend. I managed to create such clonable dropdown field, so I can select my custom posts. But I can't figure out how to display these selected posts thumbs and titles in frontend. Please help!January 20, 2017 at 11:06 AM #4866Anh Tran
KeymasterHi Sergey,
Here is an example of showing related posts:
https://gist.github.com/rilwis/91f42412efc40f7a7d991779cc500d59
January 20, 2017 at 11:31 AM #4869sergeysemenov
ParticipantI can't figure out how to make Post Field clonable. Metabox Builder doesn't have such option for Post Field. Neither manual integration in functions.php give no result. Where am I wrong?
My manual functions.php code is
$meta_boxes[] = array( 'title' => 'Related Products', 'post_types' => 'product', 'fields' => array( array( 'id' => "{$prefix}related_products", 'type' => 'post', 'post_type' => 'product', 'clone' => true, ), ), );
January 20, 2017 at 12:29 PM #4870sergeysemenov
ParticipantI managed to make it clonable. The problem was in my database which already has such values (I tested different variants before).
So, my Post Fields are created in admin, but when I insert second part of code to single-product.php, my page is blank and doesn't load.Tell me, what am I doing wrong?
I put this to my single-product.php
<?php add_filter( 'the_content', function ( $content ) { $related_posts = rwmb_meta( 'rw_related_products' ); if ( empty( $related_posts ) || ! is_array( $related_posts ) ) { return $content; } // Use output buffering to use the template tags easier. ob_start(); global $post; foreach ( $related_posts as $related_post ) { // Setup the post object so we can use template tags. $post = get_post( $related_post ); setup_postdata( $post ); // Now you can use template tags. ?> <a class="related-post" href="<?php the_permalink(); ?>"> <?php the_post_thumbnail( 'thumbnail' ); ?> <h2><?php the_title(); ?></h2> </a> <?php } $content .= ob_get_clean(); // Reset the post object. wp_reset_postdata(); return $content; } );
January 20, 2017 at 1:41 PM #4871sergeysemenov
ParticipantI figured it out.
functions.php
$meta_boxes[] = array( 'title' => 'Related Products', 'post_types' => 'product', 'fields' => array( array( 'id' => "{$prefix}related_posts", 'type' => 'post', 'post_type' => 'product', 'clone' => true, ), ), );
single-product.php
<?php $related_posts = rwmb_meta( 'rw_related_posts' ); foreach ( $related_posts as $related_post ) { // Setup the post object so we can use template tags. $post = get_post( $related_post ); setup_postdata( $post ); // Now you can use template tags. ?> "> "> </div> <? }?> <?php wp_reset_postdata(); ?>
This works for me.
Thank you for your assistance, Anh Tran.
January 20, 2017 at 2:27 PM #4875Anh Tran
KeymasterGlad that you solved the problem! Feel free to open any topic if you have any trouble.
-
AuthorPosts
- The topic ‘Display custom post custom fields everywhere in frontend’ is closed to new replies.