Passer la navigation

[Résolu] How to output custom fields in an array?

This support ticket is created Il y a 4 années et 9 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Fuseau horaire du supporter : Asia/Hong_Kong (GMT+08:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par StanleyT8485 Il y a 4 années et 9 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1815249

Tell us what you are trying to do?
I want to output the custom fields of child posts in an array.

	$postID = get_the_ID();
	$query = new WP_Query( array(
		'post_type' => 'financial', // This is the child post
		'toolset_relationships' => array(
			'role' => 'child',
			'related_to' => $postID,
			'relationship' => 'company-financial', // This is the Toolset slug relationship
		),
	));
	while( $query -> have_posts() ) {
		$query -> the_post();
		$postID = get_the_ID();
		$pageCF = get_post_custom( $postID );
		$distributions = $pageCF[ 'wpcf-distributions' ]; // The custom field I want to output is wpcf-distributions
		print_r( $distributions );  // This prints out Array ( [0] => 11.7 ) Array ( [0] => 23.8 ) Array ( [0] => 26.7 ) Array ( [0] => 46.9 ) Array ( [0] => 28.6 ) Array ( [0] => 29 ) Array ( [0] => 30.3 )
	}

I want it to output in this format 11.7, 23.8, 26.7, 36.8, 28.6, 29, 30.3.

However, the result is structured in this format
Array ( [0] => 11.7 ) Array ( [0] => 23.8 ) Array ( [0] => 26.7 ) Array ( [0] => 46.9 ) Array ( [0] => 28.6 ) Array ( [0] => 29 ) Array ( [0] => 30.3 )

#1815987

Hello,

For example, you can relace these lines from:

while( $query -> have_posts() ) {
    $query -> the_post();
    $postID = get_the_ID();
    $pageCF = get_post_custom( $postID );
    $distributions = $pageCF[ 'wpcf-distributions' ]; // The custom field I want to output is wpcf-distributions
    print_r( $distributions );  // This prints out Array ( [0] => 11.7 ) Array ( [0] => 23.8 ) Array ( [0] => 26.7 ) Array ( [0] => 46.9 ) Array ( [0] => 28.6 ) Array ( [0] => 29 ) Array ( [0] => 30.3 )
}

To:

$distributions = array();
while( $query -> have_posts() ) {
    $query -> the_post();
    $postID = get_the_ID();
    $pageCF = get_post_custom( $postID );
    $distributions[] = $pageCF[ 'wpcf-distributions' ]; // The custom field I want to output is wpcf-distributions
}
print_r( $distributions );

More help:
lien caché

#1817557

My issue is resolved now. Thank you!