Skip to content
wiki.fftac.org

Template Functions - Source Excerpt 02

Back to Template Functions

Summary

This source excerpt preserves a bounded section of 2IA.org/wp-content/themes/twoia-intelligence/inc/template-functions.php so readers can inspect the evidence without opening the full source file.

**Source path:** 2IA.org/wp-content/themes/twoia-intelligence/inc/template-functions.php

$links = array(
		array( 'label' => __( 'Home', 'two-identities-anonymous' ), 'url' => home_url( '/' ) ),
		array( 'label' => __( 'Start Here', 'two-identities-anonymous' ), 'url' => home_url( '/start-here/' ) ),
		array( 'label' => __( 'Research Archive', 'two-identities-anonymous' ), 'url' => home_url( '/research-archive/' ) ),
		array( 'label' => __( 'Methodology', 'two-identities-anonymous' ), 'url' => home_url( '/methodology/' ) ),
		array( 'label' => __( 'Topic Hubs', 'two-identities-anonymous' ), 'url' => home_url( '/topic-hubs/' ) ),
		array( 'label' => __( 'About', 'two-identities-anonymous' ), 'url' => home_url( '/about/' ) ),
		array( 'label' => __( 'Support', 'two-identities-anonymous' ), 'url' => home_url( '/support/' ), 'class' => 'menu-item--support' ),
		array( 'label' => __( 'Contact', 'two-identities-anonymous' ), 'url' => home_url( '/contact/' ) ),
	);

	$output = '';
	foreach ( $links as $link ) {
		$class   = ! empty( $link['class'] ) ? ' class="' . esc_attr( $link['class'] ) . '"' : '';
		$output .= sprintf(
			'<li%1$s><a href="%2$s">%3$s</a></li>',
			$class,
			esc_url( $link['url'] ),
			esc_html( $link['label'] )
		);
	}

	return $output;
}
add_filter( 'wp_nav_menu_items', 'twoia_primary_menu_items', 10, 2 );

/**
 * Footer menu fallback.
 */
function twoia_footer_menu_fallback() {
	?>
	<ul class="menu footer-menu fallback-menu">
		<li><a href="<?php echo esc_url( home_url( '/about/' ) ); ?>"><?php esc_html_e( 'About', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/methodology/' ) ); ?>"><?php esc_html_e( 'Methodology', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/research-archive/' ) ); ?>"><?php esc_html_e( 'Research Archive', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/public-records-and-foia/' ) ); ?>"><?php esc_html_e( 'Public Records and FOIA', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/privacy-policy/' ) ); ?>"><?php esc_html_e( 'Privacy Policy', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/lawful-contact/' ) ); ?>"><?php esc_html_e( 'Lawful Contact', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/corrections-and-right-of-reply/' ) ); ?>"><?php esc_html_e( 'Corrections', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/contact/' ) ); ?>"><?php esc_html_e( 'Contact', 'two-identities-anonymous' ); ?></a></li>
	</ul>
	<?php
}

/**
 * Render visible breadcrumbs for source-controlled pages and articles.
 *
 * @param array|null $page Optional virtual page.
 */
function twoia_render_breadcrumbs( $page = null ) {
	$items = array(
		array(
			'label' => __( 'Home', 'two-identities-anonymous' ),
			'url'   => home_url( '/' ),
		),
	);

	if ( is_singular() && ! $page ) {
		$items[] = array(
			'label' => __( 'Research Archive', 'two-identities-anonymous' ),
			'url'   => home_url( '/research-archive/' ),
		);
		$items[] = array(
			'label' => get_the_title(),
			'url'   => get_permalink(),
		);
	} elseif ( is_array( $page ) ) {
		if ( ! empty( $page['route_type'] ) && 'brief' === $page['route_type'] && ! empty( $page['parent_page'] ) ) {
			$items[] = array(
				'label' => $page['parent_page']['title'],
				'url'   => home_url( '/' . trim( $page['parent_slug'], '/' ) . '/' ),
			);
		}

		$items[] = array(
			'label' => $page['title'],
			'url'   => home_url( '/' . trim( $page['route_path'] ?? $page['slug'], '/' ) . '/' ),
		);
	} elseif ( is_archive() || is_search() ) {
		$items[] = array(
			'label' => twoia_context_label(),
			'url'   => function_exists( 'twoia_get_canonical_url' ) ? twoia_get_canonical_url() : home_url( '/' ),
		);
	}

	if ( count( $items ) < 2 ) {
		return;
	}
	?>
	<nav class="twoia-breadcrumbs" aria-label="<?php esc_attr_e( 'Breadcrumbs', 'two-identities-anonymous' ); ?>">
		<ol>
			<?php foreach ( $items as $index => $item ) : ?>
				<li>
					<?php if ( $index < count( $items ) - 1 ) : ?>
						<a href="<?php echo esc_url( $item['url'] ); ?>"><?php echo esc_html( $item['label'] ); ?></a>
					<?php else : ?>
						<span aria-current="page"><?php echo esc_html( $item['label'] ); ?></span>
					<?php endif; ?>
				</li>
			<?php endforeach; ?>
		</ol>
	</nav>
	<?php
}

/**
 * Render related posts by category.
 */
function twoia_related_posts() {
	$category_ids = wp_get_post_categories( get_the_ID() );

	if ( empty( $category_ids ) ) {
		return;
	}

	$related = new WP_Query(
		array(
			'category__in'        => $category_ids,
			'post__not_in'        => array( get_the_ID() ),
			'posts_per_page'      => 3,
			'ignore_sticky_posts' => true,
			'no_found_rows'       => true,
		)
	);

	if ( ! $related->have_posts() ) {
		wp_reset_postdata();
		return;
	}
	?>
	<section class="section section--compact related-briefings" aria-labelledby="related-briefings-title">
		<div class="section-header section-header--row">
			<div>
				<p class="eyebrow"><?php esc_html_e( 'Adjacent files', 'two-identities-anonymous' ); ?></p>
				<h2 id="related-briefings-title"><?php esc_html_e( 'Related briefings', 'two-identities-anonymous' ); ?></h2>
			</div>
		</div>
		<div class="grid grid--cards">
			<?php
			while ( $related->have_posts() ) {
				$related->the_post();
				get_template_part( 'template-parts/content', 'card' );
			}
			wp_reset_postdata();
			?>
		</div>
	</section>
	<?php
}

/**
 * Render a safe breadcrumb-like context line.
 */
function twoia_context_label() {
	if ( is_category() ) {
		return single_cat_title( '', false );
	}

	if ( is_tag() ) {
		return single_tag_title( '', false );
	}

	if ( is_search() ) {
		return sprintf(
			/* translators: %s: Search query. */
			esc_html__( 'Search results for ā€œ%sā€', 'two-identities-anonymous' ),
			get_search_query()
		);
	}

	if ( is_archive() ) {
		return get_the_archive_title();
	}

	return esc_html__( 'Research', 'two-identities-anonymous' );
}