• Resolved ginnywills

    (@ginnywills)


    I have tried so hard to follow the directions for adding custom fields. I am using the default template and only have basic contact details per member, but I have added a field called “Spouse Name”. It has shown on the back-end, but I can’t get it to show on the front end. The only thing that shows is the word ARRAY.

    I don’t know PHP so I am sure it’s just a comma or something. This is what I have added to the card.php file that I created:

    <div>
    <?php
    $text = $entry->getMeta(
    array(
    ‘id’ => ‘spouse_id’, // This should match exactly the field id used when registering the custom field.
    ‘single’ => TRUE // Do not change this.
    )
    );

    if ( ! empty( $text ) ) echo ” . esc_html( $text ) . ”;
    ?>
    </div>

Viewing 15 replies - 1 through 15 (of 26 total)
  • Plugin Author Steven

    (@shazahm1hotmailcom)

    Can you please add var_dump( $text ) before the “empty” line of code? This will not fix it, but it will let me know what is “in” the $text variable. Let me know what is output and then you can remove that line of text.

    Thanks!

    Thread Starter ginnywills

    (@ginnywills)

    Do you mean like this? I just tried this but it made everything disappear that was on the page.

    <div>
    <?php
    $text = $entry->getMeta(
    array(
    ‘id’ => ‘spouse_id’, // This should match exactly the field id used when registering the custom field.
    ‘single’ => TRUE // Do not change this.
    )
    );
    var_dump( $text )
    if ( ! empty( $text ) ) echo ” . esc_html( $text ) . ”;
    ?>
    </div>

    Plugin Author Steven

    (@shazahm1hotmailcom)

    Yes, but when you view the page, what did you see? It would be printed above ARRAY.

    Thread Starter ginnywills

    (@ginnywills)

    It broke the whole page– nothing showed but the header and footer of the website itself.

    Plugin Author Steven

    (@shazahm1hotmailcom)

    Add a semi colon after var_dump( $text ) so it is var_dump( $text );

    Thread Starter ginnywills

    (@ginnywills)

    Okay – it says this:

    array(1) { [“spouse_id”]=> array(1) { [0]=> string(4) “Andy” } } Array

    Thread Starter ginnywills

    (@ginnywills)

    If the field is empty, it says this:
    array(0) { }

    Plugin Author Steven

    (@shazahm1hotmailcom)

    ok, best guess…

    there must have been an error during the copy/paste or the editing of the code. it appears all the entry meta is being returned instead of just the specific id.

    can you please copy paste you code here that you used to register the custom text field and the code put in the template?

    please, use the CODE tags (button above reply area) so it is more easily readable and not altered by the forum when displayed.

    Thread Starter ginnywills

    (@ginnywills)

    This is what I put in the snippet plugin to register the field:

    `// Register the metabox and fields.
    add_action( ‘cn_metabox’, ‘cn_register_custom_text_field’ );

    function cn_register_custom_text_field() {

    $atts = array(
    ‘title’ => ‘Spouse Name’, // Change this to a name which applies to your project.
    ‘id’ => ‘spouse_name’, // Change this so it is unique to you project.
    ‘context’ => ‘normal’,
    ‘priority’ => ‘core’,
    ‘fields’ => array(
    array(
    ‘name’ => ‘Spouse Name’, // Change this field name to something which applies to you project.
    ‘show_label’ => TRUE, // Whether or not to display the ‘name’. Changing it to false will suppress the name.
    ‘id’ => ‘spouse_id’, // Change this so it is unique to you project. Each field id MUST be unique.
    ‘type’ => ‘text’, // This is the field type being added.
    ‘size’ => ‘regular’, // This can be changed to one of the following: ‘small’, ‘regular’, ‘large’
    ),
    ),
    );

    cnMetaboxAPI::add( $atts );
    }

    Thread Starter ginnywills

    (@ginnywills)

    `This is the code in card.php:

    <?php

    // Exit if accessed directly
    if ( ! defined( ‘ABSPATH’ ) ) {
    exit;
    }

    /**
    * @var cnOutput $entry
    */
    $style = array(
    ‘background-color’ => ‘#FFF’,
    ‘border’ => $atts[‘border_width’] . ‘px solid ‘ . $atts[‘border_color’],
    ‘border-radius’ => $atts[‘border_radius’] . ‘px’,
    ‘color’ => ‘#000’,
    ‘margin’ => ‘8px 0’,
    ‘padding’ => ’10px’,
    ‘position’ => ‘relative’,
    );
    $bio = $entry->getBio();
    $notes = $entry->getNotes();
    ?>
    <div class=”cn-entry” <?php echo cnHTML::attribute( ‘style’, $style ); ?>>

    <div class=”cn-left” style=”width:49%; float:<?php echo is_rtl() ? ‘right’ : ‘left’; ?>”>
    <?php

    if ( ‘none’ !== $atts[‘image_type’] ) {

    $entry->getImage(
    array(
    ‘image’ => $atts[‘image_type’],
    ‘width’ => $atts[‘image_width’],
    ‘height’ => $atts[‘image_height’],
    ‘zc’ => $atts[‘image_crop_mode’],
    ‘fallback’ => array(
    ‘type’ => $atts[‘image_fallback’] ? ‘block’ : ‘none’,
    ‘string’ => $atts[‘image_fallback_string’],
    ),
    ‘permalink’ => cnSettingsAPI::get( ‘connections’, ‘connections_link’, ‘name’ ),
    )
    );

    }

    ?>
    <div style=”clear:both;”></div>
    <div style=”margin-bottom: 10px;”>
    <div style=”font-size:larger;font-variant: small-caps”><strong><?php echo $entry->getNameBlock( array( ‘format’ => $atts[‘name_format’] ) ); ?></strong></div>
    <?php

    if ( $atts[‘show_title’] ) $entry->getTitleBlock();

    if ( $atts[‘show_org’] || $atts[‘show_dept’] ) {
    $entry->getOrgUnitBlock(
    array(
    ‘show_org’ => $atts[‘show_org’],
    ‘show_dept’ => $atts[‘show_dept’],
    )
    );
    }

    if ( $atts[‘show_contact_name’] ) {

    $entry->getContactNameBlock(
    array(
    ‘format’ => $atts[‘contact_name_format’],
    //’label’ => $atts[‘str_contact_label’]
    )
    );

    }

    ?>
    </div>

    </div>

    <div class=”cn-right” style=”text-align: right;”>

    <?php
    if ( $atts[‘show_addresses’] ) $entry->getAddressBlock( array( ‘format’ => $atts[‘address_format’] , ‘type’ => $atts[‘address_types’] ) );

    if ( $atts[‘show_family’] )$entry->getFamilyMemberBlock();

    if ( $atts[‘show_phone_numbers’] ) $entry->getPhoneNumberBlock( array( ‘format’ => $atts[‘phone_format’] , ‘type’ => $atts[‘phone_types’] ) );

    if ( $atts[‘show_email’] ) $entry->getEmailAddressBlock( array( ‘format’ => $atts[’email_format’] , ‘type’ => $atts[’email_types’] ) );

    if ( $atts[‘show_im’] ) $entry->getImBlock();

    if ( $atts[‘show_dates’] ) $entry->getDateBlock( array( ‘format’ => $atts[‘date_format’], ‘type’ => $atts[‘date_types’] ) );

    if ( $atts[‘show_links’] ) $entry->getLinkBlock( array( ‘format’ => $atts[‘link_format’], ‘type’ => $atts[‘link_types’] ) );

    if ( $atts[‘show_social_media’] ) $entry->getSocialMediaBlock();

    ?>

    </div>

    <div>
    <?php
    $text = $entry->getMeta(
    array(
    ‘id’ => ‘spouse_id’, // This should match exactly the field id used when registering the custom field.
    ‘single’ => TRUE // Do not change this.
    )
    );
    var_dump( $text );
    if ( ! empty( $text ) ) echo ” . esc_html( $text ) . ”;
    ?>
    </div>

    <div style=”clear:both”></div>

    <?php

    if ( $atts[‘show_bio’] && 0 < strlen( $bio ) ) {

    $entry->getBioBlock(
    array(
    ‘before’ => ‘<h4>’ . esc_html__( ‘Biographical Info’, ‘connections’ ) . ‘</h4>’ . PHP_EOL,
    ‘after’ => ‘<div class=”cn-clear”></div>’,
    )
    );

    }

    if ( $atts[‘show_notes’] && 0 < strlen( $notes ) ) {

    $entry->getNotesBlock(
    array(
    ‘before’ => ‘<h4>’ . esc_html__( ‘Notes’, ‘connections’ ) . ‘</h4>’ . PHP_EOL,
    ‘after’ => ‘<div class=”cn-clear”></div>’,
    )
    );

    }

    ?>

    <div class=”cn-meta” style=”margin-top: 6px”>
    <?php
    $entry->getContentBlock( $atts[‘content’], $atts, $template );
    ?>
    </div>

    <div class=”cn-clear” style=”display:table;width:100%;”>
    <div style=”display:table-cell;vertical-align:middle;”>
    <?php
    if ( $atts[‘show_categories’] ) {

    $entry->getCategoryBlock(
    array( ‘separator’ => ‘, ‘ )
    );
    }
    ?>
    </div>
    <div style=”display:table-cell;text-align:right;vertical-align:middle;”>
    <?php
    if ( $atts[‘show_last_updated’] ) {

    cnTemplatePart::updated(
    array(
    ‘timestamp’ => $entry->getUnixTimeStamp(),
    ‘style’ => array(
    ‘font-size’ => ’10px’,
    ‘font-variant’ => ‘small-caps’,
    ‘margin-right’ => ’10px’,
    )
    )
    );
    }

    if ( $atts[‘show_return_to_top’] ) {

    cnTemplatePart::returnToTop();
    }
    ?>
    </div>
    </div>

    </div>

    Thread Starter ginnywills

    (@ginnywills)

    any thoughts?

    Plugin Author Steven

    (@shazahm1hotmailcom)

    Unfortunately the code you posted was not between the code tags so it was hard to read to see if there was any errors.

    rather than try to figure out what is wrong with the code, just use this:

    
    $text = $entry->getMeta(
    		array(
    			'key'    => 'field_id', // This should match exactly the field id used when registering the custom field.
    			'single' => TRUE        // Do not change this.
    		)
    	);
     
    if ( ! empty( $text['spouse_id'][0] ) ) echo '' . esc_html( $text['spouse_id'][0] ) . '';
    

    Based on what you supplied as the var_dump() output, I think that should output “Andy”.

    I hope this helps, let me know.

    Thread Starter ginnywills

    (@ginnywills)

    Hiya – thanks for sticking with me on this! I have used the code you gave me and now the ARRAY is gone but its not displaying anything at all now. This is the code I used for card.php:

    <?php
    
    // Exit if accessed directly
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    /**
     * @var cnOutput $entry
     */
    $style  = array(
    	'background-color' => '#FFF',
    	'border'           => $atts['border_width'] . 'px solid ' . $atts['border_color'],
    	'border-radius'    => $atts['border_radius'] . 'px',
    	'color'            => '#000',
    	'margin'           => '8px 0',
    	'padding'          => '10px',
    	'position'         => 'relative',
    );
    $bio   = $entry->getBio();
    $notes = $entry->getNotes();
    ?>
    <div class="cn-entry" <?php echo cnHTML::attribute( 'style', $style ); ?>>
    
    	<div class="cn-left" style="width:49%; float:<?php echo is_rtl() ? 'right' : 'left'; ?>">
    		<?php
    
    		if ( 'none' !== $atts['image_type'] ) {
    
    			$entry->getImage(
    				array(
    					'image'    => $atts['image_type'],
    					'width'    => $atts['image_width'],
    					'height'   => $atts['image_height'],
    					'zc'       => $atts['image_crop_mode'],
    					'fallback' => array(
    						'type'   => $atts['image_fallback'] ? 'block' : 'none',
    						'string' => $atts['image_fallback_string'],
    					),
    					'permalink' => cnSettingsAPI::get( 'connections', 'connections_link', 'name' ),
    				)
    			);
    
    		}
    
    		?>
    		<div style="clear:both;"></div>
    		<div style="margin-bottom: 10px;">
    			<div style="font-size:larger;font-variant: small-caps"><strong><?php echo $entry->getNameBlock( array( 'format' => $atts['name_format'] ) ); ?></strong></div>
    			<?php
    
    			if ( $atts['show_title'] ) $entry->getTitleBlock();
    
    			if ( $atts['show_org'] || $atts['show_dept'] ) {
    				$entry->getOrgUnitBlock(
    					array(
    						'show_org'  => $atts['show_org'],
    						'show_dept' => $atts['show_dept'],
    					)
    				);
    			}
    
    			if ( $atts['show_contact_name'] ) {
    
    				$entry->getContactNameBlock(
    					array(
    						'format' => $atts['contact_name_format'],
    						//'label'  => $atts['str_contact_label']
    					)
    				);
    
    			}
    
    			?>
    		</div>
    
    	</div>
    
    	<div class="cn-right" style="text-align: right;">
    
    		<?php
    		if ( $atts['show_addresses'] ) $entry->getAddressBlock( array( 'format' => $atts['address_format'] , 'type' => $atts['address_types'] ) );
    
    		if ( $atts['show_family'] )$entry->getFamilyMemberBlock();
    
    		if ( $atts['show_phone_numbers'] ) $entry->getPhoneNumberBlock( array( 'format' => $atts['phone_format'] , 'type' => $atts['phone_types'] ) );
    
    		if ( $atts['show_email'] ) $entry->getEmailAddressBlock( array( 'format' => $atts['email_format'] , 'type' => $atts['email_types'] ) );
    
    		if ( $atts['show_im'] ) $entry->getImBlock();
    
    		if ( $atts['show_dates'] ) $entry->getDateBlock( array( 'format' => $atts['date_format'], 'type' => $atts['date_types'] ) );
    
    		if ( $atts['show_links'] ) $entry->getLinkBlock( array( 'format' => $atts['link_format'], 'type' => $atts['link_types'] ) );
    
    		if ( $atts['show_social_media'] ) $entry->getSocialMediaBlock();
    
    		?>
    
    	</div>
    
    <div>
    <?php
    $text = $entry->getMeta(
    		array(
    			'key'    => 'spouse_id', // This should match exactly the field id used when registering the custom field.
    			'single' => TRUE        // Do not change this.
    		)
    	);
    
    if ( ! empty( $text['spouse_id'][0] ) ) echo '' . esc_html( $text['spouse_id'][0] ) . '';?>
    </div>
    
    	<div style="clear:both"></div>
    
    	<?php
    
    	if ( $atts['show_bio'] && 0 < strlen( $bio ) ) {
    
    		$entry->getBioBlock(
    			array(
    				'before' => '<h4>' . esc_html__( 'Biographical Info', 'connections' ) . '</h4>' . PHP_EOL,
    				'after'  => '<div class="cn-clear"></div>',
    			)
    		);
    
    	}
    
    	if ( $atts['show_notes'] && 0 < strlen( $notes ) ) {
    
    		$entry->getNotesBlock(
    			array(
    				'before' => '<h4>' . esc_html__( 'Notes', 'connections' ) . '</h4>' . PHP_EOL,
    				'after'  => '<div class="cn-clear"></div>',
    			)
    		);
    
    	}
    
    	?>
    
    	<div class="cn-meta" style="margin-top: 6px">
    		<?php
    		$entry->getContentBlock( $atts['content'], $atts, $template );
    		?>
    	</div>
    
    	<div class="cn-clear" style="display:table;width:100%;">
    		<div style="display:table-cell;vertical-align:middle;">
    			<?php
    			if ( $atts['show_categories'] ) {
    
    				$entry->getCategoryBlock(
    					array( 'separator' => ', ' )
    				);
    			}
    			?>
    		</div>
    		<div style="display:table-cell;text-align:right;vertical-align:middle;">
    			<?php
    			if ( $atts['show_last_updated'] ) {
    
    				cnTemplatePart::updated(
    					array(
    						'timestamp' => $entry->getUnixTimeStamp(),
    						'style'     => array(
    							'font-size'    => '10px',
    							'font-variant' => 'small-caps',
    							'margin-right' => '10px',
    						)
    					)
    				);
    			}
    
    			if ( $atts['show_return_to_top'] ) {
    
    				cnTemplatePart::returnToTop();
    			}
    			?>
    		</div>
    	</div>
    
    </div>
    

    Hopefully I am pasting it correctly this time!

    Plugin Author Steven

    (@shazahm1hotmailcom)

    Not sure what to say. I just tested my code samples to make sure they is not an error. I did not find any and I know many others have used it. Here’s the code I just tested:

    Code Snippet:

    
    add_action( 'cn_metabox', 'cn_register_custom_text_field_spouse_name' );
    
    function cn_register_custom_text_field_spouse_name() {
    
    	$atts = array(
    		'title'    => 'Spouse Name',         
    		'id'       => 'spouse_name',
    		'context'  => 'normal',
    		'priority' => 'core',
    		'fields'   => array(
    			array(
    				'name'       => 'Spouse Name', 
    				'show_label' => TRUE,         
    				'id'         => 'spouse_id',  
    				'type'       => 'text',       
    				'size'       => 'regular',    
    			),
    		),
    	);
    
    	cnMetaboxAPI::add( $atts );
    }
    

    Template:

    
    $text = $entry->getMeta(
    		array(
    			'key'    => 'spouse_id', // This should match exactly the field id used when registering the custom field.
    			'single' => TRUE        // Do not change this.
    		)
    	);
    
    if ( ! empty( $text ) ) echo '' . esc_html( $text ) . '';
    

    This output “Andy” in the template.

    Hopefully this helps.

    Thread Starter ginnywills

    (@ginnywills)

    Yes! That worked! Thank you!

    The only thing is the label “Spouse Name” does not show on the front end. Is there a way to add that so it would say “Spouse Name: Andy”?

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘Displaying Custom Fields’ is closed to new replies.