Change Wordpress functionality

Asked

Viewed 104 times

1

How to change this script, so that instead of it grab the image from the Wordpress directory, it grab the first image that exists in the post.

The purpose of this script is instead of the theme search only images from the Wordpress directory, it also use any image you have in the post.

I am using this tutorial to try to perform this preparation:

http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it
http://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/

Tutorial Script:

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content,     $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

This little stretch I don’t know where I use:

<?php echo catch_that_image() ?>

The script to be modified is this:

<?php

/**
 * Containers for storing thumbnail types and its default sizes.
 * @since 1.4.4
 */
$arras_image_sizes = array();

function arras_add_default_thumbnails() {

    $single_thumbs = arras_get_single_thumbs_size();
    arras_add_image_size( 'single-thumb', __('Single Post Thumbnail', 'arras'),             $single_thumbs[0], $single_thumbs[1] );
arras_add_image_size( 'sidebar-thumb', __('Sidebar Widgets', 'arras'), 36, 36); 

do_action('arras_add_default_thumbnails');
}

/**
 * Function to add image size into both theme system.
 * @since 1.4.4
 */
function arras_add_image_size($id, $name, $default_width, $default_height) {
global $arras_image_sizes;

    $arras_custom_image_sizes = arras_get_option('custom_thumbs');

    // Check from options if a custom width and height has been specified, else use              defaults
if (isset($arras_custom_image_sizes[$id])) {
    $width = $arras_custom_image_sizes[$id]['w'];
    $height = $arras_custom_image_sizes[$id]['h'];
} else {
    $width = $default_width;
    $height = $default_height;
}

$arras_image_sizes[$id] = array(
    'name'  => $name, 
    'w'     => $width, 
    'h'     => $height,
    'dw'    => $default_width,
    'dh'    => $default_height
);

add_image_size($id, $width, $height, true);
}

/**
 * Function to remove image size into both theme system.
 * @since 1.4.4
 */
function arras_remove_image_size($id) {
global $arras_image_sizes, $_wp_additional_image_sizes;

unset($arras_images_sizes[$id]);
unset($_wp_additional_image_sizes[$id]);
}

/**
 * Function to get image size's name, width and height, default or custom.
 * @since 1.4.4
 */
function arras_get_image_size($id) {
global $arras_image_sizes;

return (isset($arras_image_sizes[$id])) ? $arras_image_sizes[$id] : false;
}


/**
 * Helper function to grab and display thumbnail from specified post
 * @since 1.4.0
 */
function arras_get_thumbnail($size = 'thumbnail', $id = NULL) {
global $post, $arras_image_sizes;

$wxh = arras_get_image_size( $size );
    $empty_thumbnail =  '<img src="' . get_template_directory_uri() . '/images/thumbnail.png" alt="' . get_the_excerpt()
                                        . '" title="' . get_the_title() . '" width="' .     $wxh['w'] . '" height="' . $wxh['h'] . '" />';

if ($post) $id = $post->ID;

// get post thumbnail (WordPress 2.9)
if (function_exists('has_post_thumbnail')) {
    if (has_post_thumbnail($id)) {
        return get_the_post_thumbnail( $id, $size, array(
            'alt'   => get_the_excerpt(), 
            'title' => get_the_title()
        ) );
    } else {
        // Could it be an attachment?
        if ($post->post_type == 'attachment') {
            return wp_get_attachment_image( $id, $size, false, array(
                'alt'   => get_the_excerpt(), 
                'title' => get_the_title()
            ) );
        }       
        // Use first thumbnail if auto thumbs is enabled.
        if (arras_get_option('auto_thumbs')) {
            $img_id = arras_get_first_post_image_id();
            if (!$img_id) return $empty_thumbnail;

            return wp_get_attachment_image($img_id, $size, false, array(
                'alt'   => get_the_excerpt(), 
                'title' => get_the_title()
            ) );
        }
    }
}
return $empty_thumbnail;
}

/**
 * Function to retrieve the first image ID from post.
 * @since 1.5.0
 */
function arras_get_first_post_image_id($id = NULL) {
global $post;
if (!$id) $id = $post->ID;

$attachments = get_children('post_parent=' . $id .     '&post_type=attachment&post_mime_type=image');
if (!$attachments) return false;

$keys = array_reverse(array_keys($attachments));
return $keys[0];
}

function arras_get_single_thumbs_size() {
$layout = arras_get_option('layout');

if ( strpos($layout, '1c') !== false ) {
    $size = array(930, 375);
} else if ( strpos($layout, '3c') !== false ) {
    $size = array(465, 190);
} else {
    $size = array(620, 300);
}

return apply_filters('arras_content_width', $size);
}

/* End of file thumbnails.php */
/* Location: ./library/thumbnails.php */

2 answers

0

There are two ways to do this, if you take your text and the image that is the first is the thumbnail of the post you can do the form 1, if it is an image in the text and not the highlighted you can do the form 2

Form 1:

<?php get_the_post_thumbnail() ?>

Form 2:

<?php catch_that_image() ?>

0

This script described by Function catch_that_image already takes the first image of the post through the preg_match

To display the image you will use the code below within your theme, either on the page that displays the post or in the loop

<?php echo catch_that_image() ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.