Skip to content

Instantly share code, notes, and snippets.

@oalansari82
Last active January 11, 2017 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oalansari82/4fc0d21840bc7ee65d7de837e493fcb5 to your computer and use it in GitHub Desktop.
Save oalansari82/4fc0d21840bc7ee65d7de837e493fcb5 to your computer and use it in GitHub Desktop.
Create related posts section in Genesis Framework using ACF
<?php
add_action( 'genesis_before_comments', 'io_related_posts' );
function io_related_posts() {
$relatedPosts = get_field( 'related_posts' );
if( $relatedPosts ):
echo '<div class="related-posts-container">';
echo '<h3>Related Posts</h3>';
echo '<ul class="related-posts">';
foreach( $relatedPosts as $relatedPost ): // variable must NOT be called $post (IMPORTANT)
echo '<li>';
echo '<a href="' . get_permalink( $relatedPost->ID ) . '">' . get_the_post_thumbnail( $relatedPost->ID ) .'<h5>' . get_the_title( $relatedPost->ID ) . '</h5></a>';
echo wp_trim_words( get_the_content( $relatedPost->ID ), 20, null );
echo '</li>';
endforeach;
echo '</ul>';
echo '</div>';
endif;
}
genesis();
.related-posts-container {
background-color: #fff;
font-size: 18px;
font-size: 1.8rem;
margin-bottom: 40px;
padding: 60px;
}
.related-posts-container h3 {
margin-bottom: 5%;
}
.related-posts {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.related-posts li {
-ms-flex-preferred-size: 32%;
flex-basis: 32%;
}
@media only screen and (max-width: 450px) {
.related-posts li {
-ms-flex-preferred-size: 100%;
flex-basis: 100%;
margin-bottom: 10%;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment