wasmo-theme/taxonomy-question.php

111 lines
3 KiB
PHP
Raw Normal View History

<?php
/**
* The template for displaying archive pages for questions
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Nineteen
* @since 1.0.0
*/
get_header();
$termid = get_queried_object()->term_id;
$term = get_term_by( 'id', $termid, 'question' );
?>
<section id="primary" class="content-area">
<main id="main" class="site-main">
<article class="entry">
<header class="entry-header">
<h1 class="entry-title"><?php echo $term->name ?></h1>
</header><!-- .page-header -->
<?php
//define transient name - taxid + user state.
$transient_name = 'answers-tax-question-' . $termid . '-' . is_user_logged_in();
if ( current_user_can('administrator') ) {
$transient_name = time();
}
//use transient to cache data
if ( false === ( $the_answers = get_transient( $transient_name ) ) ) {
//get users
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
'count_total' => false,
'fields' => 'all',
'who' => '',
);
$users = get_users( $args );
//user loop
foreach ( $users as $user ) {
$userid = $user->ID;
//questions loop
if( have_rows( 'questions', 'user_' . $userid ) ) {
// loop through the rows of data
while ( have_rows( 'questions', 'user_' . $userid ) ) {
the_row();
$termtaxid = get_sub_field( 'question', 'users_' . $userid );
//check if they answered this question
if ( $termtaxid === $termid ) {
// answer
$the_answers .= '<div class="answer answer-' . $userid . '">';
$the_answers .= '<blockquote>';
$the_answers .= get_sub_field( 'answer', 'users_' . $userid );
$the_answers .= '</blockquote>';
// user attribution - photo and name and link (only if they want to be listed in directory)
if ( 'true' === get_field( 'in_directory', 'user_' . $userid ) ||
'private' === get_field( 'in_directory', 'user_' . $userid ) && is_user_logged_in() ) {
$userimg = get_field( 'photo', 'user_' . $userid );
$username = esc_html( $user->nickname );
$the_answers .= '<cite>';
$the_answers .= '<a class="person person-' . $userid . '" href="' . get_author_posts_url( $userid ) . '">';
$the_answers .= '<span class="directory-img">';
if ( $userimg ) {
$the_answers .= wp_get_attachment_image( $userimg, 'medium' );
} else {
$the_answers .= '<img src="' . get_stylesheet_directory_uri() . '/img/default.svg">';
}
$the_answers .= '</span>';
$the_answers .= '<span class="directory-name">' . $username . '</span>';
$the_answers .= '</a>';
$the_answers .= '</cite>';
}
$the_answers .= '</div>';
}
}
}
}
set_transient( $transient_name, $the_answers, 24 * HOUR_IN_SECONDS );
}
?>
<div class="entry-content answers">
<?php echo $the_answers; ?>
</div>
<footer class="entry-footer">
</footer>
</article>
</main><!-- #main -->
</section><!-- #primary -->
<?php
get_footer();