AnsPress_Theme::anspress_basepage_template( string $template )

Description #

Check if anspress.php file exists in theme. If exists then load this template for AnsPress.

Parameters #

  • $template
    string (Required) Template.

Changelog #

VersionDescription
4.1.1Load single question template if exists.
4.1.0Give priority to page templates and then anspress.php and lastly fallback to page.php.
3.0.0Introduced.

Source #

File: includes/class-theme.php

	public static function anspress_basepage_template( $template ) {
		if ( is_anspress() ) {
			$templates = array( 'anspress.php', 'page.php', 'singular.php', 'index.php' );

			if ( is_page() ) {
				$_post = get_queried_object();

				array_unshift( $templates, 'page-' . $_post->ID . '.php' );
				array_unshift( $templates, 'page-' . $_post->post_name . '.php' );

				$page_template = get_post_meta( $_post->ID, '_wp_page_template', true );

				if ( ! empty( $page_template ) && 'default' !== $page_template ) {
					array_unshift( $templates, $page_template );
				}
			} elseif ( is_single() ) {
				$_post = get_queried_object();

				array_unshift( $templates, 'single-' . $_post->ID . '.php' );
				array_unshift( $templates, 'single-' . $_post->post_name . '.php' );
				array_unshift( $templates, 'single-' . $_post->post_type . '.php' );
			} elseif ( is_tax() ) {
				$_term     = get_queried_object();
				$term_type = str_replace( 'question_', '', $_term->taxonomy );
				array_unshift( $templates, 'anspress-' . $term_type . '.php' );
			}

			$new_template = locate_template( $templates );

			if ( '' !== $new_template ) {
				return $new_template;
			}
		}

		return $template;
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment