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

How to avoid modifying a template file by using child theme WordPress


In the below code, I need to add the line in bold text in a template file.
(Filename: /wp-content/themes/classipress/content-ad_listing.php)

Problem: next time the theme will be updated, this line will disappear.

Question: How to avoid modifying this template file and include this call properly in my child theme's function.php to avoid this problem?

NOTE: I have a child theme already confgured. (with functions.php, custom.css, ...)

* Listing loop content template.
* @package ClassiPress\Templates
* @author AppThemes
* @since ClassiPress 3.4
global $cp_options;

<div class="post-block-out <?php cp_display_style( 'featured' ); ?>">

<div class="post-block">
<em><strong><?php if (get_post_meta($post->ID, 'cp_ad_sold', true) == 'yes') echo '<span class="soldout-ribbon-list-'.ICL_LANGUAGE_CODE.'"></span>'; else echo ''; ?></strong></em>

<div class="post-left">

<?php if ( $cp_options->ad_images ) cp_ad_loop_thumbnail(); ?>


<div class="<?php cp_display_style( array( 'ad_images', 'ad_class' ) ); ?>">

<?php appthemes_before_post_title(); ?>

<h3><a href="<?php the_permalink(); ?>"><?php if ( mb_strlen( get_the_title() ) >= 75 ) echo mb_substr( get_the_title(), 0, 75 ) . '...'; else the_title(); ?></a></h3>

<div class="clr"></div>

<?php appthemes_after_post_title(); ?>

<div class="clr"></div>

<?php appthemes_before_post_content(); ?>

<p class="post-desc"><?php echo cp_get_content_preview( 160 ); ?></p>

<?php appthemes_after_post_content(); ?>

<div class="clr"></div>


<div class="clr"></div>

</div><!-- /post-block -->

</div><!-- /post-block-out -->

Answers (4)


Hariprasad Vijayan answers:


Create a child theme for your "classipress" theme. You can get details for how to create child theme here [[LINK href=""]][[/LINK]].

Copy content-ad_listing.php file from "classipress" theme to its child theme. And modify the section that you need from the file that you have in child theme folder.

<strong>Update 1: </strong>

If you already have child theme, copy content-ad_listing.php to your child theme and modify the area you want.

<strong>Update 2: </strong>

You really don't need to use any hooks for adding a template file to child theme. Wordpress will automatically load the file from child theme if you add it on child theme directory.

Let me know if you need help in this.


pinic comments:

Thank you it helped.


zebra webdesigns answers:

have you got the child theme already.

If not create the child theme and place the content-ad_listing.php file in there.

pinic comments:

Thank you it helped.


Reigel Gallarde answers:

steps in successfully doing this...

1. Create a folder same name as your theme's folder added by a word child...
sample, if your theme is classipress, create a folder 'classipress-child' on wp-content/themes folder.
2. next create style.css.
write at the top something like this

Theme Name: ClassiPress Child Theme
Template: ClassiPress
@import url("../classipress/style.css");

Take note of 'ClassiPress' here.. that should be the same exact word you can find on classipress/style.css file.

3. copy content-ad_listing.php on 'classipress-child' and do the edits you need.

4. Navigate on your WordPress>Appearance>Themes and find ClassiPress Child Theme and then activate it.


more about overriding template files [[LINK href=""]]here[[/LINK]].

Reigel Gallarde comments:

you can simply include a file <strong>of the same name</strong> in the <strong>child theme directory</strong>, and <em>it will override the equivalent file in the parent theme directory</em> when your site loads. For instance, if you want to change the PHP code for the site header, you can include a header.php in your child theme's directory, and that file will be used instead of the parent theme's header.php.

Reigel Gallarde comments:

in your case you have to add <strong>content-ad_listing.php</strong> in your child theme's directory, and that file will be used instead of the parent theme's <strong>content-ad_listing.php</strong>.

pinic comments:

Thank you Reigel you answer is helpful, but isn't there a more 'surgical' way of doing this? For instance using hooks (problem, I see no useable hook in this context)? I'm just concerned that if content-ad_listing.php gets changed, I will have to take the new updated file and re-introduce my change next time theme gets updated. However your solution will work and I will use it if there is no other solution.

Reigel Gallarde comments:

as I can see it, there's nothing we can do in between these lines...

<div class="post-block">
<div class="post-left">

so child theme is your last bet. I wouldn't be afraid if the parent theme get's updated. If the changes are big, I will just have to copy the file again and write that line I needed...

pinic comments:

Thank you. This way I know I'm doing things properly !

Reigel Gallarde comments:

You're welcome. ;)


Bard answers:

You will need to note what you have changed in the file(child theme), after you have updated the theme, check if the theme updated the file(if you have it in the child theme), if it does, then just update the part you have changed (check your note).

In the WP codex, Only style.css and functions.php are free to "add" code.