Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1185002

    blueprint
    Participant

    Great, thank you! The layout problem seems to be fixed now with your help!

    I followed the thread for the connection of custom post types with “recent post” and followed all the steps for editing the funktions.php in the child theme folder. Unfortunately a syntax error occures, that I could not fix. Can you see what the problem is?

    This is the message I’m getting:
    Parse error: syntax error, unexpected ‘video_typ’ (T_STRING) …. on line 65

    This is the code I tried to add to the functions.php:

    function x_shortcode_recent_posts_v2( $atts ) {
      extract( shortcode_atts( array(
        'id'           => '',
        'class'        => '',
        'style'        => '',
        'type'         => 'post',
        'count'        => '',
        'category'     => '',
        'offset'       => '',
        'orientation'  => '',
        // 'show_excerpt' => 'true',
        'no_sticky'    => '',
        '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 : '';
    
      if( $type == 'post' ){
        $category_type = 'category_name';
      } elseif ($type == 'x-portfolio') {
        $category_type = 'portfolio-category';
      } elseif ($type == 'video_beitrag’) {
        $category_type = 'video_typ';
      }
    
      $offset        = ( $offset       != ''     ) ? $offset : 0;
      $orientation   = ( $orientation  != ''     ) ? ' ' . $orientation : ' horizontal';
      // $show_excerpt  = ( $show_excerpt == 'true' );
      $no_sticky     = ( $no_sticky    == 'true' );
      $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' => $no_sticky
        ) );
    
        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';
          }
    
          // $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';
    
          $output .= '<a href="' . get_permalink( get_the_ID() ) . '">'
                     . '<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>'
                           // . $excerpt
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    add_action('init', 'override_shortcode' );
    function override_shortcode(){
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }
    
    function add_event_post_type( $types ) {
      $types['post_type_name'] = 'video_beitrag';
      return $types;
    }
    add_filter( 'cs_recent_posts_post_types', 'add_event_post_type', 999 );
    #1185510

    Rad
    Moderator

    Hi there,

    It’s because of this,

     elseif ($type == 'video_beitrag’) {
        $category_type = 'video_typ';
      }

    should be like this

     elseif ($type == 'video_beitrag') {
        $category_type = 'video_typ';
      }

    Please check your codes for proper quotes.

    Thanks!

    #1185535

    blueprint
    Participant

    😀 thanks!!!!! I was too blind to see it 🙂

    I now see that I have to choose weather I want to show custom posts types or regular ones in my “recent post” widget. Is there any way to display ALL the recent post types (custom or regular) together?

    And how can I make the custom post types appear on the sidbar under “recent posts”?

    #1185982

    Rue Nel
    Moderator

    Hello There,

    Thank you for the updates. To be able to resolve your issue, please check out this topic: https://community.theme.co/forums/topic/recent-posts-and-custom-post-types/

    Hope this helps.

    #1186439

    blueprint
    Participant

    Thanks for the link! I tried changing the functinos.php accordingly but the cutom posts still won’t show up on the sidebar or the “recent post” widget. Can you give me further instructions?

    This is what the functions.php looks like:

    <?php
    
    // =============================================================================
    // FUNCTIONS.PHP
    // -----------------------------------------------------------------------------
    // Overwrite or add your own custom functions to X in this file.
    // =============================================================================
    
    // =============================================================================
    // TABLE OF CONTENTS
    // -----------------------------------------------------------------------------
    //   01. Enqueue Parent Stylesheet
    //   02. Additional Functions
    // =============================================================================
    
    // Enqueue Parent Stylesheet
    // =============================================================================
    
    add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
    
    // Additional Functions
    // =============================================================================
    function x_custom_blog($query) {
    	if( !is_admin() && $query->is_main_query() ) {
    		if( $query->is_home() ) {
    			$query->set('post_type', array( 'post', 'video_beitrag' ) );
    		}
    	}
    }
    add_action('pre_get_posts','x_custom_blog');
    
    // custom post in recent posts bringen
    // =============================================================================
    function x_shortcode_recent_posts_v2( $atts ) {
      extract( shortcode_atts( array(
        'id'           => '',
        'class'        => '',
        'style'        => '',
        'type'         => 'post',
        'count'        => '',
        'category'     => '',
        'offset'       => '',
        'orientation'  => '',
        // 'show_excerpt' => 'true',
        'no_sticky'    => '',
        'no_image'     => '',
        'fade'         => ''
      ), $atts, 'x_recent_posts' ) );
    
      $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post', 'video_beitrag' ) );
      $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 : '';
    
      if( $type == 'post' ){
        $category_type = 'category_name';
      } elseif ($type == 'x-portfolio') {
        $category_type = 'portfolio-category';
      } elseif ($type == 'video_beitrag') {
        $category_type = 'video_typ';
      }
    
      $offset        = ( $offset       != ''     ) ? $offset : 0;
      $orientation   = ( $orientation  != ''     ) ? ' ' . $orientation : ' horizontal';
      // $show_excerpt  = ( $show_excerpt == 'true' );
      $no_sticky     = ( $no_sticky    == 'true' );
      $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' => $no_sticky
        ) );
    
        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';
          }
    
          // $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', 'cornerstone' ), 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>'
                           // . $excerpt
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    add_action('init', 'override_shortcode' );
    function override_shortcode(){
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }
    
    function add_event_post_type( $types ) {
      $types['video_beitrag'] = 'video_beitrag';
      $types['post'] = 'post';
      return $types;
    }
    add_filter( 'cs_recent_posts_post_types', 'add_event_post_type', 999 );
    
    #1187058

    Rad
    Moderator

    Hi there,

    You can’t add all post types automatically (regular and custom post type). Media images, attachment, revisions, and more and are all post types. You’ll have to be specific which post types you’ll have to add.

    Then please change this,

    add_action('init', 'override_shortcode' );
    function override_shortcode(){
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
    }

    to this

    add_action('init', 'override_shortcode' );
    function override_shortcode(){
      remove_shortcode( 'x_recent_posts' );
    remove_shortcode( 'recent_posts' );
    remove_shortcode( 'cs_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
      add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v2' );
      add_shortcode( 'cs_recent_posts', 'x_shortcode_recent_posts_v2' );
    }

    Thanks!