Display custom field on post index pages

Hi, I would like to display a field created in Advanced Custom Field on my blog index pages. I have created a child theme with a file _content.php that modifies the index page to show the full post (as recommended here: https://theme.co/apex/forum/t/full-post-content-on-index-not-working/4786/4). It works great. I have also modified the _content-post-header.php file in the child theme to add the custom field below the title line on single posts: <p><?php the_field('now_showing_times'); ?></p>. This works perfectly to add the custom field to the single posts. But I would like to add this custom field to the index page. Can you let me know how to do this?

I forgot the site: https://staging1.upstatefilms.org

Here’s a typical index page showing the full post: https://staging1.upstatefilms.org/category/now-showing

Here’s a single post page with the custom field for the showtimes: https://staging1.upstatefilms.org/judy

Hello Steve,

Thank you for the very detailed information.

To make sure that your field will display in both the single and blog index page, your _content.php files should be like this:

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT.PHP
// -----------------------------------------------------------------------------
// Display of the_excerpt() or the_content() for various entries.
// =============================================================================

$stack                     = x_get_stack();
$is_full_post_content_blog = is_home() && x_get_option( 'x_blog_enable_full_post_content' ) == '1';

?>

<p><?php the_field('now_showing_times'); ?></p>

<?php

if ( is_singular() || $is_full_post_content_blog ) :
  x_get_view( 'global', '_content', 'the-content' );
  if ( $stack == 'renew' ) :
    x_get_view( 'renew', '_content', 'post-footer' );
  endif;
else :
  x_get_view( 'global', '_content', 'the-excerpt' );
endif;

?>

Or if you want to display the field just within the content area then, please follow these steps below:
1] Using Notepad or TextEdit or Sublime Text or any text editor, please create a new file in your local machine.
2] Insert the following code into that new file

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT-THE-CONTENT.PHP
// -----------------------------------------------------------------------------
// Display of the_content() for various entries.
// =============================================================================

?>

<?php do_action( 'x_before_the_content_begin' ); ?>

<div class="entry-content content">

<?php do_action( 'x_after_the_content_begin' ); ?>

  <p><?php the_field('now_showing_times'); ?></p>

  <?php the_content(); ?>
  <?php x_link_pages(); ?>

<?php do_action( 'x_before_the_content_end' ); ?>

</div>

<?php do_action( 'x_after_the_content_end' ); ?>

3] Save the file named as _content-the-content.php
4] Upload this file to your server in the child theme’s folder /wp-content/themes/x-child/framework/views/global/

Hope this helps. Kindly let us know.

Hi, I tried the first code and it succeeded in showing the tables on the index pages, but it’s back to showing the excerpt rather than the full post content. Can you help again?

Also, on the single post pages, the table appears twice because I modified the _content-post-header.php to show the table field. Is it better to show it in the header or content?

https://staging1.upstatefilms.org/judy

Hey Steve,

Follow the instruction in the other thread again. https://theme.co/apex/forum/t/full-post-content-on-index-not-working/4786/4. Next, just add the custom field line.

Showing the custom field in the post header or post content depends on your design. If you wish to add in in the content, delete the modification in _content-post-header.php or delete that file altogether and modify the global content template instead. The x_get_view function will lead you to the directory and file you need.

That means framework/views/global/_content-the-content.php.

Hope that helps.

Worked perfectly. Thanks very much! Your support is the best.

You’re most welcome!

Now I hope I am not asking too much, but I have another request. There are actually two fields on some posts, one for now-playing movies and one for starts-friday movies. I’ve modified the content.php file to display both of them, and they are both showing on the single post page (https://staging1.upstatefilms.org/judy) and on the category index pages (https://staging1.upstatefilms.org/category/now-showing). I do not want both fields to appear at the same time, so would it be possible to do the following?

On single post pages, show only the table in custom field ‘now_showing_times”

On the index page for category “now-showing”, show only the table in custom field "now_showing_times”
(or hide field starts_friday_times)

On index pages for the category “starts-friday-rhinebeck”, show only the table in custom field “starts_friday_times” (or hide field now_showing_times)

I have gotten this to work using Content Views (where one can select which custom field to display in each case), but I’d prefer to do it without a plugin.

Thanks again for your help.

Just to clarify, I’d like the category index page to show the same table as the Content Views version:

Here’s the now-showing page in Content Views: https://staging1.upstatefilms.org/now-playing-in-rhinebeck
Here’s the category index page: https://staging1.upstatefilms.org/category/now-showing

Here’s the starts-friday page in Content Views: https://staging1.upstatefilms.org/coming-friday-rhinebeck
Here’s the category index page:https://staging1.upstatefilms.org/category/starts-friday-rhinebeck

Hello Steve,

You will need to modify the code in your child theme’s template override where the line <p><?php the_field('now_showing_times'); ?></p> is added. Make use of the WordPress Conditional Tags to display or hide the field. For example,

To display on single posts only, use this:

<?php if ( is_single() ) : ?>
   <p><?php the_field('now_showing_times'); ?></p>
<?php endif; ?>

To display on the index and with a category “now showing”:

<?php if ( is_home() && in_category('now showing') ) : ?>
   <p><?php the_field('now_showing_times'); ?></p>
<?php endif; ?>

Please note that custom coding is outside the scope of our support. Issues that might arise from the use of custom code and further enhancements should be directed to a third party developer.

Hope this helps.

Thank you for going the extra mile on this. The single post is working, but no tables are showing on the index page. Can you just check what’s wrong here?

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT.PHP
// -----------------------------------------------------------------------------
// Display of the_excerpt() or the_content() for various entries.
// =============================================================================

$stack                     = x_get_stack();
$is_full_post_content_blog = ( is_home() || is_category()  ) && x_get_option( 'x_blog_enable_full_post_content' ) == '1';

?>
<?php if ( is_single() ) : ?>
   <p><?php the_field('now_showing_times'); ?></p>
<?php endif; ?>

<?php if ( is_home() && in_category('now-showing') ) : ?>
   <p><?php the_field('now_showing_times'); ?></p>
<?php endif; ?>

<?php if ( is_home() && in_category('starts-friday-rhinebeck') ) : ?>
   <p><?php the_field('starts_friday_times'); ?></p>
<?php endif; ?>

<?php

if ( is_singular() || $is_full_post_content_blog ) :
  x_get_view( 'global', '_content', 'the-content' );
  if ( $stack == 'renew' ) :
    x_get_view( 'renew', '_content', 'post-footer' );
  endif;
else :
  x_get_view( 'global', '_content', 'the-excerpt' );
endif;

?>

Hello Steve,

Can you please update the code using this:

<?php if ( is_home() && has_term('now-showing') ) : ?>
   <p><?php the_field('now_showing_times'); ?></p>
<?php endif; ?>

<?php if ( is_home() && has_term('starts-friday-rhinebeck') ) : ?>
   <p><?php the_field('starts_friday_times'); ?></p>
<?php endif; ?>

Hope this helps.

I’m sorry but this is still not working. It’s showing full content on the category page but not the custom field.

Hi Steve,

Are you referring to the actual archive page instead of the home page? You could try replacing is_home() with is_archive(). It’s because the category page is an archive page and not a home page.

Thanks!

Thanks again for all your help. Got it to work with the following:

<?php if ( is_category('now-showing') ) : ?>

<?php the_field('now_showing_times'); ?>

<?php endif; ?>

Thanks for your great support and patience.

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.