• Resolved Donalda

    (@gechodesigns)


    I have been trying for 2 days to figure out how and where to put code to show my boxes from the backend (admin area) to the front end (template).

    In the admin area, everything works and saves perfectly. But in the front of the site on the pages, nothing is showing up at all! I’ve tried just about every snippet I could replace my own box data over the snippets and nothing is helping! I cannot get it to show up.

    <?php if( !empty( $grouped_results ) ): ?>
    <table class="lg-directory-table">
    
    <?php foreach( $grouped_results as $key => $grouped_result ): ?>
    	
    	<?php if( !empty( $group_posts ) ): ?>
    	<tr>
    		<th colspan="<?php echo count( $args['fields'] ); ?>" scope="rowgroup">
    			<?php $term = get_term($key, LG_PREFIX . 'directory_group'); ?>
    			<?php echo $term->name; ?>
    		</th>
    	</tr>
    	<?php endif; ?>
    	
    	<?php if( 
    		!isset( $args['template_options']['show_headers'] )
    		|| $args['template_options']['show_headers'] 
    	): ?>
    	<tr>
    		<?php foreach( $args['template_options']['fields'] as $field_name ): ?>
    			<th><small><?php echo apply_filters('lg_directory_member_field_header', $field_name ); ?></small></th>
    		<?php endforeach; ?>
    	</tr>
    	<?php endif; ?>
    	
    	<?php global $post; ?>
    	<?php foreach( $grouped_result as $post ): setup_postdata($post); ?>
    		
    		<?php
    			
    			$member = array();
    			$member_metabox = cmb2_get_metabox( LG_PREFIX . 'directory_member' , LG_PREFIX . 'directory_member' );
    			
    			foreach( $member_metabox->prop( 'fields' ) as $field_id => $field ) {
    				
    				$field_value = get_post_meta( $post->ID, $field_id, true );
    				
    				if( $field_value ) {
    					$array_field_id = str_replace( LG_PREFIX . 'directory_member_', '', $field_id );
    					$member[ $array_field_id ] = $field_value;
    				}
    			}
    		
    		?>
    	
    		<tr>
    			<?php foreach( $args['template_options']['fields'] as $field_name ): ?>
    			
    				<td>
    					<span class="<?php echo 'lg-directory-' . $field_name; ?>">
    					<?php							
    						$field_value = '';
    						if( isset( $member[$field_name] ) ) {
    							$field_value = $member[$field_name];
    						}
    						echo apply_filters('lg_directory_member_field_value', $field_value, $field_name, $member, $args );
    					?>
    					</span>
    				</td>
    				
    			<?php endforeach; ?>	
    		</tr>
    		
    	<?php endforeach; wp_reset_postdata(); ?>
    	
    <?php endforeach; ?>
    
    </table>
    <?php endif; ?>
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Let’s scale things back a little bit, and start small before expanding.

    My first question: are you trying to display the metabox form? or are you trying to display the contents saved to the metabox within the admin area already?

    Asking because the code above is a respectful small hot mess of various things. For example, what’s supposed to be in the $grouped_results array variable? As is, it looks like it has keyed indexes that are term IDs that also hold their own array that are apparently either post objects or post IDs that you then iterate over and grab metabox setups for, and attempt to output some post meta from those same objects/IDs.

    So, what’s the most important part from the above that you’re wanting to show?

    Thread Starter Donalda

    (@gechodesigns)

    Thanks, and I know it’s a horrible jumble if anything.

    What I am trying to do is that I built a directory in the admin area that contains name, address, email address, bio, and a picture. I am trying to have it show the directory listing of the person when you click their link. So far it doesn’t work. I can’t figure out where to put the template or if I’m not supposed to make a template and instead call it in functions, and then it would show up based on the page or category.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Noted on the grand overall intent here, a directory listing.

    That said, if I was doing something like this, 9 times out of 10, I’d make this a post type. Each entry/person in the directory, would be a post in that post type. This way, the post title could act as the person’s name, post content could be their biography, featured image could be their portrait. Then, with CMB2, I could start working out the rest of the details, email address, physical address, phone number, etc etc. At that point, I’d have all the information I need.

    Next would be the post type archive on the frontend, instead of listing the posts in the more traditional “blog” style where you have the title, post date, author, post content, etc. You could use the archive template to output the names and permalinks into a table or however which way you want to present that. Then when you click each person’s name, you could be lead to their single post permalink, and start listing out the profile information, bio, photo, etc.

    This would potentially save you a lot of hassle with trying to work with that bit above and hopefully get you much more on your way. It’d also allow you to use all of WordPress’ functions and code that’s already available to you

    As a concrete example that’s similar, on one of my personal sites recently, I’ve set up a “book” post type. I am saving various bits of details like book title, description, total pages, ISBN, photo, book author, etc and am starting to show that information on my frontend. Intent for me is to imitate certain parts of goodreads.com on my own. I’m using post types and CMB2 to power it all.

    Thread Starter Donalda

    (@gechodesigns)

    How would I go about getting CMB2 to display the information? Because I’ve been trying using templates for Single posts and everything and putting in little blurbs of code I’ve found, but nothing seems to allow me to display anything other than what WordPress already has which is title and content. No other fields will display.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Standard, available in all WordPress get_post_meta() functions. Use the meta keys will be the ID values used for the individual fields. If you have a CMB2 textarea that has an ID of “user_address”, then something like echo get_post_meta( get_the_ID(), 'user_address', true ); should echo out that value. CMB2 doesn’t store anything anywhere unusual. If you’re working with a post object type, it’ll be saved as post meta. If you’ve created some fields for terms, it’ll all be in term meta. Same thing for user meta if you made use of “user” object type. If you’ve set everything up as an options page, then instead of get_*_meta() you’d want to make use of get_option() since those will end up being saved as part of the wp_options table.

    Thread Starter Donalda

    (@gechodesigns)

    Thanks so much!! I finally get it. Wow! Was I going about it the wrong way. Thanks the echo get_post_meta( get_the_ID(), 'user_address', true ) was exactly what I was looking for. Now, I can go about getting the rest of my site done. Thanks so much for your patience and looking at my eyesore of an excuse for code.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

    Hi Michael,

    This is what I am trying to achieve:

    I have created a field using cmb2, it is textarea and field id is _example https://prnt.sc/trm9ud
    and I want to use _example content from the textarea to output on wysiwyg editor

    I tried to use code above with the id:
    echo get_post_meta( get_the_ID(_example), ‘user_address’, true );

    but it does not output anything. This is how the whole field looks like https://prnt.sc/trmdeo

    Thanks.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    @techhelpnc can you provide the code you’re using for this, including the CMB2 configuration? I find it odd that “_example” is being output as the field value in some spots, when you say it’s the specified CMB2 ID value.

    Also, for what it’s worth, your get_post meta line above should be:

    echo get_post_meta( get_the_ID(), ‘_example’, true );

    The ID here for “get_the_ID()” is the numeral post ID, while your CMB2 field ID is going to be “_example”, which gets used as the meta key

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to show the fields in the front end?’ is closed to new replies.