• Resolved blogger1234

    (@blogger1234)


    I have been tweaking a theme that I like, but I have come to a point that I need someone’s help. Currently, my sidebar is custom made by the original designer and shows three boxes: tags, recent comments, and “Popular” posts (popular is defined by how many comments are on a given post).

    I want to change the “Popular” box to show the most recent posts published in order. In the theme editor, the sidebar file has a div id – “Popular”, which I believe is getting instructions from the themes function file. From the themes function file, I found this:

    <?php
    function popularPosts($num) {
    global $wpdb;

    $posts = $wpdb->get_results(“SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num”);

    foreach ($posts as $post) {
    setup_postdata($post);
    $id = $post->ID;
    $title = $post->post_title;
    $count = $post->comment_count;

    if ($count != 0) {
    $popular .= ‘

    Can anyone tell me what I need to change to make this function collect and display the most recent posts? Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • vtxyzzy

    (@vtxyzzy)

    This is UNTESTED, but should be close:

    <?php
    function popularPosts($num) {
    global $wpdb;
    
    $posts = $wpdb->get_results("SELECT ID, post_title, post_date FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= NOW() ORDER BY post_date DESC LIMIT 0 , $num");
    
    foreach ($posts as $post) {
    setup_postdata($post);
    $id = $post->ID;
    $title = $post->post_title;
    
    $popular .= ' 
    
    ';
    $popular .= '' . $title . ' ';
    $popular .= '
    ';
    }
    return $popular;

    It will need some modification if you want to make the titles a link to the post.

    Thread Starter blogger1234

    (@blogger1234)

    Unfortunately, I get a:

    Parse error: syntax error, unexpected T_STRING in /home/content/17/5686317/html/wp-content/themes/aparatus/functions.php on line 244

    When I try that. Any other ideas? Thanks again for providing your thoughts.

    Tyler

    vtxyzzy

    (@vtxyzzy)

    You posted a partial listing of the popularPosts function. Did you replace the part you posted with what I posted, or did you replace the whole function?

    Thread Starter blogger1234

    (@blogger1234)

    Probably only part… it’s tricky because it is part of a much larger function. The created (who is rather busy) of the theme suggested this, but it doesn’t work either:

    I wouldn’t mess with the popularPosts function – since that was built for specifically that. If you want to pull recent posts – go in the tabbed-container.php and take out

      <?php echo popularPosts(10); ?>

    replace with:

      <?php global $post; $myposts = get_posts(‘numberposts=10′); foreach($myposts as $post) : setup_postdata($post); ?>

    • “><?php the_title(); ?>
    • <?php endforeach; ?>

    He is talking about putting it here:

    <div id=”tabbed-container”>

    <div id=”myTabs” class=”mootabs”>
    <ul class=”mootabs_title”>
    <li title=”Popular”>Popular
    <li title=”RecentComments”>Recent Comments
    <li title=”Tags”>Tags

    <div id=”Popular” class=”mootabs_panel”>

      <?php echo popularPosts(10); ?>

    </div><!–popular–>

    <div id=”RecentComments” class=”mootabs_panel”>

    <?php my_rec_comments(7); ?>

    </div>
    <div id=”Tags” class=”mootabs_panel”>

    <?php wp_tag_cloud(); ?>
    </div>
    </div>
    </div><!–tabbed-container–>

    If its useful to you, the site I’m working on is: https://seattlemiscellany.com/

    And it is the “Popular” tab on the right I want to be most recent. Thanks again for following up with me.

    vtxyzzy

    (@vtxyzzy)

    Looks like your developer’s suggestion is correct. Did you get it fixed?

    Thread Starter blogger1234

    (@blogger1234)

    Unfortunately not. I get a

    Parse error: syntax error, unexpected ‘=’ in /home/content/17/5686317/html/wp-content/themes/aparatus/tabbed-container.php on line 12

    message then preview box stops working.

    vtxyzzy

    (@vtxyzzy)

    Why don’t you paste the entire tabbed-container.php in wordpress.pastebin.ca, and post the link to it here so we can see all of the code.

    Thread Starter blogger1234

    (@blogger1234)

    With the suggested change: https://wordpress.pastebin.ca/1821597

    vtxyzzy

    (@vtxyzzy)

    OK, two little problems:

    1. There is an extra opening <ul> right after <div id="Popular" class="mootabs_panel">. Just delete it.
    2. The first tick mark after get_posts is not an apostrophe, it is a backtick. Change to an apostrophe.

    That should fix it.

    Thread Starter blogger1234

    (@blogger1234)

    That did the trick! Thanks again for your help, I really appreciate it. It’s been a long time since I’ve been comfortable with HTML and all its changes.

    vtxyzzy

    (@vtxyzzy)

    Glad it worked! Now, please use the dropdown at top right to mark this topic ‘Resolved’.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Widget commands’ is closed to new replies.