1 filthio Mar 23, 2014 23:08
3 tilqicom Apr 11, 2014 23:27
Speaking of which; does anyone know the best way to get the first attached image url of the post ?
I used to get it with the above code in 4.x
// Make sure the Item is loaded
if( $FileList = $Item->get_attachment_FileList(20) )
{ // Get 20 attached files (there might be non-images so we try them all)
while( $File = & $FileList->get_next() )
{ // Loop through attached files
if( $File->exists() && $File->is_image() )
{ // Got an image
//$url = $File->get_url();
$url = $File->get_thumb_url( 'fit-852x480' );
In 5.x all I could do was getting the Images and preg_matching.
$post_images = $Item->get_images( $params,'raw');
preg_match('/(src)=("[^"]*")/',$post_images, $img_parts);
$postimgurl = str_replace(array('src="','"'),array('',''),$img_parts[0]);
This part seemed interesting and usable in _item.class.php but could not get it to work:
// Get list of attached files
$LinkOnwer = new LinkItem( $this );
if( ! $FileList = $LinkOnwer->get_attachment_FileList( $params['limit'], $params['restrict_to_image_position'] ) )
{
return '';
}
$galleries = array();
/**
* @var File
*/
$File = NULL;
while( $File = & $FileList->get_next() )
{
$params['File'] = $File;
if( ! $File->is_image() )
{
// GET THE IMAGE URL HERE $File->get_attachment_url or something ?
4 tilqicom Apr 11, 2014 23:59
For 5.x, here is what I used for og:tags in _skin.class.php
function display_init()
{
global $blog, $disp, $Item, $baseurl, $skins_url, $Chapter, $ReqURI;
// call parent:
parent::display_init();
$og_uri = $_SERVER['HTTP_HOST'].$ReqURI;
if( isset($Item) && ! empty($Item) ) {
$og_uri = $baseurl .'index.php?p='.$id.'&blog='.$blog.'&redir=no';
$og_desc = $Item->excerpt;
$og_title = $Item->title;
$post_images = $Item->get_images( array(
'before' => '',
'before_image' => '',
'before_image_legend' => '',
'after_image_legend' => '',
'after_image' => '',
'after' => '',
'limit' => 1,
'image_link_to' => 'original',
'before_gallery' => '',
'after_gallery' => '',
// Optionally restrict to files/images linked to specific position: 'teaser'|'aftermore'
'restrict_to_image_position' => 'teaser',
),'raw' );
if( ! empty ($post_images) ) {
preg_match('/(src)=("[^"]*")/',$post_images, $img_parts);
$postpic = str_replace(array('src="','"'),array('',''),$img_parts[0]);
}
}
else if ( isset($Chapter) && ! empty($Chapter) ) {
//if we are on category
$og_desc = $Chapter->description;
$og_title = $Chapter->name;
}
else {
// Just in case, for everything else.
$og_desc = $Blog->description;
$og_title = $Blog->name;
}
$id = $Item->ID;
add_headline('<meta property="og:type" content="article">');
add_headline('<meta property="og:description" content="'.$og_desc.'">');
add_headline('<meta property="og:url" content="'. $og_uri . '">');
add_headline('<meta property="og:title" content="'.$og_title.'">');
if ( isset($postpic) && ! empty($postpic) ) {
add_headline('<meta property="og:image" content="'.$postpic.'">');
}
1- For excerpt (og:description) I use the below
2- For og:image I use the attached image.
Also I'd like to point out that your og:url should not be prettyurl. Otherwise, you will lose your "likes" when the url changes -when you change your title or your permalink structure-. There's no way to redirect / transfer them. So I use the shortlink, such as:
Plesae note that
1- These codes go to _skin.class.php ( I placed them inside my display_init() function )
2- You need to call the globals
3- You might wanna wrap your code with a conditional to display og tags only on single display to avoid confusion with a
4- This works with 4.x. With 5.x the og:image thing doesnt work since get_attachment_FileList() seems to have moved to another class