admin管理员组文章数量:1134248
I Have this loop:
<?php
$post_ids_fetched = array(347, 258);
$editor = new WP_Query(
array(
'posts_per_page'=>4,
'post_type'=> 'earth',
'post__in' => $post_ids_fetched,
'orderby' => 'post__in',
'post_status' => 'publish'
)
);
if( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
?>
<table >
<tbody>
<tr>
<td class="am-t-lable">state</td>
<td class="am-t-value"><?php the_field('state'); ?></td>
</tr>
<tr>
<td class="am-t-lable">city</td>
<td class="am-t-value"><?php the_field('city'); ?></td>
</tr>
</tbody>
</table>
<?php }} ?>
How can the output of the loop be the following?
<table>
<tbody>
<tr>
<td class="am-t-lable">state</td>
<td class="am-t-value">state name 1</td>
<td class="am-t-value">state name 2</td>
<td class="am-t-value">state name 3</td>
<td class="am-t-value">state name 4</td>
</tr>
<tr>
<td class="am-t-lable">city</td>
<td class="am-t-value">city name 1</td>
<td class="am-t-value">city name 2</td>
<td class="am-t-value">city name 3</td>
<td class="am-t-value">city name 4</td>
</tr>
</tbody>
</table>
I have 40 ACF fields. Is it very wrong to put 40 separate loop inside each <tr>
?
<?php
$post_ids_fetched = array(347, 258);
$editor = new WP_Query(
array(
'posts_per_page'=>4,
'post_type'=> 'earth',
'post__in' => $post_ids_fetched,
'orderby' => 'post__in',
'post_status' => 'publish'
)
);
?>
<table >
<tbody>
<tr>
<td class="am-t-lable">state</td>
<?php
if( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
?>
<td class="am-t-value"><?php the_field('state'); ?></td>
<?php }} ?>
</tr>
<tr>
<td class="am-t-lable">state</td>
<?php
if( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
?>
<td class="am-t-value"><?php the_field('city'); ?></td>
<?php }} ?>
</tr>
....
</tbody>
</table>
I Have this loop:
<?php
$post_ids_fetched = array(347, 258);
$editor = new WP_Query(
array(
'posts_per_page'=>4,
'post_type'=> 'earth',
'post__in' => $post_ids_fetched,
'orderby' => 'post__in',
'post_status' => 'publish'
)
);
if( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
?>
<table >
<tbody>
<tr>
<td class="am-t-lable">state</td>
<td class="am-t-value"><?php the_field('state'); ?></td>
</tr>
<tr>
<td class="am-t-lable">city</td>
<td class="am-t-value"><?php the_field('city'); ?></td>
</tr>
</tbody>
</table>
<?php }} ?>
How can the output of the loop be the following?
<table>
<tbody>
<tr>
<td class="am-t-lable">state</td>
<td class="am-t-value">state name 1</td>
<td class="am-t-value">state name 2</td>
<td class="am-t-value">state name 3</td>
<td class="am-t-value">state name 4</td>
</tr>
<tr>
<td class="am-t-lable">city</td>
<td class="am-t-value">city name 1</td>
<td class="am-t-value">city name 2</td>
<td class="am-t-value">city name 3</td>
<td class="am-t-value">city name 4</td>
</tr>
</tbody>
</table>
I have 40 ACF fields. Is it very wrong to put 40 separate loop inside each <tr>
?
<?php
$post_ids_fetched = array(347, 258);
$editor = new WP_Query(
array(
'posts_per_page'=>4,
'post_type'=> 'earth',
'post__in' => $post_ids_fetched,
'orderby' => 'post__in',
'post_status' => 'publish'
)
);
?>
<table >
<tbody>
<tr>
<td class="am-t-lable">state</td>
<?php
if( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
?>
<td class="am-t-value"><?php the_field('state'); ?></td>
<?php }} ?>
</tr>
<tr>
<td class="am-t-lable">state</td>
<?php
if( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
?>
<td class="am-t-value"><?php the_field('city'); ?></td>
<?php }} ?>
</tr>
....
</tbody>
</table>
Share
Improve this question
edited Aug 1, 2023 at 12:48
och
asked Aug 1, 2023 at 11:52
ochoch
391 silver badge8 bronze badges
4
|
1 Answer
Reset to default 1This cannot be done inside the tr tags, you need to separate this out into two stages. First collect the data ( without displaying any HTML ), then display that data.
For example:
$editor = new WP_Query( [
'posts_per_page' => 20
] );
// each item in this array will be a post/an array of all
// the fields in a post
$table = [];
// and these are the items/rows we want to display
$labels = [ 'first', 'second', 'third' ];
// First fill $table with data
if ( $editor->have_posts() ) {
while( $editor->have_posts() ) {
$editor->the_post();
$table[] = [
'first' => get_post_meta( get_the_ID(), 'firstmeta', true ),
'second' => get_post_meta( get_the_ID(), 'second', true ),
'third' => get_post_meta( get_the_ID(), 'third..etc', true ),
];
}
}
// Second, display the data we just collected
?>
<table >
<tbody>
<?php
foreach( $labels as $label ) {
?>
<tr>
<th class="am-t-lable"><?php echo esc_html( $label ); ?></th>
<?php
foreach( $table as $post ) {
?>
<td class="am-t-value"><?php echo esc_html( $post[ $label ] ); ?></td>
<?php
}
?>
</tr>
}
?>
</tbody>
</table>
Importantly, this is not the only way to do something like this, there are lots of alternatives, some that don't involve loops at all but use functions such as array_map
etc.
The most important part, is that you don't have to fetch the data and display it at the same time and mix that code together. In the above example you could take each step and make them their own function completely separate! Separation of concerns is a good skill and best practice in programming.
本文标签: phpHow to show correct td of table tags in wpquery loop
版权声明:本文标题:php - How to show correct td of table tags in wp_query loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736852984a1955590.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_field
for the getter version in the ACF API. Importantly though, none of this is specific to WordPress or even PHP/web development it's generic programming knowledge. 1 loop to gather the data, a second to display the states, a third to display the cities – Tom J Nowell ♦ Commented Aug 1, 2023 at 12:36<tr>
? – och Commented Aug 1, 2023 at 12:42tr
– Tom J Nowell ♦ Commented Aug 1, 2023 at 13:20<thead>
element. I wonder if WPSE is the best stack to ask this on given that it isn't really a WordPress specific question but a general PHP/programming question – Tom J Nowell ♦ Commented Aug 1, 2023 at 13:23