Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1056240
    Sarah W
    Participant

    Hi,
    I’m using Icon and I would like my ‘recent posts’ to display a small amount of text and ‘read more’.
    Is this possible?
    Thank you

    #1056273
    Sarah W
    Participant

    Further to that, actually what I want…or rather the client wants….is for the ‘recent posts’ to display
    a thinner image and some text with ‘read more’.
    So half image, half text in the boxes.
    Is this possible?

    Thank you!

    #1056275
    Sarah W
    Participant
    This reply has been marked as private.
    #1056451
    Darshana
    Moderator

    Hi there,

    Thanks for writing in! Please refer to the following post for a possible implementation (https://community.theme.co/forums/topic/adding-blog-text-and-read-more-link-to-recent-posts/#post-810125).

    Hope that helps.

    #1056553
    Sarah W
    Participant

    It does work, thanks. But is there a way of making the “Read More’ show up?

    #1056558
    Sarah W
    Participant

    Also is there a way of reducing the height of the featured image on the ‘recent posts’ box?
    Thank you

    #1057091
    Jade
    Moderator

    Hi Sarah,

    Please try this code to add the read more:

    // =============================================================================
    // Custom Excerpt Length - Call: echo excerpt($length);
    // =============================================================================
    
    function excerpt($limit) {
       $excerpt = explode(' ', get_the_excerpt(), $limit);
       if (count($excerpt)>=$limit) {
          array_pop($excerpt);
          $excerpt = implode(" ",$excerpt).'.';
       } else {
          $excerpt = implode(" ",$excerpt);
       } 
       // $excerpt = preg_replace('<code>\[[^\]]*\]</code>','',$excerpt);
       return $excerpt;
    }
    
    // =============================================================================
    // Add Excerpt to Recent Posts -> [x_recent_posts]
    // =============================================================================
    
    function x_shortcode_recent_posts_excerpt( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts, 'x_recent_posts' ) );
    
      $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );
      $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $count         = ( $count       != ''          ) ? $count : 3;
      $category      = ( $category    != ''          ) ? $category : '';
      $category_type = ( $type        == 'post'      ) ? 'category_name' : 'portfolio-category';
      $offset        = ( $offset      != ''          ) ? $offset : 0;
      $orientation   = ( $orientation != ''          ) ? ' ' . $orientation : ' horizontal';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
    
      $js_params = array(
        'fade' => ( $fade == 'true' )
      );
    
      $data = cs_generate_data_attributes( 'recent_posts', $js_params );
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";
    
        $q = new WP_Query( array(
          'orderby'             => 'date',
          'post_type'           => "{$type}",
          'posts_per_page'      => "{$count}",
          'offset'              => "{$offset}",
          "{$category_type}"    => "{$category}",
          'ignore_sticky_posts' => true
        ) );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
    
          if ( $no_image == 'true' ) {
            $image_output       = '';
            $image_output_class = 'no-image';
          } else {
            $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
            $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
            $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
            $image_output_class = 'with-image';
          }
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', csl18n() ), the_title_attribute( 'echo=0' ) ) ) . '">'
                     . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                       . '<div class="entry-wrap">'
                         . $image_output
                         . '<div class="x-recent-posts-content">'
                           . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                           . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                           . do_shortcode('[x_gap size="10px"]')
                           . '<span class="x-recent-posts-excerpt">' .  strip_tags(excerpt(25))  . '</span>'
                           . '<span class="x-recent-post-read-more"><a href="' .the_permalink() .'">Read More</a></span>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_action('wp_head', 'update_x_recent_posts_to_v2');
    
    function update_x_recent_posts_to_v2() {
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_excerpt' );
    }

    Then add this code in the CSS customizer:

    .x-recent-posts .x-recent-posts-img {
         padding-bottom: 45.25%;
    }

    Hope this helps.

    #1068576
    Sarah W
    Participant

    Hi,
    I’m afraid it doesn’t.
    I had to replace the original functions.php via ftp as it initially caused an internal error 500.
    Then I tried it again and it went nuts; separated the boxes, put weird white lines in and put the ‘read
    more’ in another box on the other side of the page.
    You are welcome to go into the site and try putting your code it into the functions.php, as maybe I’m missing something.
    But I can’t see what I’ve done wrong.

    http://176.32.230.251/amblesidesports.co.uk/wordpress/

    #1068580
    Sarah W
    Participant
    This reply has been marked as private.
    #1068979
    Rad
    Moderator

    Hi Sarah,

    Recent post item is a link itself, adding read more with another link will break it. Link within a link isn’t possible.

    Try this one,

    // =============================================================================
    // Custom Excerpt Length - Call: echo excerpt($length);
    // =============================================================================
    
    function excerpt($limit) {
       $excerpt = explode(' ', get_the_excerpt(), $limit);
       if (count($excerpt)>=$limit) {
          array_pop($excerpt);
          $excerpt = implode(" ",$excerpt).'.';
       } else {
          $excerpt = implode(" ",$excerpt);
       } 
       // $excerpt = preg_replace('<code>\[[^\]]*\]</code>','',$excerpt);
       return $excerpt;
    }
    
    // =============================================================================
    // Add Excerpt to Recent Posts -> [x_recent_posts]
    // =============================================================================
    
    function x_shortcode_recent_posts_excerpt( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts, 'x_recent_posts' ) );
    
      $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );
      $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $count         = ( $count       != ''          ) ? $count : 3;
      $category      = ( $category    != ''          ) ? $category : '';
      $category_type = ( $type        == 'post'      ) ? 'category_name' : 'portfolio-category';
      $offset        = ( $offset      != ''          ) ? $offset : 0;
      $orientation   = ( $orientation != ''          ) ? ' ' . $orientation : ' horizontal';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
    
      $js_params = array(
        'fade' => ( $fade == 'true' )
      );
    
      $data = cs_generate_data_attributes( 'recent_posts', $js_params );
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";
    
        $q = new WP_Query( array(
          'orderby'             => 'date',
          'post_type'           => "{$type}",
          'posts_per_page'      => "{$count}",
          'offset'              => "{$offset}",
          "{$category_type}"    => "{$category}",
          'ignore_sticky_posts' => true
        ) );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
    
          if ( $no_image == 'true' ) {
            $image_output       = '';
            $image_output_class = 'no-image';
          } else {
            $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
            $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
            $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
            $image_output_class = 'with-image';
          }
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', csl18n() ), the_title_attribute( 'echo=0' ) ) ) . '">'
                     . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                       . '<div class="entry-wrap">'
                         . $image_output
                         . '<div class="x-recent-posts-content">'
                           . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                           . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                           . do_shortcode('[x_gap size="10px"]')
                           . '<span class="x-recent-posts-excerpt">' .  strip_tags(excerpt(25))  . '</span>'
                           . '<span class="x-recent-post-read-more">Read More</span>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_action('wp_head', 'update_x_recent_posts_to_v2');
    
    function update_x_recent_posts_to_v2() {
      
      remove_shortcode( 'x_recent_posts' );
      remove_shortcode( 'recent_posts' );
      remove_shortcode( 'cs_recent_posts' );
    
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_excerpt' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_excerpt' );
      add_shortcode( 'cs_recent_posts', 'x_shortcode_recent_posts_excerpt' );
    
    }

    Thanks!

  • <script> jQuery(function($){ $("#no-reply-1056240 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>