WordPress.org

Ready to get started?Download WordPress

Forums

Display random blog posts in content boxes (3 posts)

  1. Layser
    Member
    Posted 1 year ago #

    Hey everyone,

    I'm new to WordPress but quickly learning.

    On my website I have three content boxes, and I would like to be able to display a random blog entry into each, so that they don't duplicate and show the same three at any time. Ideally, I would like to be able to randomly generate say out of the last 10 posts, and pull the title to display as a title, with the feature image, and a small text overlay providing a few lines of text which is hyperlinked.

    With the help of a friend I have managed to create a plugin which pulls a random post title, which does not duplicate, and is hyperlinked. And I have managed to somewhat canabalise another plugin to be able to display this as a text overlay when you mouse over the image, however this would be better if there was a slightly transparent text box permanently over the image.

    It's taken me a long time to get to this stage, I have tried searching for a plugin that does this (something must exist!). But maybe I'm being too specific.

    Can anyone help with this? Like I said my knowledge is minimal, I can provide the code that has been written if necessary however I believe it's a simple database lookup and retrieval.

    Thanks in advance!

  2. Layser
    Member
    Posted 1 year ago #

    Actually a better way for me would be to pull the category as a title, as in "posted in: topic 1", and the title as the overlay over the feature image.

  3. Layser
    Member
    Posted 1 year ago #

    Here is the code currently written, but I need it to find the category to display as the header, and find the image to display as content background, then find the title to display as an overlay.

    <?php
    
    add_shortcode('rlink', 'writeRandomLink');
    global $researchlinks;
    
    function writeRandomLink($atts, $content) {
    	global $researchlinks;
    	if ($researchlinks == null) {
    		$researchlinks = array();
    		$researchlinks[] = 1;
    	} else if (count($researchlinks) == 4) {
    		unset($researchlinks);
    		$researchlinks[] = 1;
    	}
    
    	$args = array('orderby' => rand, 'showposts' => 1, 'post__not_in' => $researchlinks);
    	$researchpost = new WP_Query($args);
    
    	if ($researchpost -> have_posts()) : $researchpost -> the_post();
    		$output = "<div id=\"researchlink\">";
    		$output .= "<a href=\"";
    		$output .= get_permalink($post->ID);
    		$output .= "\" rel=\"bookmark\" title=\"";
    		$output .= htmlspecialchars(get_the_title());
    		$output .= "\">";
    		$output .= htmlspecialchars(get_the_title());
    		$output .= "</a>";
    		$output .= "</div>";
    		$researchlinks[] = get_the_ID();
    	endif;
    	return $output;
    }
    
    ?>

    Can anyone help expand?

Topic Closed

This topic has been closed to new replies.

About this Topic