Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #183426

    nvanhoose
    Participant

    I am currently updating my site to x-theme. Building it on another server and preparing to migrate, etc.

    All of my blog posts on the current site have a custom field labeled “condensed-title.” I used this on the old site when the full title was a little long.

    I would like to use this meta field when I employ the themes recent posts shortcode in certain cases. Is this possible? For instance, when I use the recent posts shortcode in the sidebar, some of my posts have titles that are just too long and look bad. The “condensed-title” I’ve already made for all of the posts would be perfect.

    Thanks for your help!!!

    #183778

    nvanhoose
    Participant

    The way I accomplish this right now is as such:

    <ul>
    <?php while (have_posts()): the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'list-thumb' ); ?></a>
    <b><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, 'condensed_title', true); ?></a></b><br />
    <p><?php echo get_the_other_excerpt(); ?><span><a href="<?php the_permalink(); ?>">more »</a></span></p></li>
    <?php endwhile; ?>
    </ul>

    Obviously not very elegant but it worked. With all the functions going on in this theme, I can’t make heads or tails of where or how to call up that alternate, shorter title…

    #183792

    nvanhoose
    Participant

    Hey guys,

    So I basically got this working by changing the relevant line in functions.php to

    . '<h3 class="h-recent-posts">' . get_post_meta(get_the_ID(), 'custom-field-title, true ) . '</h3>'

    which is referenced in another thread. It would be nice to be able to trigger this action with something in the shortcode like shortened-title=yes… but for now is working great. Thanks.

    #183816

    Zeshan
    Member

    Hi there,

    Thanks for writing in!

    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    After that, add following code in your child theme’s functions.php file:

    function x_shortcode_recent_posts2( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => '',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts ) );
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $type          = ( $type        == 'portfolio' ) ? 'x-portfolio' : 'post';
      $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';
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} 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_output       = '<div class="x-recent-posts-img">' . get_the_post_thumbnail( get_the_ID(), 'entry-cropped', NULL ) . '</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"', '__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">' . (($v = get_post_meta(get_the_ID(), 'condensed_title', true)) ? $v : 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_filter('init', 'custom_recents_posts');
    
    function custom_recents_posts() {
       remove_shortcode( 'recent_posts' );
    
       add_shortcode( 'recent_posts', 'x_shortcode_recent_posts2' );
    }
    

    Hope this helps. 🙂

    Thank you.

    #337543

    troydesh
    Participant

    I am trying to do something very similar. I tried modifying this solution, but couldn’t quite figure it out.

    I’d like to do is add data from the portfolio custom field called “role” just below the post tile (in place of date) when using the recent_posts shortcode

    It would also be fantastic if I could add a custom button to display below that custom field data. I could potentially use the Project Link field for the data. Or would a separate custom field be best?
    Just not sure how to implement into shortcode.

    So when using recent_posts shortcode it would display:

    Image
    Post Title
    Role
    Button

    #337685

    Rue Nel
    Moderator

    Hello There,

    Thanks for updating this thread!

    Please make use of the custom field instead. For more details on how to use custom fields, please check it out here: https://codex.wordpress.org/Custom_Fields

    To display it in the custom recent posts element, please make sure that you do have a child theme installed. You can then insert this code in your child theme’s functions.php file

    function x_shortcode_recent_posts2( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => '',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => ''
      ), $atts ) );
    
      $id            = ( $id          != ''          ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class         = ( $class       != ''          ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
      $style         = ( $style       != ''          ) ? 'style="' . $style . '"' : '';
      $type          = ( $type        == 'portfolio' ) ? 'x-portfolio' : 'post';
      $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';
    
      // Your custom fields
      $field1 = get_post_meta( get_the_ID(), 'custom-field-1-name', true );
      $field2 = get_post_meta( get_the_ID(), 'custom-field-2-name', true );
    
      $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} 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_output       = '<div class="x-recent-posts-img">' . get_the_post_thumbnail( get_the_ID(), 'entry-cropped', NULL ) . '</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"', '__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">' . (($v = get_post_meta(get_the_ID(), 'condensed_title', true)) ? $v : get_the_title()) . '</h3>'
                           . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                           . <span class="custom-class field1">' . $field1 . '</span>
                           . <span class="custom-class field2">' . $field2 . '</span>
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    add_filter('init', 'custom_recents_posts');
    
    function custom_recents_posts() {
       remove_shortcode( 'recent_posts' );
       add_shortcode( 'recent_posts', 'x_shortcode_recent_posts2' );
    }

    As this is all custom development, regretfully we won’t be able to assist further. Custom development is outside the scope of our support. We’re happy to provide advice and get you started in the right direction, but you would still be responsible for the implementation.

    Thank you for your understanding.

    #338313

    troydesh
    Participant

    I appreciate your help on this. For some reason this code results in an error though
    Parse error: syntax error, unexpected ‘<‘

    in this line of functions.php in my child theme:

    . <span class=”custom-class field1″>’ . $role . ‘</span>

    #338646

    Rad
    Moderator

    Hi there,

    There are missing single quotes. Should be like this,

                           . '<span class="custom-class field1">' . $field1 . '</span>'
                           . '<span class="custom-class field2">' . $field2 . '</span>'

    Cheers!

    #659890

    DoebankDesigns
    Participant

    I’m trying to use this code to display two custom fields instead of the post date, but I can’t seem to get it to work. Any ideas?

    #660140

    Nabeel A
    Moderator

    Hi there,

    Thanks for writing in! You’ll need to add the above code in your child theme’s functions.php file. Can you please provide us the URL of your site so we can take a look at your current setup?

    Thanks!