我正在构建一个使用亚麻籽作为父级的子主题。该主题包括几个“内容样式”,可以使用主题>自定义程序进行选择。这些样式中的每一个在父functions.php文件中都有一个函数,该函数将CSS和wordpress内容呈现为一个。我想编辑这里的一些循环函数,特别是将the_excerpt更新为the_content,但我想以一种适合我的子主题的方式这样做。
由于这段代码位于父functions.php中,而且由于主题自定义器专门按名称调用它,所以我不能简单地添加一个新函数,也不能使用相同的函数名覆盖它。
我的最佳猜测是,在我的子functions.php中加载同名的新函数之前,我需要以某种方式从父function.php中删除这个函数,但我似乎不知道如何加载。
下面是将函数加载到模板中的模板文件中的代码:
<?php for ($i = 1; $i < 8; $i++)
if (get_theme_mod('flaxseedpro_fa_'.$i) != 'none') {
flaxseedpro_display_featured_area(
get_theme_mod('flaxseedpro_fa_'.$i),
get_theme_mod('flaxseedpro_fa_title'.$i),
get_theme_mod('flaxseedpro_fa_cat'.$i)
);
}
?>这里,$i变量是在主题>自定义程序屏幕中设置的值。此文件和代码可作为子主题的一部分轻松修改。
下面是来自父functions.php的两个代码片段,它们选择了适当的特色区号:
function flaxseedpro_display_featured_area($style, $fa_title, $cat) {
if (is_home()) :
switch ($style) {
case 'carousel':
flaxseedpro_carousel($fa_title, $cat);
break;
case 'style1':
flaxseedpro_featured_style1($fa_title, $cat);
break;
case 'style2':
flaxseedpro_featured_style2($fa_title, $cat);
break;
case 'style3':
flaxseedpro_featured_style3($fa_title, $cat);
break;
case 'style4':
flaxseedpro_featured_style4($fa_title, $cat);
break;
default:
break;
}
endif;
}这就产生了如下几种功能:
function flaxseedpro_featured_style2($fa_title, $cat) {
?>
<div class="featured-style1 featured-style2">
<?php if ('' !== $fa_title) : ?>
<h2 class="featured-section-title container">
<?php echo esc_html($fa_title); ?>
</h2>
<?php endif; ?>
<div class="featured-posts-inner container">
<?php
$args = array(
'posts_per_page' => 5,
'cat' => $cat,
);
$fa_posts = new WP_Query($args);
if ($fa_posts->have_posts()) :
$counter = 0;
while ($fa_posts->have_posts()) :
$fa_posts->the_post();
$counter++;
if (1 === $counter) {
?>
<div class="feat-col1 md-6 sm-6">
<div class="md-12 pr-0 pl-0">
<article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
<div class="item-container mb-3">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium-large', array('class' => 'featured-post-thumbnail-primary')); ?></a>
<?php else : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>"></a>
<?php endif; ?>
</div>
<div class="post-title-parent">
<a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<div class="post-author">
<?php esc_html_e('By', 'flaxseed-pro'); ?> <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>" title="<?php echo esc_attr(get_the_author()); ?>"><?php the_author(); ?></a> <i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?>
</div>
<div class="entry-excerpt body-font mb-3"><?php the_excerpt(); ?></div>
</div>
<a href="<?php the_permalink(); ?>" class="theme-button"><?php _e('Read More','flaxseed-pro') ?></a>
</article>
</div>
</div>
<?php
} else {
?>
<div class="feat-col2 md-6 sm-6">
<article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
<div class="md-4 xs-4">
<div class="item-container">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium', array('class' => 'featured-post-thumbnail-secondary')); ?></a>
<?php else : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>"></a>
<?php endif; ?>
</div>
</div>
<div class="md-8 xs-8">
<div class="post-title-parent">
<a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<br><small><?php esc_html_e('By', 'flaxseed-pro'); ?> <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>" class="url fn n" title="<?php echo esc_attr(get_the_author()); ?>"><?php the_author(); ?></a> <i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></small>
</div>
</div>
</article>
</div>
<?php
}
endwhile;
endif;
wp_reset_postdata();
?>
</div>
</div>
<?php
}这是我一直试图覆盖的代码,但无法作为子主题的一部分找到解决方案。我找到的所有答案似乎都需要一个钩子,从这段代码中我看不出来。
发布于 2021-02-05 19:01:22
这件事解决了。
我更改了模板文件(flaxseedpro_display_featured_area >> flaxseedpro_display_featured_area2)中调用的函数名,然后重新定义了开关函数,将'style3‘替换为新的’style5 5‘。然后用更新的CSS代码添加了新的'style5‘函数。
https://stackoverflow.com/questions/66038600
复制相似问题