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

Advanced except outside of loop WordPress

  • SOLVED

Hello,

Sorry to all due my confusion on last question. Need more coffee!!


Anyway, this is sort of follow on from what I was working on...

I am using the advanced excerpt plugin - so I have added this to my functions remove_all_filters('the_excerpt'); - this stops all excerpt plugin conflicts.

This is the advanced exceprt call... the_advanced_excerpt('length=370');

But as far as I know it does work outside the loop, get_advanced_excerpt doesn't exist - so this is where I need your help.


I'm using this function to pull in post data from outside of the loop and echo in the head.


// OPEN GRAPH META
function insert_fb_opengraph() {
global $post;
if ( !is_singular() ) {
return;
echo '<meta property="fb:admins" content="00000000000"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="Article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="Road Angel Live"/>';
echo '<meta property="og:description" content="' . $advancedExcerpt . '"/>';
}

if( ! has_post_thumbnail( $post->ID ) ) {
$default = get_bloginfo('template_url') . '/images/facebook-default.jpg';
echo '<meta property="og:image" content="' . $default . '"/>';
} else {
$ogImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );
echo '<meta property="og:image" content="' . esc_attr( $ogImage[0] ) . '"/>';
}

echo "\n";
}
add_action( 'wp_head', 'insert_fb_opengraph', 5 );



As you can see in the... echo '<meta property="og:description" content="' . $advancedExcerpt . '"/>';

I have an $advancedExcerpt variable which I need to get the_advanced_exceprt somehow from the current post.

Can anyone help me with this?

Thanks

Answers (3)

2012-07-16

Hai Bui answers:

Since we cannot use the function outside of the loop, we need to start the loop manually and print the excerpt inside it:

if (have_posts()) : the_post();
echo '<meta property="og:description" content="';
the_advanced_excerpt();
echo '"/>';
endif;


Josh Cranwell comments:

I'm not great at php, still can't this work properly? can a loop be executed in the functions.php? Thanks


// OPEN GRAPH META
function insert_fb_opengraph() {
global $post;
if ( !is_singular() )

return;
echo '<meta property="fb:admins" content=""/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="Article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="Road Angel Live"/>';

if (have_posts()) : the_post();

echo '<meta property="og:description" content="' . the_advanced_excerpt() . '"/>';

endif;

if( ! has_post_thumbnail( $post->ID ) ) {
$default = get_bloginfo('template_url') . '/images/facebook-default.jpg';
echo '<meta property="og:image" content="' . $default . '"/>';
} else {
$ogImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );
echo '<meta property="og:image" content="' . esc_attr( $ogImage[0] ) . '"/>';
}

echo "\n";
}
add_action( 'wp_head', 'insert_fb_opengraph', 5 );


Hai Bui comments:

Why don't you use my code? As I understand, the_advanced_excerpt will print the excerpt, it doesn't return the string, so we have to do it like this:
// OPEN GRAPH META

function insert_fb_opengraph() {

global $post;

if ( !is_singular() )



return;

echo '<meta property="fb:admins" content=""/>';

echo '<meta property="og:title" content="' . get_the_title() . '"/>';

echo '<meta property="og:type" content="Article"/>';

echo '<meta property="og:url" content="' . get_permalink() . '"/>';

echo '<meta property="og:site_name" content="Road Angel Live"/>';



if (have_posts()) : the_post();



echo '<meta property="og:description" content="';
the_advanced_excerpt();
echo '"/>';



endif;



if( ! has_post_thumbnail( $post->ID ) ) {

$default = get_bloginfo('template_url') . '/images/facebook-default.jpg';

echo '<meta property="og:image" content="' . $default . '"/>';

} else {

$ogImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );

echo '<meta property="og:image" content="' . esc_attr( $ogImage[0] ) . '"/>';

}



echo "\n";

}

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


Hai Bui comments:

This will work, too:
if (have_posts()) : the_post(); ?>

<meta property="og:description" content="<?php the_advanced_excerpt();?>" />
<?php
endif;

2012-07-16

Arnav Joy answers:

use this

$advancedExcerpt = the_advanced_excerpt('length=370' , true);


Josh Cranwell comments:

// OPEN GRAPH META
function insert_fb_opengraph() {
global $post;

$advancedExcerpt = the_advanced_excerpt('length=370' , true);

if ( !is_singular() )

return;
echo '<meta property="fb:admins" content="670941974, 514820059, 100002202789197"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="Article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="Road Angel Live"/>';
echo '<meta property="og:description" content="' . $advancedExcerpt . '"/>';

if( ! has_post_thumbnail( $post->ID ) ) {
$default = get_bloginfo('template_url') . '/images/facebook-default.jpg';
echo '<meta property="og:image" content="' . $default . '"/>';
} else {
$ogImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );
echo '<meta property="og:image" content="' . esc_attr( $ogImage[0] ) . '"/>';
}

echo "\n";
}
add_action( 'wp_head', 'insert_fb_opengraph', 5 );




Just get echo and elipse echo'ed :-/

People see to think this will only work in a loop?


Josh Cranwell comments:

// OPEN GRAPH META
function insert_fb_opengraph() {

global $post;
if ( !is_singular() )

return;
echo '<meta property="fb:admins" content=""/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="Article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="Road Angel Live"/>';

wp_reset_query();
if (have_posts()) : while(have_posts()) the_post();
echo '<meta property="og:description" content="' . the_advanced_excerpt() . '"/>';
endwhile; endif; wp_reset_query();

if( ! has_post_thumbnail( $post->ID ) ) {
$default = get_bloginfo('template_url') . '/images/facebook-default.jpg';
echo '<meta property="og:image" content="' . $default . '"/>';
} else {
$ogImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );
echo '<meta property="og:image" content="' . esc_attr( $ogImage[0] ) . '"/>';
}
echo "\n";
}

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




Thanks Arnav - This breaks the site :/


Arnav Joy comments:

try this
// OPEN GRAPH META
function insert_fb_opengraph() {
global $post;



if ( !is_singular() )

return;
echo '<meta property="fb:admins" content="670941974, 514820059, 100002202789197"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="Article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="Road Angel Live"/>';

wp_reset_query();
if (have_posts()) : while(have_posts()) the_post();
$advancedExcerpt = the_advanced_excerpt('length=370' , true);
echo '<meta property="og:description" content="' . $advancedExcerpt . '"/>';
endwhile;
endif;


if( ! has_post_thumbnail( $post->ID ) ) {
$default = get_bloginfo('template_url') . '/images/facebook-default.jpg';
echo '<meta property="og:image" content="' . $default . '"/>';
} else {
$ogImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'facebook-thumb' );
echo '<meta property="og:image" content="' . esc_attr( $ogImage[0] ) . '"/>';
}

echo "\n";
}
add_action( 'wp_head', 'insert_fb_opengraph', 5 );


Josh Cranwell comments:

Missing a : after while(have_posts()) but works perfect thanks!

2012-07-16

Francisco Javier Carazo Gil answers:

Look at FAQs:

Does this plugin work outside the Loop?
No, this plugin fetches the post from The Loop and there is currently no way to pass a post ID or any custom input to it. However, you can start The Loop manually and apply the plugin as usual.

So you should start the loop manually: http://codex.wordpress.org/The_Loop#Multiple_Loops