功能图像无法在wordpress中工作

时间:2014-09-21 16:28:47

标签: wordpress wordpress-theming

我正在开发一个wordpress主题。但我可以为自定义帖子添加功能图像。 起初我在functions.php中创建了一个自定义帖子

<?php 
function rithemes_post_type() {
        register_post_type( 'portfolio_items',
            array(
                'labels' => array(
                        'name' => __( 'Portfolio-items' ),
                        'singular_name' => __( 'Portfolio-item' ),
                        'add_new' => __( 'Add New ' ),
                        'add_new_item' => __( 'Add New Portfolio-item' ),
                        'edit_item' => __( 'Edit Portfolio-item' ),
                        'new_item' => __( 'New Portfolio-item' ),
                        'view_item' => __( 'View Portfolio-item' ),
                        'not_found' => __( 'Sorry, we couldn\'t find the Slide you are looking for.' )
                ),
        'public' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'menu_position' => null,
        'has_archive' => true,
        'hierarchical' => true,
        'capability_type' => 'post',
        'rewrite' => array( 'slug' => 'Portfolio-items' ),
        'supports' => array( 'title', 'editor', 'custom-fields','thumbnail' )
        )
    ); 

add_action( 'init', 'rithemes_post_type' );

&GT;

然后我在functions.php

中编写以下代码
    add_theme_support('post-thumbnails', array( 'portfolio_pretty','portfolio_items','portfolio_single' ) );
add_image_size( 'blog_post_single', 615, 390, true );
add_image_size( 'portfolio_pretty', 610, 300, true ); 
add_image_size( 'portfolio_single', 440, 275, true );

然后我在index.php中编码如下

<?php
        global $post;
        $args=array( 'posts_per_page'=> 6, 'post_type'=>'portfolio_items');
        $myposts=get_posts($args);
        foreach($myposts as $post): setup_postdata($post);?>
            <div class="three columns category trains">
            <h5><?php the_title(); ?></h5>
            <?php $songkhipto=get_post_meta($post->ID, 'songkhipto', true);  ?>
        <p>
             <?php echo $songkhipto; ?>
        </p>
        <div class="portofoliothumb">
            <div class="portofoliothumboverlay fouroverlay">
                <div class="viewgallery fourgallery">
                    <a data-gal="prettyPhoto[gallery]" href="<?php the_post_thumbnail('portfolio_pretty'); ?><img src="<?php echo get_template_directory_uri(); ?>/images/playgal.png" class="left galleryicon" alt=""> Gallery</a>
                </div>
                <div class="inner fourdetail">
                    <a class="projectdetail" href="<?php the_permalink(); ?>">+ Project Details</a>
                </div>
            </div>
            <img src="<?php the_post_thumbnail('portfolio_single'); ?>" class="fourimage" alt=""/>
        </div>
        </div>
        <?php endforeach; ?>

但未显示特征图像。这只显示图像名称。我需要帮助

3 个答案:

答案 0 :(得分:0)

你混淆了the_post_thumbnail和get_the_post_thumbnail。

你想:

<img src="<?= get_the_post_thumbnail(get_the_ID(), 'portfolio_single'); ?>" class="fourimage" alt=""/>

答案 1 :(得分:0)

the_post_thumbnail返回图片,而不是图片的链接。你假设你有类似的东西:

http://example.com/wp-content/uploads/2014/09/featured-image.png

但事实上你得到:

<img src="http://example.com/wp-content/uploads/2014/09/featured-image.png">

答案 2 :(得分:0)

您实际上并不需要img标记。

直接推杆 <?php the_post_thumbnail('portifolio_single'); ?>应该这样做。