Home › Forums › Back End › need help with “How to: Get the first image from the post and display it”
- This topic is empty.
-
AuthorPosts
-
September 18, 2012 at 4:06 pm #39909chanmanMember
http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it
This is a wordpress problem. I put the code where it said to in my functions and loop files. but all it outputs is the url of the image. here is my site http://www.yardsale.cdcwebdesign.com.
And no I am not trying to steal the picture a the top of the page i will pay for it when the site goes live. thanks in advanceSeptember 18, 2012 at 5:41 pm #110303chrisburtonParticipant@chanman Did you remove anything? Paste the exact code you put inside your loop.
Or you can try this:
function echo_first_image ($postID)
{
$args = array(
‘numberposts’ => 1,
‘order’=> ‘ASC’,
‘post_mime_type’ => ‘image’,
‘post_parent’ => $postID,
‘post_status’ => null,
‘post_type’ => ‘attachment’
);$attachments = get_children( $args );
//print_r($attachments);
if ($attachments) {
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) ? wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) : wp_get_attachment_image_src( $attachment->ID, ‘full’ );echo ‘
September 18, 2012 at 6:39 pm #110305chanmanMemberThis is what is in my entire loop.php file
> “>
‘, ‘, ‘, ‘‘); ?>
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo ‘Post Divider‘;
}
?>
September 18, 2012 at 10:33 pm #110315chanmanMemberSorry I posted the loop instead of functions I’ll post it in the morning.
September 19, 2012 at 9:12 am #110341chanmanMemberThis is just the top of the functions.php file
// You may add your custom functions here
///////////////////////////////////////function catch_that_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘//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;
}///////////////////////////////////////
// Load theme languages
///////////////////////////////////////
load_theme_textdomain( ‘themify’, TEMPLATEPATH.’/languages’ );///////////////////////////////////////
// Enable WordPress feature image
///////////////////////////////////////
add_theme_support( ‘post-thumbnails’);///////////////////////////////////////
// Register Custom Menu Function
///////////////////////////////////////
if (function_exists(‘register_nav_menus’)) {
register_nav_menus( array(
‘main-nav’ => __( ‘Main Navigation’, ‘themify’ ),
‘footer-nav’ => __( ‘Footer Navigation’, ‘themify’ ),
) );
}September 19, 2012 at 9:23 am #110342chrisburtonParticipant@chanman Remove what you previously used and try the code I provided for you and put it in your theme file.
You also seem to have an error in your functions.php file. I cannot access your site at all.
September 19, 2012 at 9:25 am #110343chanmanMemberI’m trying to do that right now but every time i try to save the file it gives me this
Parse error: syntax error, unexpected $end in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/functions.php on line 199
September 19, 2012 at 9:30 am #110345chrisburtonParticipant@chanman Line 199 seems to be fixed now. You have an error on line 3 in loop.php. Perhaps that is ``
If so, remove that.
September 19, 2012 at 9:31 am #110346chanmanMemberI figured out the problem i left out the last } in your code.
September 19, 2012 at 9:33 am #110347chanmanMemberOk, I got rid of my stuff and put yours and now it’s saying this:
Fatal error: Call to undefined function catch_that_image() in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/includes/loop.php on line 3
that probably means I’m still calling my code and not yours right?
September 19, 2012 at 9:34 am #110348chanmanMemberThis is whats on line 3
September 19, 2012 at 9:35 am #110349chrisburtonParticipant@chanman The code I provide you with goes into the theme file (loop.php) not functions.php and you still have an error on line 3 in loop.php.
September 19, 2012 at 9:35 am #110350chrisburtonParticipant@chanman Yes. Above I asked you to remove that.
September 19, 2012 at 9:38 am #110351chanmanMemberI thought it went in the functions file let me change it real quick and see what i get.
September 19, 2012 at 9:40 am #110352chrisburtonParticipant@chanman Wrap it in php tags ``
function echo_first_image ($postID)
{
$args = array(
‘numberposts’ => 1,
‘order’=> ‘ASC’,
‘post_mime_type’ => ‘image’,
‘post_parent’ => $postID,
‘post_status’ => null,
‘post_type’ => ‘attachment’
);$attachments = get_children( $args );
//print_r($attachments);
if ($attachments) {
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) ? wp_get_attachment_image_src( $attachment->ID, ‘thumbnail’ ) : wp_get_attachment_image_src( $attachment->ID, ‘full’ );echo ‘
AuthorPosts- The forum ‘Back End’ is closed to new topics and replies.