• Hi,

    i’m trying to make my site add automatic information for facebook likes og:image og:Title and such.

    can someone explain why this code doesn’t work (everything works except the description part)

    <?php if (is_single()) { ?>
    <meta property="og:title" content="<?php the_title(); ?>"/>
    <meta property="og:description" content="<?php $content = get_the_content(); $content = strip_tags($content); echo substr($content, 0, 100); ?>" />
    <meta property="og:url" content="<?php the_permalink(); ?>"/>
    <?php $fb_image = wp_get_attachment_image_src(get_post_thumbnail_id( get_the_ID() ), 'thumbnail'); ?>
    <?php if ($fb_image) : ?>
        <meta property="og:image" content="<?php echo $fb_image[0]; ?>" />
    <?php endif; ?>
    <meta property="og:type" content="<?php
        if (is_single() || is_page()) { echo "article"; } else { echo "website";} ?>"
    />
    <meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
    
     <?php } ?>

    I’m using <?php $content = get_the_content(); $content = strip_tags($content); echo substr($content, 0, 100); ?> to pull the content and then strip everything beyond 100 characters, but it shows up as nothing in the html source

    its also emtpy if i use <?php echo strip_tags(get_the_excerpt($post->ID)); ?> but that’s probably because i’ve not created any excerpts, i’m not sure if excerpts are being automatically generated or if they have to be manually created by default, i’m guessing the second.

    Any help on this would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try like this

    global $post;
    $content = get_post($post->ID,OBJECT);
    $content = strip_tags($content->post_content);
    echo substr($content, 0, 100);

    Try this method, using the same excerpt filter as WordPress does (wrapped in a function for ease of use).

    function my_custom_excerpt( $post_id = NULL ) {
    	if(!$post_id) {
    		global $post;
    		$post_id = $post->ID;
    	}
    
    	$post = get_post($post_id);
    
    	$content = $post->post_content;
    	$excerpt = $post->post_excerpt;
    
    	if( !$excerpt ) {
    		$excerpt = $content;
    	}
    
    	echo apply_filters( 'get_the_excerpt', $excerpt );
    }

    Use it by calling my_custom_excerpt() to use the current post ID, or my_custom_excerpt( 55 ) to use the post with ID# 55.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need some help pulling excerpt from post that doesn't have an excerpt.’ is closed to new replies.