Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twenty Fifteen - Custom 404 page for a custom post type
<?php
/**
* The template for displaying 404 pages (not found)
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php $post_type_var = get_query_var('post_type'); // Get the custom post type ?>
<?php if ( $post_type_var == 'projekt' ) : // Check if we come from the custom post type "projekt" ?>
<section class="error-404 not-found">
<header class="page-header">
<h1 class="page-title"><?php esc_html_e( 'Oops! That project can&rsquo;t be found.', 'twentyfifteen-child' ); ?></h1>
</header><!-- .page-header -->
<div class="page-content">
<p><?php esc_html_e( 'It looks like the project you were looking for could not be found at this location. Maybe try a search?', 'twentyfifteen-child' ); ?></p>
<?php get_search_form(); ?>
<h2><?php esc_html_e( 'Latest Projects', 'twentyfifteen-child' ); ?></h2>
<?php
//args
$args = array(
'post_type' => 'projekt',
'posts_per_page' => 3
);
// the query
$project_query = new WP_Query( $args ); ?>
<?php if ( $project_query->have_posts() ) : ?>
<ul class="latest-projects">
<?php while ( $project_query->have_posts() ) : $project_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.', 'twentyfifteen-child' ); ?></p>
<?php endif; ?>
</div><!-- .page-content -->
</section><!-- .error-404 -->
<?php else : // All other post types like post and page get the normal 404 content ?>
<section class="error-404 not-found">
<header class="page-header">
<h1 class="page-title"><?php esc_html_e( 'Oops! That page can&rsquo;t be found.', 'twentyfifteen-child' ); ?></h1>
</header><!-- .page-header -->
<div class="page-content">
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentyfifteen-child' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .page-content -->
</section><!-- .error-404 -->
<?php endif; ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
@juandl

This comment has been minimized.

Copy link

juandl commented Apr 3, 2018

What is the operation of this code? show a different text if it is a custom pos type? @matthiaspabst

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.