Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I need to Add a function to child theme. WordPress

  • SOLVED

I would like to add a "post reading time" to the top of all blog posts (above the post title). I have the plugin and code necessary. All that needs to be added is:

<?php post_read_time(); ?>

I added it to the Parent theme to see if it works, and it does. Here's a snippet of where I added it in 'single.php' file:


<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="preview blog-main-preview">
<?php post_read_time(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>

<?php


HOWEVER, that was just a quick test. I removed the post_read_time code and tried to add it to the functions.php file in the child theme, but it keeps breaking the website.

I need someone to provide the proper syntax to 1.) add this via my child theme and 2.) only display on posts in the marketing category (slug='marketing' ID=7).

Answers (3)

2015-03-17

Navjot Singh answers:

Well you can make a copy of your parent theme's single.php with the reading time code in it and copy it to child theme's directory. It will override the original file.


markh comments:

That was a quick response Navjot! Too, quick. :-)

I noticed I left out an important part. Sorry, I just updated the question as I left out the category requirement.


Navjot Singh comments:

In the same single.php in your child theme add this code instead of the reading time code

<?php
if(in_category('marketing')) {

post_read_time();

}?>


markh comments:

Thanks that worked great! I've voted for you to receive the award. Cheers.

2015-03-17

Francisco Javier Carazo Gil answers:

Navjot answer is ok, you can:
1) Include this function in your functions.php in your child theme
2) copy and paste in the child theme your single.php
3) modify it in the child theme
4) activate child theme

Do not forget to enqueue correctly styles when creating a child theme: http://codex.wordpress.org/Child_Themes

2015-03-17

Jeremy answers:

Copy the single.php with the added code to your child theme directory.


markh comments:

Jeremy, that was a quick response! Too, quick. :-)

I noticed I left out an important part. Sorry, I just updated the question as I left out the category requirement.