php - 如何更改 wordpress 中特色图片的图片大小?

标签 php html css wordpress function

我以为我已经弄清楚了,当然,代码不起作用。不确定我做错了什么或如何解决它。我只想能够在我的 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&raquo;</a>
        </p>

        <?php } else {

            the_content();

        } ?>

-->

        <P>
            <?php echo get_the_excerpt(); ?> 
            <a href="<?php the_permalink(); ?>">Read more&raquo;</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/

相关文章:

html - Bootstrap .center-block 没有按预期工作

php - CSS 仅在短暂但明显的延迟后生效

php - 模板化多语言网站的最有效方式

php - 特定条件下的 MySQL COUNT() 总帖子数?

php - 如何使用我的服务器作为代理通过 PHP 下载文件?

php - 使用 CodeIgniter 将上传图片字段添加到注册表单

jquery - Fullcalendar 更改日历的整个背景颜色

html - 如何使用框架创建有效的 HTML5?

html - 框大小:边框框;不影响文本区域

css - Bootstrap 站点移动 View 不使用完整视口(viewport)