Adding page 'last updated' text into a page

I am creating an information website for a charity. At the bottom of each page of information I want to add an accordion which includes details about that page (see image)

At the moment I am manually typing the date and disclaimer text. I have two questions.

1 - Is there a code I can insert so that each time that page is edited/updated the date of the ‘last checked’ is automatically changed?

2 - With the disclaimer, is there any way I can create that as a sort of text element, so that if I change that element slightly its automatically reflected across other pages where that disclaimer element is placed?

Thanks, Ashley.

Hey Ashley,

Thanks for writing in! To resolve your issues, since the child theme is set up, please add the following code in your child theme’s functions.php file

function add_page_information_accordion_item(){ ?>
	
  <?php if ( is_page( 'About OCD' ) || '1393' == $post->post_parent ) : ?>

	<hr class="x-gap" style="margin: 40px 0 0 0;">

	<div id="pi-accordion" class="x-accordion"> 
		<div class="x-accordion-group" data-cs-collapse-group="">
			<div class="x-accordion-heading">
				<a class="x-accordion-toggle collapsed" data-cs-collapse-toggle="" data-cs-collapse-parent="#pi-accordion">Page Information</a>
			</div>
			<div class="x-accordion-body collapse" data-cs-collapse-content="">
				<div class="x-accordion-inner">
					<div class="x-container max width">
						<div class="x-column x-sm x-1-2" style=""><b>Last Checked:</b> <span style="color: #0a4d80;"><?php esc_html( get_the_modified_date() ); ?></span>. </div>
						<div class="x-column x-sm x-1-2 last" style=""><b>Next Review:</b> <span style="color: #0a4d80;">December 2019</span>. </div>
					</div>
					
					<hr class="x-gap" style="margin: 20px 0 0 0;">
					
					<p class="p-small"><b>Disclaimer:</b> This article is for information only and should not be used for the diagnosis or treatment of Obsessive-Compulsive Disorder or any other medical condition. OCD-UK have taken all reasonable care in compiling this information, but always recommend consulting a doctor or other suitably qualified health professional for diagnosis and treatment of Obsessive-Compulsive Disorder or any other medical condition.</p>

				</div>
			</div>
		</div>
	</div>

  <?php endif; ?>

<?php }
add_action('the_content', 'add_page_information_accordion_item');

With this code, the page information will be automatically added at the end of you pages. Having this way, you can easily edit the disclaimer text and the last updated date will auto change.

Hope this helps.

Thank you for the response. It sounds like this will be the answer I need, but the code results in a blank page with no content showing? I am looking at the code but I can’t see the error preventing the content showing.

Hello There,

There was a code error. Please have it updated and use this instead:

// Add Page Information
// =============================================================================
function add_page_information_accordion_item($content){
  
  if ( is_page( 'About OCD' ) || '1393' == $post->post_parent ) : 

    $content .= '<hr class="x-gap" style="margin: 40px 0 0 0;">'

             .= '<div id="pi-accordion" class="x-accordion"> '
             .= ' <div class="x-accordion-group" data-cs-collapse-group="">'
             .= '  <div class="x-accordion-heading">'
             .= '    <a class="x-accordion-toggle collapsed" data-cs-collapse-toggle="" data-cs-collapse-parent="#pi-accordion">Page Information</a>'
             .= '  </div>'
             .= '   <div class="x-accordion-body collapse" data-cs-collapse-content="">'
             .= '     <div class="x-accordion-inner">'
             .= '       <div class="x-container max width">'
             .= '         <div class="x-column x-sm x-1-2" style=""><b>Last Checked:</b> <span style="color: #0a4d80;">' . esc_html( get_the_modified_date() ) . '</span>. </div>'
             .= '         <div class="x-column x-sm x-1-2 last" style=""><b>Next Review:</b> <span style="color: #0a4d80;">December 2019</span>. </div>
                    </div>'
                    
             .= '       <hr class="x-gap" style="margin: 20px 0 0 0;">'
                    
             .= '       <p class="p-small"><b>Disclaimer:</b> This article is for information only and should not be used for the diagnosis or treatment of Obsessive-Compulsive Disorder or any other medical condition. OCD-UK have taken all reasonable care in compiling this information, but always recommend consulting a doctor or other suitably qualified health professional for diagnosis and treatment of Obsessive-Compulsive Disorder or any other medical condition.</p>'

             .= '     </div>
                </div>
              </div>
            </div>'

    return $content;

  endif;

  return $content;

}
add_filter('the_content', 'add_page_information_accordion_item'); 
// =============================================================================

Hope this helps. Please let us know how it goes.

I appreciate the help. I am afraid that code is causing a conflict with the pages not showing when using that code.

Hey There,

I have re structured the code and here it is:

