I'm developing a site locally using WordPress and have used the plugin Advanced Custom Fields to create a group of fields that will display when post category is set to 'Front Page Events'. This category has a slug of fpe
.
I put the code below in my child theme's functions.php
to create a path to a single template folder:
/*
* Creates a path to single template folder for Front Page Events to be viewed as single posts
*/
define('SINGLE_PATH', TEMPLATEPATH . '/single');
/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :
if(file_exists('SINGLE_PATH' . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
elseif(file_exists('SINGLE_PATH' . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
endforeach;
}
I then manually created a folder in my theme (a child of twentyseventeen) called 'single'.
I copied single.php
from the parent theme and pasted it into twentyseventeen-child/single
. I then renamed this file to single-cat-fpe.php
. I then added some code to get the custom fields from the post:
<?php
/**
* Template Name: Front Page Events
* Template Post Type: post, page
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since Twenty Seventeen 1.0
* @version 1.0
*/
get_header(); ?>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the Loop.
while ( have_posts() ) :
the_post(); ?>
<h1>TEST</h1>
<?php
get_template_part( 'template-parts/post/content', get_post_format() ); ?>
<p><?php echo the_field('event_title'); ?></p>
<p><?php echo the_field('event_description'); ?></p>
<p><?php echo the_field('event_link'); ?></p>
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
the_post_navigation(
array(
'prev_text' => '<span class="screen-reader-text">' . __( 'Previous Post', 'twentyseventeen' ) . '</span><span aria-hidden="true" class="nav-subtitle">' . __( 'Previous', 'twentyseventeen' ) . '</span> <span class="nav-title"><span class="nav-title-icon-wrapper">' . twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '</span>%title</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next Post', 'twentyseventeen' ) . '</span><span aria-hidden="true" class="nav-subtitle">' . __( 'Next', 'twentyseventeen' ) . '</span> <span class="nav-title">%title<span class="nav-title-icon-wrapper">' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ) . '</span></span>',
)
);
endwhile; // End the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div><!-- .wrap -->
<?php
get_footer();
I am able to get the data from the custom fields to show up in a slider on the front page of the site. However, the custom fields are not showing up when I just try to view the single post. (Nor is the test line with the h1 tags.) I am also not able to see the template I created in the post editor. Can anyone help me figure out how I can correctly display the custom fields when viewing a single post in this category?
Actually it looks like this might have been a theme-related issue. Because I'm using a child of twenty seventeen, it was necessary to put the single post file in template-parts/post/content-php. I've solved it now.