简体   繁体   中英

How to edit a variable value of parent theme in child theme in wordpress

I want to change a variable value of the theme Writee (Wordpress). There is variable in "content-post-header.php' called "$date_format" which its value is hardcoded as "l, F j, Y" (this variable is not in a function). It is the content:

<?php 
/****************************************/
## Blog post header content.
/***************************************/

global $post;

$date_format = 'l, F j, Y';

?>
<div class="entry-header">
    <div class="entry-meta">
        <span class="entry-cat"><?php the_category(' ')?></span>
    </div>
    <?php 
    if (! is_single()) :
        the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '">', '</a></h2>' );

     else: 
        the_title( '<h1 class="entry-title">', '</h1>' );

    endif; 

   ?>
    <div class="entry-meta">
        <span class="entry-author"><?php echo __('By', 'writee'); ?> <?php the_author_posts_link(); ?> </span>
        <span class="entry-date"><?php echo __('on', 'writee'); ?> <a href="<?php echo get_month_link(get_the_time('Y'), get_the_time('m')); ?>"><?php the_time($date_format); ?></a></span>
    </div>
</div>

I want to override this variable and change its value to "l, j F, Y". I have a child theme called Writee-child and there is a function.php file there that as I've read in forums, I must override the variable there, but by doing that the value doesn't change. for example, I tried the following code:

function change_date_format(){ 
  global $date_format;
  $date_format = 'l, j F, Y';
}
add_action( 'after_setup_theme', 'change_date_format' );

of course I have no idea what the code does but it was the most relevant thing I found by googling. So, how may I change this variable value?

You need to create a file in your child theme with the same name as the file in the parent theme. And put that new file in the same folder structure as file lies in the parent theme. And in the new file you can change the code.

<?php 
/****************************************/
## Blog post header content.
/***************************************/

global $post;

$date_format = 'what you want';

?>
<div class="entry-header">
    <div class="entry-meta">
        <span class="entry-cat"><?php the_category(' ')?></span>
    </div>
    <?php 
    if (! is_single()) :
        the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '">', '</a></h2>' );

     else: 
        the_title( '<h1 class="entry-title">', '</h1>' );

    endif; 

   ?>
    <div class="entry-meta">
        <span class="entry-author"><?php echo __('By', 'writee'); ?> <?php the_author_posts_link(); ?> </span>
        <span class="entry-date"><?php echo __('on', 'writee'); ?> <a href="<?php echo get_month_link(get_the_time('Y'), get_the_time('m')); ?>"><?php the_time($date_format); ?></a></span>
    </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM