Archived topic
How To Insert Random Post Title As Internal Link
5 replies · Started by Md. on February 6, 2022
I want randomly 2 post titles will be inserted automatically in each article as internal links. Exactly where I want to link the random post title internally: https://postimg.cc/mcqXCY3C
How do I do this through GP Premium Element or Code Snippet?
My site: https://aboutcheck.com/
Hi there,
1. Add this PHP Snippet to your site:
function db_show_rand_posts() {
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 2,
);
$newQuery = new WP_Query( $args );
if ( $newQuery->have_posts() ) {
$string = '<ul>';
while ( $newQuery->have_posts() ) {
$newQuery->the_post();
$string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
}
$string .= '</ul>';
wp_reset_postdata();
}
return $string;
}
add_shortcode('rndm-posts','db_show_rand_posts');
This article explains how to add PHP: https://docs.generatepress.com/article/adding-css/
2. Create a new Hook Element:
https://docs.generatepress.com/article/hooks-element-overview/
2.1 Add this shortcode: [rndm-posts] to the hook text area
2.2 Set the Hook to: generate_after_content
2.3 Check the Execute Shortcodes
2.4 Set the Display Rules Location to Entire Site.
It worked. How can I take this to the above of Safety Guideline?
Where are you adding the Shortcode ?
Added this shortcode in Hook (generate_after_content)...