GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Wordpress pros inside pls: Random Post w/ thumbnails (https://gfy.com/showthread.php?t=1044531)

tonyparra 11-05-2011 03:52 AM

Wordpress pros inside pls: Random Post w/ thumbnails
 
Ive been trying to add random post with thumbnail to a blog, but cant get it to work, cant find a plugin :

PHP Code:

<ul>
<?php
$args 
= array( 'numberposts' => 5'orderby' => 'rand''post_status' => 'publish''offset' => 0);
$rand_posts get_posts$args );
foreach( 
$rand_posts as $post ) : ?>
    <li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><img src="<?php echo 

get_post_meta($post->IDthumbnailtrue); ?>" width="100" height="100">
</p></li>
<?php endforeach; ?>
</ul>

it spits out only the latest post and repeats it 5 times :helpme what the hall am i doing wrong???

tonyparra 11-05-2011 04:11 AM

tried this no luck

Code:

<li><h3>Some Random posts</h3>
 <ul>
 <?php $posts = get_posts(?orderby=rand&numberposts=5′); foreach($posts as $post) { ?>
 <li><a href=?<?php the_permalink(); ?>? title=?<?php  the_title(); ?>?><?php the_title(); ?></a>
 </li>
 <?php } ?>
 </ul>
 </li>


tonyparra 11-05-2011 05:19 AM

Quote:

Originally Posted by tonyparra (Post 18538445)
Ive been trying to add random post with thumbnail to a blog, but cant get it to work, cant find a plugin :

PHP Code:

<ul>
<?php
$args 
= array( 'numberposts' => 5'orderby' => 'rand''post_status' => 'publish''offset' => 0);
$rand_posts get_posts$args );
foreach( 
$rand_posts as $post ) : ?>
    <li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><img src="<?php echo 

get_post_meta($post->IDthumbnailtrue); ?>" width="100" height="100">
</p></li>
<?php endforeach; ?>
</ul>

it spits out only the latest post and repeats it 5 times :helpme what the hall am i doing wrong???

this one displays what i want but the permalink is all the same!!:mad::mad: i fucking hate you right now wordpress :disgust

fris 11-05-2011 05:20 AM

I did something like this recently, but i picked it via category.

tonyparra 11-05-2011 05:36 AM

Quote:

Originally Posted by fris (Post 18538500)
I did something like this recently, but i picked it via category.

can you enlighten me please :helpme

tonyparra 11-05-2011 07:26 AM

Quote:

Originally Posted by tonyparra (Post 18538499)
this one displays what i want but the permalink is all the same!!:mad::mad: i fucking hate you right now wordpress :disgust

Im sorry i luv you wordpress :upsidedow:pimp

Babaganoosh 11-05-2011 07:28 AM

Quote:

Originally Posted by tonyparra (Post 18538653)
Im sorry i luv you wordpress :upsidedow:pimp

Wordpress understands we all say things we don't mean when we're angry and it forgives you.

bpluva 11-05-2011 07:39 AM

Quote:

Originally Posted by tonyparra (Post 18538499)
this one displays what i want but the permalink is all the same!!:mad::mad: i fucking hate you right now wordpress :disgust

Try the code Below after while have_post the_post

<?php
global $wpdb;
$numposts = 32;
$rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT $numposts");
foreach($rand_posts as $post) :
setup_postdata($post);
?>
<?php endforeach; ?>

vdbucks 11-05-2011 09:17 AM

Here ya go.. the proper way to do it ><

For use as the main or only loop:
PHP Code:

<?php
$args1 
= array(
        
'posts_per_page'    => 5,
        
'orderby'        => 'rand',
        
'post_status'        => 'publish',
        
'offset'        => 0
    
);
query_posts($args1);

if (
have_posts()) : while (have_posts()) : the_post(); ?>
    <li>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><img src="<?php echo get_post_meta($post->IDthumbnailtrue); ?>" width="100" height="100"></p>
    </li>
<?php endwhile; endif; ?>

Use this as a secondary loop on a page with multiple loops... will save some resources by using the initial query from the main loop as opposed to creating a new query:
PHP Code:

<?php 
$args2 
= array(
        
'posts_per_page'    => 5,
        
'orderby'        => 'rand',
        
'post_status'        => 'publish',
        
'offset'        => 0
    
);
$rand_posts get_posts($args2);

foreach (
$rand_posts as $post) : setup_postdata($post); ?>
    <li>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><img src="<?php echo get_post_meta($post->IDthumbnailtrue); ?>" width="100" height="100"></p>
    </li>
<?php endforeach; ?>


fris 11-05-2011 08:09 PM

Quote:

Originally Posted by tonyparra (Post 18538516)
can you enlighten me please :helpme

Had someone that wanted to show recent photos from a category, (each photo represents a post *each image thumb taken from posts with attachments*.

he always had his galleries in 1 category, so he wanted a widget or a shortcode to show recent pics (galleries)

tonyparra 11-06-2011 12:49 PM

Quote:

Originally Posted by vdbucks (Post 18538791)
Here ya go.. the proper way to do it ><

Thanks i did get it to work :thumbsup

vdbucks 11-06-2011 06:07 PM

Quote:

Originally Posted by tonyparra (Post 18540660)
Thanks i did get it to work :thumbsup

If you want to add a category clause to it.. you could add something like the following to the args array...

via category id:
PHP Code:

$args1 = array(
        
'cat'            => 3,
        
'posts_per_page'    => 5,
        
'orderby'        => 'rand',
        
'post_status'        => 'publish',
        
'offset'        => 0
    
);
query_posts($args1); 

of course, you could also just use the category name like so:
PHP Code:

$args1 = array(
        
'category_name'        => 'cat_name_here',
        
'posts_per_page'    => 5,
        
'orderby'        => 'rand',
        
'post_status'        => 'publish',
        
'offset'        => 0
    
);
query_posts($args1); 

or my favorite way, by category slug because I don't like using uppercase letters and/or spaces in my queries:
PHP Code:

$Obj get_category_by_slug('cat_slug_here');
$catid $Obj->term_id;
$args1 = array(
        
'cat'             => $catid,
        
'posts_per_page'    => 5,
        
'orderby'        => 'rand',
        
'post_status'        => 'publish',
        
'offset'        => 0
    
);
query_posts($args1); 



All times are GMT -7. The time now is 03:24 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123