• Hello guys,

    I am having some problem displaying a list of posts preferably with excerpts on a post page.

    Now I am using gravity forms to let users submit posts on a post page, what the gravity forms do is, they record the post ID of the post that the form was submitted on. So that new post that has been made by the form has a forum ID of the post that it was submitted from.

    What I want to do is display all posts that are attached to a specific post ID in their respective posts.

    I’ve gotten some tutorials, here is what I am trying to get… this obviously is with attached images, I just want this to be changed to links to those attached posts possibly having excerpts as well

    https://theme.fm/wp-content/uploads/2011/07/post-attachments-plain-600×376.png

    Here is the code of the above

    add_filter( 'the_content', 'my_the_content_filter' );
    function my_the_content_filter( $content ) {
    	global $post;
    
    	if ( is_single() && $post->post_type == 'post' && $post->post_status == 'publish' ) {
    		$attachments = get_posts( array(
    			'post_type' => 'attachment',
    			'posts_per_page' => 0,
    			'post_parent' => $post->ID
    		) );
    
    		if ( $attachments ) {
    			$content .= '<h3>Attachments</h3>';
    			$content .= '<ul class="post-attachments">';
    			foreach ( $attachments as $attachment ) {
    				$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
    				$title = wp_get_attachment_link( $attachment->ID, false );
    				$content .= '<li class="' . $class . '">' . $title . '</li>';
    			}
    			$content .= '</ul>';
    		}
    	}
    
    	return $content;
    }

    I tried to change it to fit what I wanted.. by doing the following…

    add_filter( 'the_content', 'my_the_content_filter' );
    function my_the_content_filter( $content ) {
    	global $post;
    
    	if ( is_single() && $post->post_type == 'post' && $post->post_status == 'publish' ) {
    		$mirrorlinks = get_posts( array(
    			'post_category' => 395,
    			'posts_per_page' => 0,
    			'post_parent' => $post->ID
    		) );
    
    		if ( $mirrorlinks ) {
    			$content .= '<h3>Other Mirrors</h3>';
    			$content .= '<ul class="post-attachments">';
    			foreach ( $mirrorlinks as $mirrorlinks ) {
    				$class = "post-attachment mime-" . sanitize_title( $mirorlinks->post_mime_type );
    				$title = wp_get_post_link( $mirrorlinks->ID, false );
    				$content .= '<li class="' . $class . '">' . $title . '</li>';
    			}
    			$content .= '</ul>';
    		}
    	}
    
    	return $content;
    }

    That gives me errors lol.

    So currently, I just want to do the following, first the code gets all the posts from the links category (since thats where all the submitted posts go). Then it checks the post ID the user is currently on, and displays the links category posts that have the matching parent post IDs.

    The display just includes the title, and hopefully a little excerpt.

    Please guys, pretty much any help would be appreciated. The best would be some type of code that I could work with, which includes which file I should put it in (I am not very experienced with wordpress coding).

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter faeronsayn

    (@faeronsayn)

    The following code I was able to actually use to show those link posts but it is displaying all posts. It is not displaying link posts specific to the post that I am currently on.

    <?php
    $cat_id = get_cat_ID('Links'); //<--your category here
    $args=array(
      'cat' => $cat_id,
      'meta_key'=>'parentId',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>"rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter faeronsayn

    (@faeronsayn)

    Any help would be appreciated, I am sure this isn’t that hard :'(

    [No bumping, thank you.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Showing Attached Posts on post page’ is closed to new replies.