Last active
July 25, 2017 05:29
-
-
Save zakirsajib/7ada5eeb784a1f4e7143b5ac1e56c080 to your computer and use it in GitHub Desktop.
Display related post’s title within single post’s texts in same category. Actually insert the related post title after each 12, 17 and 25 paragraphs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function my_post_list($title = 'Read More:', $args = array()){ | |
global $post; | |
$project_category = wp_get_post_categories($post->ID); | |
$defaults = array( // Set some defaults for querying the Posts | |
'ignore_sticky_posts' => true, | |
'posts_per_page' => 1, | |
'post_status' => 'publish', | |
'category__in' => $project_category, | |
'orderby' => 'rand' | |
); | |
// Parse any arguments passed to this function with the defaults to create a final '$args' array | |
$args = wp_parse_args($args, $defaults); | |
$random = new WP_Query($args); // Query the Posts | |
if($random->have_posts()) : // Check to make sure some random Posts were returned | |
echo '<div class="read-more">'; | |
echo '<h4>' . $title . '</h4>'; | |
echo '<ul>'; | |
// Create a new custom Loop, and for each Post set up the postdata | |
while($random->have_posts()) : $random->the_post(); | |
printf('<li id="random-post-%1$s">', get_the_ID()); | |
printf( | |
'<a href="%1$s" title="%2$s">%3$s</a>', | |
get_permalink(get_the_ID()), | |
the_title_attribute('before=Check out \'&after=\'&echo=0'), | |
get_the_title() | |
); | |
echo '</li>'; | |
endwhile; | |
wp_reset_postdata(); // Reset the postdata so that the rest of your Loop will work correctly | |
echo '</ul>'; | |
echo '</div>'; | |
endif; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the_content(); No need to use the default the_content() function | |
$content = apply_filters('the_content', get_the_content()); | |
$content = explode("</p>", $content); | |
for ($i = 0; $i <count($content); $i++) { | |
if ($i == 12) { | |
my_post_list(); | |
} | |
if ($i == 17) { | |
my_post_list(); | |
} | |
if ($i == 25) { | |
my_post_list(); | |
} | |
echo $content[$i] . "</p>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment