admin管理员组文章数量:1130349
I have a table that populates data pulled from ACF. It basically adds a PDF of a report if there's one available. I think I got that part figured out, but my code is awfully complicated and I'll like to simplify it with a switch statement or something similar. Also, how do I get the table to display a value of 'x' when there's no PDF file available? It's currently removing the cell and I don't want it to do that. Thanks in advance.
<table class="float-left table">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Annual Report</th>
<th scope="col">Interim Statement</th>
<th scope="col">Prospectus</th>
</tr>
</thead>
<tbody>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<?php if( have_rows( 'recent_documents') ): ?>
<tr>
<th>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</th>
<?php while( have_rows( 'recent_documents') ): the_row(); ?>
<?php if ( have_rows( 'new_document') ): ?>
<?php while ( have_rows( 'new_document') ): the_row(); ?>
<?php if (strpos(get_sub_field( 'report_type'), "Annual") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>annual</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Interim") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>interim</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Prospectus") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>Prospectus</a>
</td>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</tr>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</tbody>
</table>
I have a table that populates data pulled from ACF. It basically adds a PDF of a report if there's one available. I think I got that part figured out, but my code is awfully complicated and I'll like to simplify it with a switch statement or something similar. Also, how do I get the table to display a value of 'x' when there's no PDF file available? It's currently removing the cell and I don't want it to do that. Thanks in advance.
<table class="float-left table">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Annual Report</th>
<th scope="col">Interim Statement</th>
<th scope="col">Prospectus</th>
</tr>
</thead>
<tbody>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<?php if( have_rows( 'recent_documents') ): ?>
<tr>
<th>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</th>
<?php while( have_rows( 'recent_documents') ): the_row(); ?>
<?php if ( have_rows( 'new_document') ): ?>
<?php while ( have_rows( 'new_document') ): the_row(); ?>
<?php if (strpos(get_sub_field( 'report_type'), "Annual") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>annual</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Interim") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>interim</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Prospectus") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>Prospectus</a>
</td>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</tr>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</tbody>
</table>
Share
Improve this question
edited Oct 16, 2018 at 10:56
djboris
7585 silver badges10 bronze badges
asked Oct 16, 2018 at 6:18
fridayikonfridayikon
34 bronze badges
1
|
1 Answer
Reset to default 0As I am new here too, I can't comment and ask for clarification or give some suggestions, so I'll do that here.
Why do you have nested repeaters recent_documents and new_document? It appears that recent_documents always has only single row, otherwise there would be a mess. But anyway, working with what you have, you could do something like this:
<table class="float-left table">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Annual Report</th>
<th scope="col">Interim Statement</th>
<th scope="col">Prospectus</th>
</tr>
</thead>
<tbody>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<?php if( have_rows( 'recent_documents') ): ?>
<tr>
<th>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</th>
<?php
// We get the inner repeater (if it exists)
$documents = get_field( 'recent_documents' );
// Create our 'dictionary' array
$report_types = array( 'Annual', 'Interim', 'Prospectus' );
// Loop through it. This will always create three cells, even if there are no documents.
foreach ( $report_types as $key => $report_type ) : ?>
<td>
<?php
$new_document = ! empty( $documents[$key]['new_document'] ) ? $documents[$key]['new_document'][0] : false;
if ( ! $new_document
|| strpos( $new_document['report_type'], $report_type ) === false
|| empty( $new_document['file'] ) )
{
echo 'x';
continue;
}
echo '<a href="' . $new_document['file'] . '" target="_blank"><i class="fas fa-file-pdf"></i>' . $report_type . '</a>';
?>
</td>
<?php endforeach; ?>
</tr>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</tbody>
</table>
Let me know if this doesn't do what you wanted.
Please be aware that new_documents have to go in this particular order: Annual, Interim, Prospectus, otherwise it would skip documents even if they exist.
I have a table that populates data pulled from ACF. It basically adds a PDF of a report if there's one available. I think I got that part figured out, but my code is awfully complicated and I'll like to simplify it with a switch statement or something similar. Also, how do I get the table to display a value of 'x' when there's no PDF file available? It's currently removing the cell and I don't want it to do that. Thanks in advance.
<table class="float-left table">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Annual Report</th>
<th scope="col">Interim Statement</th>
<th scope="col">Prospectus</th>
</tr>
</thead>
<tbody>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<?php if( have_rows( 'recent_documents') ): ?>
<tr>
<th>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</th>
<?php while( have_rows( 'recent_documents') ): the_row(); ?>
<?php if ( have_rows( 'new_document') ): ?>
<?php while ( have_rows( 'new_document') ): the_row(); ?>
<?php if (strpos(get_sub_field( 'report_type'), "Annual") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>annual</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Interim") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>interim</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Prospectus") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>Prospectus</a>
</td>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</tr>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</tbody>
</table>
I have a table that populates data pulled from ACF. It basically adds a PDF of a report if there's one available. I think I got that part figured out, but my code is awfully complicated and I'll like to simplify it with a switch statement or something similar. Also, how do I get the table to display a value of 'x' when there's no PDF file available? It's currently removing the cell and I don't want it to do that. Thanks in advance.
<table class="float-left table">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Annual Report</th>
<th scope="col">Interim Statement</th>
<th scope="col">Prospectus</th>
</tr>
</thead>
<tbody>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<?php if( have_rows( 'recent_documents') ): ?>
<tr>
<th>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</th>
<?php while( have_rows( 'recent_documents') ): the_row(); ?>
<?php if ( have_rows( 'new_document') ): ?>
<?php while ( have_rows( 'new_document') ): the_row(); ?>
<?php if (strpos(get_sub_field( 'report_type'), "Annual") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>annual</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Interim") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>interim</a>
</td>
<?php endif; ?>
<?php if (strpos(get_sub_field( 'report_type'), "Prospectus") !==false): ?>
<td><a href="<?php the_sub_field('file'); ?>" target="_blank"><i class="fas fa-file-pdf"></i>Prospectus</a>
</td>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</tr>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</tbody>
</table>
Share
Improve this question
edited Oct 16, 2018 at 10:56
djboris
7585 silver badges10 bronze badges
asked Oct 16, 2018 at 6:18
fridayikonfridayikon
34 bronze badges
1
-
1
If you just fetch the contents of
recent_documents, you’ll see that it’s a big multidimensional array with all of your data. I find that much easier to output directly rather than using all thehave_rowsnonsense. – Milo Commented Oct 16, 2018 at 14:55
1 Answer
Reset to default 0As I am new here too, I can't comment and ask for clarification or give some suggestions, so I'll do that here.
Why do you have nested repeaters recent_documents and new_document? It appears that recent_documents always has only single row, otherwise there would be a mess. But anyway, working with what you have, you could do something like this:
<table class="float-left table">
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Annual Report</th>
<th scope="col">Interim Statement</th>
<th scope="col">Prospectus</th>
</tr>
</thead>
<tbody>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<?php if( have_rows( 'recent_documents') ): ?>
<tr>
<th>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</th>
<?php
// We get the inner repeater (if it exists)
$documents = get_field( 'recent_documents' );
// Create our 'dictionary' array
$report_types = array( 'Annual', 'Interim', 'Prospectus' );
// Loop through it. This will always create three cells, even if there are no documents.
foreach ( $report_types as $key => $report_type ) : ?>
<td>
<?php
$new_document = ! empty( $documents[$key]['new_document'] ) ? $documents[$key]['new_document'][0] : false;
if ( ! $new_document
|| strpos( $new_document['report_type'], $report_type ) === false
|| empty( $new_document['file'] ) )
{
echo 'x';
continue;
}
echo '<a href="' . $new_document['file'] . '" target="_blank"><i class="fas fa-file-pdf"></i>' . $report_type . '</a>';
?>
</td>
<?php endforeach; ?>
</tr>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</tbody>
</table>
Let me know if this doesn't do what you wanted.
Please be aware that new_documents have to go in this particular order: Annual, Interim, Prospectus, otherwise it would skip documents even if they exist.
本文标签: Build table using Advanced Custom Fields
版权声明:本文标题:Build table using Advanced Custom Fields 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749256344a2340540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


recent_documents, you’ll see that it’s a big multidimensional array with all of your data. I find that much easier to output directly rather than using all thehave_rowsnonsense. – Milo Commented Oct 16, 2018 at 14:55