Forums

[resolved] Random posts with specified ID's of posts (12 posts)

  1. syed544
    Member
    Posted 1 year ago #

    I want to show 5 random posts from 2 categories (ID no. 4 & 7)
    for this I use this code :
    $rand_posts = get_posts('numberposts=5&orderby=rand&cat=4,7');

    It worked ofcourse.
    Now I want to give 10 specific ID's of posts instead of those two above mentioned categories.
    How can it be possible?

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    this might work:

    <?php $post_list = array( 1,1029,188,185,171,170,233,167,169,168);
    	shuffle($post_list);
    	$rand_list = array_slice($post_list, 0, 5);
    	$rand_posts = get_posts(array( 'include' => $rand_list, 'orderby' => 'rand' ));
  3. syed544
    Member
    Posted 1 year ago #

    It works like charm !!
    Thanks a lot.

  4. syed544
    Member
    Posted 1 year ago #

    one more question please:
    how can I get the author name (Display name) [with hyperlink] of the specific random post?

    I used this:
    <?php get_the_author_link(); ?>
    but it didnt work.

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    how did it not work?

    get_the_author_link() does not output anything; you would need to echo it:
    http://codex.wordpress.org/Function_Reference/get_the_author_link

    or you could try to use:
    http://codex.wordpress.org/Function_Reference/the_author_link

  6. syed544
    Member
    Posted 1 year ago #

    I have used both of these :

    <?php the_author_link(); ?>
    <?php echo get_the_author_link(); ?>

    but ...... not working !!
    The Author's display name doesn't show.

  7. alchymyth
    The Sweeper
    Posted 1 year ago #

    is anything showing?

    is the code used in the loop?

    as you are working with 'get_posts()', have you used 'setup_postdata()' in your code?
    http://codex.wordpress.org/Template_Tags/get_posts

    you could paste the full code of your template into a http://wordpress.pastebin.com/ and post the link to it here.

    can you post a link to your site to illustrate the iisue?

  8. syed544
    Member
    Posted 1 year ago #

    The code is for showing "Featured posts" from current month.
    I have pasted this code here at pastebin :
    Random posts with Title/Date/Author/Category links

  9. alchymyth
    The Sweeper
    Posted 1 year ago #

    after this line:

    foreach( $rand_posts as $post ) :

    insert:
    setup_postdata($post);

    if you read the 'get_posts()' link to the codex docu it is all shown in there.

  10. syed544
    Member
    Posted 1 year ago #

    Thanks !
    I have inserted that code as you advise & it works.

    But the Author name is without hyperlink. any suggestion?
    sorry to disturb you.

  11. alchymyth
    The Sweeper
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/the_author_link

    the_author_link() would link to the authors website -
    maybe none is entered, then there is no link.

    or where do you expect the link to link to?

    if you want the author's name to link to his/her posts, try to use:

    http://codex.wordpress.org/Function_Reference/the_author_posts_link

  12. syed544
    Member
    Posted 1 year ago #

    You really deserves an award for your nice helping nature. :)
    yeah, i mean the same, i.e.: author's name to link to his/her posts
    which i got from :
    <?php the_author_posts_link(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic