1

我想更改主题 Writee (Wordpress) 的变量值。“content-post-header.php”中有一个名为“$date_format”的变量,其值被硬编码为“l,F j,Y”(此变量不在函数中)。它是内容:

<?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>

我想覆盖这个变量并将其值更改为“l,j F,Y”。我有一个名为 Writee-child 的子主题,并且那里有一个 function.php 文件,正如我在论坛中阅读的那样,我必须覆盖那里的变量,但是这样做的值不会改变。例如,我尝试了以下代码:

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

当然我不知道代码的作用,但这是我通过谷歌搜索找到的最相关的东西。那么,我怎样才能改变这个变量值呢?

4

1 回答 1

0

您需要在子主题中创建一个与父主题中的文件同名的文件。并将该新文件放在与父主题中的文件相同的文件夹结构中。在新文件中,您可以更改代码。

<?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>
于 2017-06-17T09:44:05.997 回答