• Resolved Bloke

    (@bloke)


    Here is my custom function that is a widget that displays the last post the category. But I need to use some PHP script to show just the first few lines of the post. Since I am not using <!–more–> tag. I need to find another way. Also you may wonder why I am using the preg_replace its because I need to remove [gallery] from showing.

    function widget($args, $instance)
      {
        extract($args, EXTR_SKIP);
    
        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
    
        if (!empty($title))
          echo $before_title . $title . $after_title;;
    
    query_posts('cat=10&posts_per_page=1');	
    
    if (have_posts()) :
    	while (have_posts()) : the_post();
    		 the_title(); echo '<br/>';
    the_post_thumbnail(apply_filters('graphene_excerpt_thumbnail_size', 'thumbnail'));
    $content = get_the_content();
    
    echo '<p span class="here">'; echo preg_replace ('/\[.*\]/', '', $content); echo '</span></p>';
    	endwhile;
    endif; 
    
    wp_reset_query();
    
        echo $after_widget;
      }
    
    }
    add_action( 'widgets_init', create_function('', 'return register_widget("MyWidget");') );?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • Have you considered the standard ‘summary’?

    1. Dashboard > settings > reading >
    2. For each article in the feed show: > 'summary'
    3. Now add a summary [in the exerpt box] below the regular edit field.

    Or you can swap get_the_content() with get_the_exerpt()

    Thread Starter Bloke

    (@bloke)

    Thanks for the replies. If I understand this will change all posts? I just want this specific function/widget to display a specific amount of characters. If I use get_the_excerpt() it displays what I put in the excerpt field and not the main article. We use the excerpt box to post a ordered list and not the main post.

    If you use the option I suggested, you can control which page will display a summary.
    I don’t know how to control the number of characters.

    If you simply want to trim the amount of characters then use substr.

    Replace the below line of code:

    $content = get_the_content();

    With:

    $content = substr(get_the_content(),0,10);

    The first argument passed is the sting you want to reduce, second is the character number from where the the function should start counting and the third would be the number of allowed characters.

    Thread Starter Bloke

    (@bloke)

    Great that worked. Thank you.

    Thread Starter Bloke

    (@bloke)

    Ok I guess I need to now add a read more… to the end of the content. How can do that?

    You will need to add a filter to excerpt_more:

    First define the link for the read more link:

    function continue_reading_link() {
        return '... <p><a href="'. get_permalink() . '" title="' . __( 'Continue Reading' ) ." class="read-more-link">' . __( 'Read More ?' ) . '</a></p>';
    }

    Then filter the excerpt_more:

    function auto_excerpt_more( $more ) {
        $content = get_the_content();
        if ( !is_admin() && !empty($content) ) {
            if (!strpos($more,'class="read-more-link">')) {
                return continue_reading_link();
            }
    	}
    }
    add_filter( 'excerpt_more', 'auto_excerpt_more' );
    Thread Starter Bloke

    (@bloke)

    Thanks. I tried it but could find where to lace it to get it to work. Either there is a typo or the balance or brackets was off. It won;t work above or below the widget function.

    function widget($args, $instance)
      {
        extract($args, EXTR_SKIP);
    
        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
    
        if (!empty($title))
          echo '<p class="title">' .$before_title . $title . $after_title;
    
        // WIDGET CODE GOES HERE
    
    query_posts('cat=10&posts_per_page=1');	
    
    if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post();echo ':  ';
    		 the_title(); echo '<br/>';?></p>
    <?php the_post_thumbnail(apply_filters('graphene_excerpt_thumbnail_size', 'thumbnail'));
    
    $content = substr(get_the_content(),0,290);
    
    echo '<p span class="here">'; echo preg_replace ('/\[.*\]/', '', $content); echo '</span></p>';
    	endwhile;
    endif; ?>
    
    <?php wp_reset_query();
    
        echo $after_widget;
      }
    
    }

    My apologies the functions for the read more link should be placed in your function.php file before or after the widget function.

    Thread Starter Bloke

    (@bloke)

    I put them in my theme functions file. I think this line is missing a closing quote or something because I get an error:

    function continue_reading_link() {
        return '... <p><a href="'. get_permalink() . '" title="' . __( 'Continue Reading' ) ." class="read-more-link">' . __( 'Read More ?' ) . '</a></p>';
    }

    I changed .” class=”read-more-link”>’ to
    .’ class=”read-more-link”>’
    but still not working.

    function continue_reading_link() {
        return '... <p><a href="'. get_permalink() . '" title="' . __( 'Continue Reading' ) . '" class="read-more-link">' . __( 'Read More ?' ) . '</a></p>';
    }
    
    function auto_excerpt_more( $more ) {
        $content = get_the_content();
        if ( !is_admin() && !empty($content) ) {
            if (!strpos($more,'class="read-more-link">')) {
                return continue_reading_link();
            }
        }
    }
    add_filter( 'excerpt_more', 'auto_excerpt_more' );
    Thread Starter Bloke

    (@bloke)

    I put that code in my functions file in my child theme. The rest of my code is in one plugin file. At first it didn’t work and I realized nothing was calling the function to show the link.

    I added echo auto_excerpt_more(); in my plugin file and it shows. Is that correct?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Showing first couple lines of post’ is closed to new replies.