在WordPress存档页

时间:2018-01-31 23:00:41

标签: php wordpress custom-post-type

是否可以调用次要图像而不是“特色”图像。存档页面上的图像?我已创建了次要图片类型,并且我希望专门在自定义帖子类型上使用该图片,而不是默认的特色图片。

次要图像代码:

/** Secondary Image **/
add_action( 'add_meta_boxes', 'listing_image_add_metabox' );

function listing_image_add_metabox () {
    add_meta_box( 'listingimagediv', __( 'Listing Image', 'text-domain' ), 'listing_image_metabox', 'trainer', 'side', 'low');
}

function listing_image_metabox ( $post ) {
    global $content_width, $_wp_additional_image_sizes;

    $image_id = get_post_meta( $post->ID, '_listing_image_id', true );

    $old_content_width = $content_width;
    $content_width = 254;

    if ( $image_id && get_post( $image_id ) ) {

        if ( ! isset( $_wp_additional_image_sizes['post-thumbnail'] ) ) {
            $thumbnail_html = wp_get_attachment_image( $image_id, array( $content_width, $content_width ) );
        } else {
            $thumbnail_html = wp_get_attachment_image( $image_id, 'post-thumbnail' );
        }

        if ( ! empty( $thumbnail_html ) ) {
            $content = $thumbnail_html;
            $content .= '<p class="hide-if-no-js"><a href="javascript:;" id="remove_listing_image_button" >' . esc_html__( 'Remove listing image', 'text-domain' ) . '</a></p>';
            $content .= '<input type="hidden" id="upload_listing_image" name="_listing_cover_image" value="' . esc_attr( $image_id ) . '" />';
        }

        $content_width = $old_content_width;
    } else {

        $content = '<img src="" style="width:' . esc_attr( $content_width ) . 'px;height:auto;border:0;display:none;" />';
        $content .= '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set listing image', 'text-domain' ) . '" href="javascript:;" id="upload_listing_image_button" id="set-listing-image" data-uploader_title="' . esc_attr__( 'Choose an image', 'text-domain' ) . '" data-uploader_button_text="' . esc_attr__( 'Set listing image', 'text-domain' ) . '">' . esc_html__( 'Set listing image', 'text-domain' ) . '</a></p>';
        $content .= '<input type="hidden" id="upload_listing_image" name="_listing_cover_image" value="" />';

    }

    echo $content;
}

add_action( 'save_post', 'listing_image_save', 10, 1 );

function listing_image_save ( $post_id ) {
    if( isset( $_POST['_listing_cover_image'] ) ) {
        $image_id = (int) $_POST['_listing_cover_image'];
        update_post_meta( $post_id, '_listing_image_id', $image_id );
    }
}

以下是我目前在存档页面上使用的代码:

<div class="grids <?php echo $posts_layout . ' ' . $columns_num; ?> entries">
    <?php
    while ( have_posts() ) : the_post();

        get_template_part( 'content', 'post' );

    endwhile;
    ?>
</div>

来自后期操作页控制格式的代码:

function content_post_item_image() {

    // Start image buffer
    ob_start();
?>

        <?php // Check if an image exists.
        if ( has_post_thumbnail() || first_post_image() ) : ?>
        <div class="entry-image">
            <div class="entry-image-inner">

                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php
                    /**
                    * Different image size based on layout selection for
                    * Homepage, Categories and Posts Page
                    **/
                    do_action( 'post_item_image' );
                    ?>
                </a>

                <?php
                /**
                 * Behaviour based on post format
                **/

                // Gallery
                if ( 'gallery' == get_post_format() ) :

                    echo '<i class="icomoon-camera-retro"></i>';

                // Video
                elseif ( 'video' == get_post_format() ) :

                    $url_value = 'add_video_url';
                    echo '<a href="#" class="load-media-content" data-postid="' . get_the_ID() . '" data-metakey="' . esc_attr( $url_value ) . '"></a><div class="media-content"></div>';

                // Audio
                elseif ( 'audio' == get_post_format() ) :

                    $url_value = 'add_audio_url';
                    echo '<a href="#" class="load-media-content" data-postid="' . get_the_ID() . '" data-metakey="' . esc_attr( $url_value ) . '"></a><div class="media-content"></div>';

                endif;
                ?>

                <?php
                // If post has rating, show the score.
                if ( get_field( 'enable_rating' ) == 'true' ) :

                    include( locate_template( 'inc/rating-calculations.php' ) );
                    ?>

                    <span class="score-outer" style="width:<?php echo ($total_score * 10) ?>%;">
                        <span class="score-line"><i class="show-total"><?php echo esc_html( $final_result ); ?></i></span>
                    </span>

                <?php endif; ?>

            </div>
        </div>
        <?php endif ?>

<?php
    $content_post_image = ob_get_clean();
    // End image buffer


    /* Output in Page Composer */
    if ( is_page_template ( 'page-composer.php' ) ) :

        $sd_image = get_sub_field( 'post_item_displays' );
        $displays_image = is_array( $sd_image ) && in_array ( 'sd_image', $sd_image );


        /* Output the the image */
        if ( $displays_image ) :

           echo $content_post_image;

        endif;

    /* Output in Archives */
    else :

            echo $content_post_image;

    endif;

} // End Image

0 个答案:

没有答案