Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1219327
    Jan Hoogland
    Participant

    Hello,

    I’ve placed a section ‘recent-posts’ on the homepage from the which I’am developing and have chosen to display ‘portfolio-item’s. Now I’d like to display the categories of every portfolio-item beneath the title. I’ve already setup a child-theme, but I’m not sure which file I must edit to make this happen. Can u tell me which one and which code I must add tot this file?

    Thanks in advance!

    #1219476
    Rupok
    Member

    Hi there,

    It’s not usually possible but can be achieved through custom development. It would be similar to adding excepr to recent post element. So you can follow this thread – https://community.theme.co/forums/topic/recent-post-excerpt-and-excerpt-length-cornerstone/#post-297951

    Let’s add the code to show category to your desired place.

    Thanks!

    #1219628
    Jan Hoogland
    Participant

    I’ve added the code in my functions.php but which code (like: [x_recent_posts type=”post” enable_excerpt=”true” count=”3″ orientation=”horizontal”]) do I put where to get it to work. With custom themes I’ve created earlier, I could add a code such as ‘<?php
    $before = ”;
    $seperator = ”; // blank instead of comma
    $after = ”;
    the_tags( $before, $seperator, $after );?>
    When I added tags. Isn’t there a way to do so with categories?

    Thanks!

    #1219888
    Nabeel A
    Moderator

    Hi there,

    Please replace the previous code with this one in Child Theme’s functions.php file:

    function x_shortcode_recent_posts_v2( $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}"
        ) );
    
        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';
    		$categories = get_the_category();
          }
    
          $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" style="text-align: center;">' . get_the_title() . '</h3>'
    					   . '<div class="meta-wrapper">'
                           . 	'<span class="x-recent-posts-date" style="text-align: center;">' . $categories[0]->name . '</span>'
    					   . '</div>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_filter('wp_head', 'custom_recent_posts');
    
    function custom_recent_posts() {
      remove_shortcode( 'x_recent_posts' );
    
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }

    Let us know how this goes!

    #1220389
    Jan Hoogland
    Participant

    Hello,

    When I do that, only a white screen appears (white screen of death?). And is there a beginning of a comment (
    /* function x_shortcode_recent_posts_v2( $atts )) too much? A part of the code is stille comment-out when I put it in my editor.

    #1220399
    Rad
    Moderator

    Hi there,

    It’s internal server error, we could only verify it by checking it directly. Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site
    – WordPress Admin username / password
    – FTP credentials

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    Thanks!

    #1220807
    Jan Hoogland
    Participant

    hello,

    I’am sorry, but the site is currently on localhost. Is there a way to see it instead?

    Thanks

    #1221115
    Rupok
    Member

    Hi there,

    We need to check both WordPress and your files and might need to go with some trial and error. When you are ready to move on a server, you can update us. We’ll surely look into this.

    Thanks!

    #1221126
    Nabeel A
    Moderator

    Hi again,

    The given code was actually for entire functions.php file, however I’ve updated the previous code in my previous reply. You can add this code instead in your functions.php:

    function x_shortcode_recent_posts_v2( $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}"
        ) );
    
        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';
    		$categories = get_the_category();
          }
    
          $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" style="text-align: center;">' . get_the_title() . '</h3>'
    					   . '<div class="meta-wrapper">'
                           . 	'<span class="x-recent-posts-date" style="text-align: center;">' . $categories[0]->name . '</span>'
    					   . '</div>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_filter('wp_head', 'custom_recent_posts');
    
    function custom_recent_posts() {
      remove_shortcode( 'x_recent_posts' );
    
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }

    Let us know how this goes!

    #1229679
    Jan Hoogland
    Participant
    This reply has been marked as private.
    #1229681
    Jan Hoogland
    Participant
    This reply has been marked as private.
    #1230808
    Lely
    Moderator

    Hi There,

    Thank you for the screenshot. Please try to update the code to this:

    
    function x_shortcode_recent_posts_v2( $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}"
        ) );
    
        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($type =='post'){
    		$categories = get_the_category();
    		}else{
    		$categories = get_the_terms( get_the_ID() , 'portfolio-category' );		
    		}
    
          $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" style="text-align: center;">' . get_the_title() . '</h3>'
    					   . '<div class="meta-wrapper">'
                           . 	'<span class="x-recent-posts-date" style="text-align: center;">' .$categories[0]->name . '</span>'
    					   . '</div>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_filter('wp_head', 'custom_recent_posts');
    
    function custom_recent_posts() {
      remove_shortcode( 'x_recent_posts' );
    
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }

    I have added this part:

    	  if($type =='post'){
    		$categories = get_the_category();
    		}else{
    		$categories = get_the_terms( get_the_ID() , 'portfolio-category' );		
    		}

    Hope this helps.

    #1231951
    Jan Hoogland
    Participant

    You’re the best! Thanks!

    #1231986
    Jan Hoogland
    Participant

    Hello,

    Like a mentoined, the categories are showing up at the portfolio-items. But I also have a recent posts section with news-posts. Now I want to display the date with the recent posts at the news-items and the categories at the portfolio-items. Can U tell me how I can do that?

    Thanks!

    #1232295
    Rad
    Moderator

    Hi there,

    You mean selectively display date for post only, and category for portfolio only? Or should they have both date and category?

    Thanks!

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