// Add Page Information
// =============================================================================
function add_page_information_accordion_item($content){
  
  if ( is_page( 'About OCD' ) || '1393' == $post->post_parent ) : 

    $content .= '<hr class="x-gap" style="margin: 40px 0 0 0;">';
    $content .= '<div id="pi-accordion" class="x-accordion"> ';
    $content .= ' <div class="x-accordion-group" data-cs-collapse-group="">';
    $content .= '  <div class="x-accordion-heading">';
    $content .= '    <a class="x-accordion-toggle collapsed" data-cs-collapse-toggle="" data-cs-collapse-parent="#pi-accordion">Page Information</a>';
    $content .= '  </div>';
    $content .= '   <div class="x-accordion-body collapse" data-cs-collapse-content="">';
    $content .= '     <div class="x-accordion-inner">';
    $content .= '       <div class="x-container max width">';
    $content .= '         <div class="x-column x-sm x-1-2" style=""><b>Last Checked:</b> <span style="color: #0a4d80;">' . esc_html( get_the_modified_date() ) . '</span>. </div>';
    $content .= '         <div class="x-column x-sm x-1-2 last" style=""><b>Next Review:</b> <span style="color: #0a4d80;">December 2019</span>. </div>
                    </div>';
    $content .= '       <hr class="x-gap" style="margin: 20px 0 0 0;">';
    $content .= '       <p class="p-small"><b>Disclaimer:</b> This article is for information only and should not be used for the diagnosis or treatment of Obsessive-Compulsive Disorder or any other medical condition. OCD-UK have taken all reasonable care in compiling this information, but always recommend consulting a doctor or other suitably qualified health professional for diagnosis and treatment of Obsessive-Compulsive Disorder or any other medical condition.</p>';
    $content .= '     </div>
                </div>
              </div>
            </div>';

    return $content;

  endif;

  return $content;

}
add_filter('the_content', 'add_page_information_accordion_item', 5); 
// =============================================================================

Please understand that since this is a custom code, it may not work in the future if in case there are modifications to the accordion items or any element within this code.

Hope this helps.

Hi Thanks again for helping with this. The page does display now, but the added accordion item code doesn’t appear at the bottom.

Hi there,

How did you add it? I don’t see it in your child theme’s functions.php. And please change this line

if ( is_page( 'About OCD' ) || '1393' == $post->post_parent ) :

to this

if ( is_page( 1393 ) || 1393 == $post->post_parent ) :

Thanks!

Hi,

I played around with the code, in the end removing the 1393 reference seemed to do the trick.

if ( is_page( ‘About OCD’ ) == $post->post_parent ) :

Hello There,

We are just glad that you have figured it out a way to correct the said issue.
Thanks for letting us know!

Best Regards.

Thank you again for all your help on this. I wanted slightly more control over the links I include in each accordion so in the end I stumbled on a different method to achieve the automation aspect by using shortcodes in the function.php. I will post below in case anybody else is trying to achieve the same.

The part I am struggling with which I hope you can help is the next page review date. I want it to be automated to two years after the last modified date. I can’t seem to get the Y to be +2. I am not sure what the code is, are you able to advise?

// Page NEXT Review date Shortcode for the content pages.
// =============================================================================
// 
function nextreviewed_func( $atts ){
    return '<b>Next Review:</b> <span style="color: #0a4d80;">' . esc_html( get_the_modified_date( 'M Y' ) ) . '</span> ';
   }
add_shortcode( 'nextreviewed', 'nextreviewed_func' );

For anyone interested in the shortcode for the last reviewed date just add [lastreviewed] into your page and the following into your functions.php

// Page Last Reviewed Shortcode for the content pages.
// =============================================================================
// 
function lastreviewed_func( $atts ){
    return '<b>Last Checked:</b> <span style="color: #0a4d80;">' . esc_html( get_the_modified_date() ) . '</span> ';
   }
add_shortcode( 'lastreviewed', 'lastreviewed_func' );
//  Disclaimer Shortcode for the content pages.
// =============================================================================
// 

function disclaimer_func( $atts ){
	return "<b>Disclaimer:</b> <<< INSERT DISCLAIMER TEXT HERE >>>.";
	}
add_shortcode( 'disclaimer', 'disclaimer_func' );

Hey There,

To calculate the date, please check out this link:

Hope this helps.

It did thank you… It took me a few hours but I got there in the end.

This displays a date of the last page modified date plus 2.5 years for the next revision date.

function nextreviewed_func( $atts ){
 $today = esc_html( get_the_modified_date('jS') ) ;
    $thisMonth = esc_html( get_the_modified_date('n') ) ;
    $thisYear = esc_html( get_the_modified_date('Y') ) ;
    return '<b>Next Review Due:</b> <span style="color: #0a4d80;">' . date("jS F Y", mktime(0,0,0, $thisMonth+6, $today, $thisYear+2)). '</span>' ;
   }
add_shortcode( 'nextreviewed', 'nextreviewed_func' );

You’re welcome!
Thanks for letting us know that it has worked for you.

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