Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1127814
    Mark_westcott
    Participant

    Hey,

    I have been using a code to get the recent posts to go to the portfolio index instead of the post itself. This has now broke when updated site to CS builder from VC builder. Could you help to bring this functionality back please.

    site: fisheyestudio.com

    This is the code I was using:

    // Recent Posts with custom link
    // =============================================================================
    
    function x_shortcode_recent_posts_add_link( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => '',
        'link'        => ''
      ), $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';
      $link          = ( $link        != ''          ) ? $link : '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}"
        ) );
    
        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';
          }
    
          if ( ! $link ) {
          $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>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
                   } else {
            $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . $link . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), 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>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
          }
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_action('init', 'add_link_to_recent_posts');
    
    function add_link_to_recent_posts() {
      remove_shortcode( 'recent_posts' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_add_link' );
    }
    
    // End Recent Posts with custom link
    // =============================================================================
    #1128275
    Rue Nel
    Moderator

    Hello There,

    Thanks for writing in! Please do update the portion of your code and use this instead:

    function add_link_to_recent_posts() {
      remove_shortcode( 'recent_posts' );
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_add_link' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_add_link' );
    }

    Hope this helps. Please let us know how it goes.

    #1129000
    Mark_westcott
    Participant

    Seems that didn’t work and just returned fisheyestudio.com/false

    Is there a simpler way to do this (i.e.. cleaner code than what we have above)?

    Basically I’m trying to have recent posts go to the portfolio’s index page.. Currently I have 2 portfolio categories: commercial & wedding. Each recent posts image when clicked, would go to their respective portfolio’s index.

    Any help is appreciated. Thanks!

    #1129474
    Rad
    Moderator

    Hi there,

    I just checked and it’s working fine on my end, you may also change it to this

    function add_link_to_recent_posts() {
      remove_shortcode( 'recent_posts' );
      remove_shortcode( 'x_recent_posts' );
      remove_shortcode( 'cs_recent_posts' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_add_link' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_add_link' );
      add_shortcode( 'cs_recent_posts', 'x_shortcode_recent_posts_add_link' );
    }

    How do I reproduce the issue related to fisheyestudio.com/false? You may provide video recording as visual guide 🙂

    Thanks!

    #1129684
    Mark_westcott
    Participant

    Figured it out..

    I re-looked over the old advice and completely forgot there’s a backend element to this all. I needed to insert the link in the shortcode. This is probably why I was getting the return of fisheyestudio.com/false when I clicked on the recent posts, as it was inserting that instead of the link I was supposed to input.

    I’ve gone into the text portion of the page and added the links in each recent posts section, so now they load into the index pages as wanted. Thanks for helping me with this!

    But, I’m now left with another issue that I hope you could solve: can this link be added inside the cornerstone builder? inside the recent posts element (possibly with a bit of JS)? Since the way it’s implemented now, it’ll erase the links if I enter cornerstone to edit the page, so not a long term solution.

    Any help would be great. Thanks!

    #1129761
    Rue Nel
    Moderator

    Hello There,

    if you want the option “Link” and that you can easily set it to true or false in Cornerstone, that would require modification of the element which is not highly recommended. I would rather suggest that you create your own custom recent post addon so that you can use it in Cornerstone as an extension. To know more how you can do this, please check out this:
    https://community.theme.co/kb/cornerstone-custom-elements/

    Hope this helps.

    #1131120
    Mark_westcott
    Participant

    Thanks for the recommend. Although, that may be a little too much for me to figure out at the moment, as I have a lot on the go. I’ll look into it when I get some time.

    For now, I guess I’ll just have to stick with manually altering the code outside of the CS builder.

    Thanks!

    #1131217
    Mark_westcott
    Participant

    Never mind. Just tested it and realized that I can use a text element then use the recent posts shortcode. Then I can easily add the link there.

    #1131344
    Lely
    Moderator

    Hi There,

    Yes, it will work on shortcode. Thank for updating us and glad this is working for you now.

    Cheers!

    #1132369
    Mark_westcott
    Participant

    Just a heads up in case others try this. You have to change in the shortcode section=”xxx” to category=”xxx” for some reason the default when you add it is section but this doesn’t work. If you change that to category instead it works just fine.

    Not sure if this is an overlooked bug or not.

    Thanks!

    #1132631
    Jade
    Moderator

    Thanks for sharing Mark. Cheers!

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