我以为我已经弄清楚了,当然,代码不起作用。不确定我做错了什么或如何解决它。我只想能够在我的 functions.php 文件中定义图像大小,并在需要时从我的 index.php 或 single.php 中调用它们。 (请原谅代码中的所有评论。我正在学习一个尝试学习 wordpress 的教程,并且该教程没有涵盖“如果它不起作用怎么办”的问题)所以目前,我正在玩的图像是 1360x582,它会根据我的 css 调整大小,但不会根据我应用它们时的功能调整大小。任何帮助都会很棒!
我的 index.php 看起来像这样:
<?php
get_header();
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-info"><?php the_time('F jS, Y'); ?> | by <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a> | Posted in
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories){
foreach ($categories as $category) {
$output .= '<a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a>' . $separator;
}
echo trim($output, $separator); //trim() removes comma from last item//
}
?></p>
<?php the_post_thumbnail('small_thumbnail'); ?> <!--featured image-->
<!-- if you want an excerpt to be displayed only if one is manually entered, otherwise show full post:
<?php if ($post->post_excerpt) { ?>
<P>
<?php echo get_the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more»</a>
</p>
<?php } else {
the_content();
} ?>
-->
<P>
<?php echo get_the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more»</a>
</p>
<!-- <?php the_content('Read More...'); ?> -->
</article>
<?php endwhile;
else :
echo '<p>No content found</p>';
endif;
get_footer();
?>
函数.php:
//Theme Setup
function Theme_setup() {
//Navigation Menus
register_nav_menus(array(
'primary' => __( 'Primary Menu'),
'footer' => __( 'Footer Menu'),
));
//Add Feature Image Support
add_theme_support('post-thumbnails');
add_image_size('small-thumbnail', 180, 120, true); //width, height, hard (true) or soft crop
add_image_size('banner-image', 920, 210, true);
}
add_action('after_setup_theme', 'Theme_setup');
我的 single.php 现在看起来和我的 index.php 几乎一样,除了它在特色图片上使用“横幅图像”而不是“小缩略图”
我应用于图像的 css 是这样的:
/* Image Styles */
img {
max-width: 100%;
height: auto;
}
最佳答案
add_image_size('small-thumbnail', 180, 120, true);
更改 180 和 120 的值(宽度和高度),您就可以开始了。
此资源还可以帮助您:
https://wordpress.org/support/topic/set-featured-image-size
您还可以使用名为 Simple Image Sizes 的插件控制这些值
关于php - 如何更改 wordpress 中特色图片的图片大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33869505/