Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Grab Last Attachment Image instead of First & Fix OG: Placement WordPress

  • SOLVED

I use the following code but would like for it to pull the most recent image attachment for a post when shared to facebook. Currently it is pulling the 1st image ever attached to the post.

Next if possible, it may not be, but when this gets inserted into the HEAD it spans vertical instead of each META property on separate lines. Example:

<meta property="og:url" /><meta property="og:url" /><meta property="og:url" />

I would like for it to be:

<meta property="og:url" />
<meta property="og:url" />
<meta property="og:url" />


function _get_image_id( $index = 0 ) {
global $post;
$image_ids = array_keys(
get_children(
array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'DESC',
)
)
);

if ( isset( $image_ids[$index] ) )
return $image_ids[$index];
return false;
}
function get_att_images($args = array()) {
global $post;
$defaults = array(
'format' => 'url',
'size' => 'full',
'num' => 0,
'attr' => '',
);

$args = wp_parse_args( $args, $defaults );
if ( has_post_thumbnail() && ( 0 === $args['num'] ) ) {
$id = get_post_thumbnail_id();
$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
} else {
$id = _get_image_id( $args['num'] );
$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
}
$src = str_replace( home_url(), '', $url );

if ( 'html' === strtolower( $args['format'] ) )
$output = $html;
elseif ( 'url' === strtolower( $args['format'] ) )
$output = $url;
else
$output = $src;

if ( empty( $url ) ) $output = false;
return $output;
}

function insert_fb_in_head()

{

global $post;

if ( !is_singular()) //if it is not a post or a page

return;

echo '<meta property="fb:app_id" content="#" />';
echo '<meta property="fb:admins" content="#"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:locale" content="en_US" />';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:site_name" content="#"/>';
echo '<meta property="og:image:width" content="600" />';
echo '<meta property="og:image:height" content="400" />';

$post_thumb = get_att_images();
echo '<meta property="og:image" content="' .( ($post_thumb) ? $post_thumb : '../images/fblogo.png' ). '" />';
setup_postdata( $post );
$description = get_the_excerpt();
echo '<meta property="og:description" content="' .( ($description) ? $description : 'Read more!' ). '" />';
echo "\n";

}

add_action( 'wp_head', 'insert_fb_in_head', 5 );

Answers (2)

2020-05-19

Ali Hussain answers:


function _get_image_id($index = 0) {
global $post;
$image_ids = array_keys(
get_children(
array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'DESC',
)
)
);

return end($image_ids);
}
function get_att_images($args = array()) {
global $post;
$defaults = array(
'format' => 'url',
'size' => 'full',
'num' => 0,
'attr' => '',
);

$args = wp_parse_args($args, $defaults);
if (has_post_thumbnail() && (0 === $args['num'])) {
$id = get_post_thumbnail_id();
$html = wp_get_attachment_image($id, $args['size'], false, $args['attr']);
list($url) = wp_get_attachment_image_src($id, $args['size'], false, $args['attr']);
} else {
$id = _get_image_id();
$html = wp_get_attachment_image($id, $args['size'], false, $args['attr']);
list($url) = wp_get_attachment_image_src($id, $args['size'], false, $args['attr']);
}
$src = str_replace(home_url(), '', $url);

if ('html' === strtolower($args['format']))
$output = $html;
elseif ('url' === strtolower($args['format']))
$output = $url;
else
$output = $src;

if (empty($url)) $output = false;
return $output;
}

function insert_fb_in_head(){

global $post;

if (!is_singular()) //if it is not a post or a page

return;

echo '<meta property="fb:app_id" content="#" />';
echo '<meta property="fb:admins" content="#"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:locale" content="en_US" />';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:site_name" content="#"/>';
echo '<meta property="og:image:width" content="600" />';
echo '<meta property="og:image:height" content="400" />';

$post_thumb = get_att_images();
echo '<meta property="og:image" content="' . (($post_thumb) ? $post_thumb : '../images/fblogo.png') . '" />' . "\n";
setup_postdata($post);
$description = get_the_excerpt();
echo '<meta property="og:description" content="' . (($description) ? $description : 'Read more!') . '" />' . "\n";
}

add_action('wp_head', 'insert_fb_in_head', 5);



This should do the trick i.e. get the last image attached to the post

2020-05-19

Reigel Gallarde answers:

in your get_children function, change accordingly
'orderby' => 'publish_date',
'order' => 'ASC',


orderby publish_date will make sure it is the most recent.


Reigel Gallarde comments:

change

echo '<meta property="fb:app_id" content="#" />';
echo '<meta property="fb:admins" content="#"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:locale" content="en_US" />';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:site_name" content="#"/>';
echo '<meta property="og:image:width" content="600" />';
echo '<meta property="og:image:height" content="400" />';


to

ob_start();
?>
<meta property="fb:app_id" content="#" />
<meta property="fb:admins" content="#"/>
<meta property="og:url" content="<?php echo get_permalink() ?>"/>
<meta property="og:locale" content="en_US" />
<meta property="og:title" content="<?php echo get_the_title() ?>"/>
<meta property="og:type" content="article"/>
<meta property="og:site_name" content="#"/>
<meta property="og:image:width" content="600" />
<meta property="og:image:height" content="400" />
<?php
echo ob_get_clean();