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 19, 2012 at 9:44 am #110353chanmanMember
I figured that out after i saved it.
September 19, 2012 at 9:50 am #110355chrisburtonParticipant@chanman Repaste your loop.php file without changing anything.
September 19, 2012 at 9:55 am #110356chanmanMemberIt’s giving me an error but heres the code
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 ‘” >
“>
‘, ‘, ‘, ‘‘); ?>
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo ‘Post Divider‘;
}
?>
September 19, 2012 at 10:00 am #110357chanmanMemberHere is the error it’s giving me:
Fatal error: Cannot redeclare echo_first_image() (previously declared in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/includes/loop.php:4) in /nfs/c10/h02/mnt/142167/domains/yardsale.cdcwebdesign.com/html/wp-content/themes/base/includes/loop.php on line 27
September 19, 2012 at 10:01 am #110358chanmanMemberSorry for all the problems I’m new to wordpress and can’t figure it out yet.
September 19, 2012 at 10:03 am #110359chrisburtonParticipant@chanman Try this just for kicks.
if(!function_exists(‘echo_first_image’))
{
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 19, 2012 at 10:11 am #110360chanmanMemberit’s not working for me and it’s not giving an error you go check it out.
http://www.yardsale.cdcwebdesign.comSeptember 19, 2012 at 10:13 am #110361chanmanMemberI don’t know if this matters but on the first post the image is in my media library and the second and third post they are called from the mapbox api.
September 19, 2012 at 10:16 am #110362chrisburtonParticipant@chanman I might know a fix for your previous code. Repaste your old code into functions.php and loop.php but instead of ``
do this: `
September 19, 2012 at 10:24 am #110364chanmanMemberdidn’t work
here is the loop
” >
“>
‘, ‘, ‘, ‘‘); ?>
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo ‘Post Divider‘;
}
?>
here is the fuctions
// 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’ );September 19, 2012 at 10:25 am #110365chanmanMemberit’s not giving an error either.
September 19, 2012 at 10:27 am #110366chrisburtonParticipant@chanman Remove the img tag and just try `` to see if it outputs anything.
September 19, 2012 at 10:30 am #110367chanmanMembernothing yet.
September 19, 2012 at 10:33 am #110368chrisburtonParticipantThen you must have changed something. Are you trying to get the first image of every post?
September 19, 2012 at 10:39 am #110370chanmanMemberyep, i’m not very good at php yet. Do you know of a way you access my code to help me? Is there a way to ad an admin to wordpress or a temp admin?
AuthorPosts- The forum ‘Back End’ is closed to new topics and replies.