• Resolved hinhthoi

    (@hinhthoi)


    Hi,

    I want to display some posts that were posted previously on the new post. So, every time i create a new post, the previous posts (say 5 posts) are displayed at the end of the new post. These 5 post are selected randomly from the previous posts.

    So, is this possible?

Viewing 15 replies - 1 through 15 (of 17 total)
  • It is possible, it depends on comfortable you are with PHP and wordpress.

    You could write a simple function (it shows 5 random posts in a bullet point list):

    <ul>
    <?php
    $postargs=array(
    'showposts' => 5,
    'orderby' => rand
    );
    
    $random_posts = new WP_Query($postargs);
    while ($random_posts->have_posts()) :
    $random_posts->the_post();
      echo '<li>'. get_the_title() .'</li>'
    endwhile;
    ?>
    </ul>

    If you’re not familiar with editing themes, try searching for a related posts plugin.

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi Harmck
    Thank you very much for your codes. Could you please let me know where should i insert the code.
    I cannot use a related post plugin because these posts are not related to each other, they are just like a dictionary website. The purpose of my question is to create a post and try to related to ITS previous post so that google can understand the keywords for the previous posts.
    Thank you very much.

    Thread Starter hinhthoi

    (@hinhthoi)

    I guess i have to post the above codes into my single.php right?

    I have one more question i need your help:
    In the above code, the 5 random posts have the anchor text exactly the same with the posts’ titles (From my understanding, i have very limited knowledge in php)

    My URL structure is like this:

    http://mydomain.com/keyword1-this-is-information
    http://mydomain.com/keyword2-this-is-information
    http://mydomain.com/keyword3-this-is-information
    http://mydomain.com/keyword4-this-is-information
    http://mydomain.com/keyword5-this-is-information

    The post titles are correspondingly the same (e.g Keyword1 This Is Information, Keyword2 This Is Information, Keyword3 This Is Information, Keyword4 This Is Information, Keyword5 This Is Information)

    Now i want the anchor text is only Keyword1, Keyword2,… but ignore the “this is information”

    Is it possible to do this?

    Thanks!

    Thread Starter hinhthoi

    (@hinhthoi)

    I have just tried adding it in my single.php in one of my WP but it does not work, it shows the error

    Parse error: syntax error, unexpected T_ENDWHILE, expecting ‘,’ or ‘;’ in /home/bestco13/public_html/healthyhow.net/wp-content/themes/swift/single.php on line 52

    In your code is there any spelling mistake? I notice there is something like “new WP_Query”. Should it be “new_WP_Query”?

    Can you let me know where should i put the code? I want to put it at the end of the post. My whole single.php is:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    echo '<li>'. get_the_title() .'</li>'

    Should be:

    echo '<li>'. get_the_title() .'</li>';

    It’s missing the semi colon, I modified it from existing code removing stuff you don’t need. There should be no other problems. You’ll have to add the link using the permalink function. But that should be straightforward.

    Correct, It should go somewhere in your single php. Where exactly though depends entirely on the theme you use. Try finding where the end of your post is output and put it after that. Your code was removed as it was too long. Do not paste full file code into here, use Pastebin as suggested by the mods.

    With regards to your URL structure. Please clarify, the “this is information” changes per keyword, yes?

    You can split the text by say using a specific character such as a space and always display the first word which is the keyword. As long as the keyword is never two words this would be a relatively easy thing to do. Let me know if this is what you need.

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi Harmck

    Now the code does not give error, but it only loads the title of the 5 random posts, not the clickable link.

    With regard to the keyword, keyword1, keyword 2… may comprise 1 or several words, but the “this is information” are the same for all posts.

    Yesterday my home internet was down, so only now i can go to work to check your messages. After inserting the code, my single.php looks like this

    http://pastebin.com/kQA0rg9Q

    Could you help me with the keywords problem and the clinkable link problem?

    Thank you so much

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi, i use pastebin and follow the instruction in the forum rule, but the codes do not appear in my post.

    Thread Starter hinhthoi

    (@hinhthoi)

    “You’ll have to add the link using the permalink function. But that should be straightforward”

    What do you mean by this? My permanent link structure is /%postname%/

    By the way, the website i install to test the code is http://healthyhow.net, the 5 random posts are at the end of the post, but they are not clickable.

    My actual website that i want to add your code after i know that it works perfectly is http://syndromes.howhubs.com

    Thanks!

    To add the links using the permalink function, I meant change:

    echo '<li>'. get_the_title() .'</li>';

    To this:

    echo '<li><a href="'.get_permalink().'">'. get_the_title() .'</a></li>';

    So I described adding the simple a HTML tag around the text that is displaying.

    As for your ‘This is Information’ problem, if you want to hide it, I would say delete it. Unfortunately I don’t have the time to code this for you.

    Good luck with your website

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi Harmck

    Thank you so much for your help. By the way, is there any thanks button that i can press to thank you?

    Thanks!

    Thread Starter hinhthoi

    (@hinhthoi)

    It works perfectly. Now i have clikable links!

    No problem, marking it as resolved and you just saying it was thanks enough.

    Thread Starter hinhthoi

    (@hinhthoi)

    Hi Harmck

    Sorry to trouble you again. After searching a lot, i cannot find anything related to the keywords problem.

    As you mentioned above, getting the first word of the title is doable, could you give me the code to get the first word? Since quite a lot of keywords are only 1 word. And this may be also a clue for me to search more, since i’m not sure how to search about this topic in google.

    Thanks for your kind help!

Viewing 15 replies - 1 through 15 (of 17 total)

The topic ‘how to display previous posts’ is closed to new replies.