Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Course catalog using table loop for Genesis
<?php
// replace the usual post listing with directory table
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'scl_course_table_loop', 10 );
function scl_course_table_loop() {
if ( have_posts() ) :
do_action( 'genesis_before_while' );
$headers = array( esc_html__('Course'), esc_html__('Number'), esc_html__('Instructor(s)'), esc_html__('Time') );
echo scl_loop_table_headers( $headers );
while ( have_posts() ) : the_post();
$data = array(
sprintf( '<a href="%s" title="%s">%s</a>', esc_url( get_permalink() ), the_title_attribute( 'echo=0' ), get_the_title() ),
get_field( 'course_number' ),
get_field( 'instructors' )
);
if ( count( $havetimes ) )
$data[] = get_field( 'times' );
echo scl_loop_table_cells( array_combine( $headers, $data ) );
endwhile; //* end of one post
scl_loop_table_footer();
do_action( 'genesis_after_endwhile' );
else : //* if no posts exist
do_action( 'genesis_loop_else' );
endif; //* end loop
}
// Table loops
function scl_loop_table_headers( $headers ) {
$headerrow = '';
foreach ( $headers as $header ) {
$headerrow .= sprintf( "<th>%s</th>\n", $header );
}
return sprintf( '<div class="loop"><table cellspacing="0" class="responsive">
<thead>
<tr>
%s
</tr>
</thead>
<tbody>'."\n", $headerrow );
}
function scl_loop_table_cells( $data ) {
$datarow = '';
$rowindex = 1;
foreach ( $data as $title => $field ) {
$class = '';
if ( empty( trim ( $field ) ) )
$class = ' class="empty"';
$tag = 'td';
if ( 1 == $rowindex )
$tag = 'th';
$datarow .= sprintf( '<%s title="%s" %s>%s</%1$s>'."\n", $tag, $title, $class, $field );
$rowindex++;
}
return sprintf( "<tr id='post-%d' %s>\n %s \n </tr>\n", get_the_ID(), genesis_attr( 'entry' ), $datarow );
}
function scl_loop_table_footer() {
return "</tbody>\n </table>\n</div><!-- .loop -->\n";
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.