在wordpress中获取特定的图像大小

时间:2014-05-06 12:55:00

标签: php wordpress image

我一直在使用以下代码来获取子页面的所有精选图像,然后在旋转木马滑块中显示,但我想要特定尺寸= 231 * 130和class = img-responsive。 但我无法做到这一点

<?php
$mypages = get_pages( array( 'child_of' => 67, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );

foreach( $mypages as $page ) {    
    $content = $page->post_content;
    if ( ! $content ) // Check for empty page
        continue; ?>                                     

    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
        <div class="thumbnail">
            <a href="<?php echo get_page_link( $page->ID ); ?>"> 
                <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID, array( 231, 130 ), array( 'class' => 'img-responsive img-shadow' ) ) ); ?>
                <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>">
            </a>    

            <div class="caption clearfix">
                <h4><?php echo $page->post_title; ?></h4>
                <a href="<?php echo get_page_link( $page->ID ); ?>">view all</a>
            </div>
        </div>
    </div>
<?php } ?>

请帮我弄清楚我做错了什么

3 个答案:

答案 0 :(得分:1)

使用wp_get_attachment_imageCODEX

在你的情况下:

<?php
$mypages = get_pages( array( 'child_of' => 67, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );

foreach( $mypages as $page ) {    
    $content = $page->post_content;
    if ( ! $content ) // Check for empty page
        continue; ?>                                     

    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
        <div class="thumbnail">
            <a href="<?php echo get_page_link( $page->ID ); ?>"> 
                <?php $image = wp_get_attachment_image( get_post_thumbnail_id($post->ID), 'child-page','', array('class'    => "img-responsive img-shadow", 'alt'   => get_the_title()))  ?>
                <?php echo $image; ?> 
            </a>    

            <div class="caption clearfix">
                <h4><?php echo $page->post_title; ?></h4>
                <a href="<?php echo get_page_link( $page->ID ); ?>">view all</a>
            </div>
        </div>
    </div>
<?php } ?>

和你的functions.php

add_image_size( 'child-page', 231, 130, true );

答案 1 :(得分:0)

你在这里混淆了你的论点:

<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ,array(231,130),array('class'=>'img-responsive img-shadow') ));?>

get_post_thumbnail_id只占用网页ID。但实际上,当你应该做的时候,你会嵌套两个函数。使用此:

<?php get_the_post_thumbnail($page->ID, array(231,130), array('class' => 'img-responsive img-shadow')); ?>

这也会回显结果,因此您可以删除第二行。该职能Here's the Codex entry

答案 2 :(得分:0)

试试这可能会对你有帮助: -

<a href="<?php echo get_page_link( $page->ID ); ?>">
    <?php echo get_the_post_thumbnail($page->ID, array(231,130), array('class' => 'img-responsive img-shadow'));?>
</a>