Forums

Asides break all pages other than Home Page (10 posts)

  1. obeattie
    Member
    Posted 6 years ago #

    Hi there, I am integrating asides into my theme using the following code:

    <?php
    if ($posts) : foreach ($posts as $post) : start_wp();
    ?>
    <?php if (in_category(2)) { ?>
    <?php static $sidebarasidescount = 0;
    if ($sidebarasidescount == "5") { break; }
    else { ?>

  2. <div class="asidebody"><?php echo $post->post_excerpt ?> <?php the_content(); ?></div>
    <small class="asidemeta"><span class="asidesmeta-commentno"><?php comments_popup_link(__('(0)'), __('(1)'), __('(%)')); ?></span> -
    <span class="asidesmeta-datetime"><?php the_time('D-M-Y / g a'); ?></span><span class="asidesmeta-editlink"><?php edit_post_link('Edit', ' — '); ?></span></small>
  3. <?php $sidebarasidescount++; } ?>
    <?php } ?>

    <?php endforeach; else: ?>
    <?php _e('

  4. Sorry, no posts matched your criteria.'); ?>
  5. <?php endif; ?>

    Where am I going wrong?

    If anyone could help me it would be fantastic,
    Many Thanks in Advance,
    Oli

    P.S. If you would like to see my prob 1st hand the URL is http://obeattie.com/dev/one/

  • Michael Bishop
    Forum Concierge
    Posted 6 years ago #

    If I'm understanding your code, you are listing these in a sidebar. If so, why not simply use a get_posts?
    <h2>Asides</h2>
    <ul>
    <?php
    $posts = get_posts('category=2&numberposts=5');
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    This is just an example, if you want the whole post, you can add the_content, etc.
    More examples Template_Tags/get_posts

  • obeattie
    Member
    Posted 6 years ago #

    Many thanks. I will try ASAP.

  • obeattie
    Member
    Posted 6 years ago #

    Hi There,

    That code was great. However, when I try to add the aside content and info by using:

    <div class="asidebody"><?php the_content(); ?></div>
    <small class="asidemeta">
    <span class="asidesmeta-commentno"><?php comments_popup_link(__('(0)'), __('(1)'), __('(%)')); ?></span>
    -
    <span class="asidesmeta-datetime"><?php the_time('D-M-Y / g a'); ?></span>
    <span class="asidesmeta-editlink"><?php edit_post_link('Edit', ' — '); ?></span></small></li>

    I get the following error

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND comment_approved = '1'' at line 1]
    SELECT COUNT(comment_ID) FROM wp_comments WHERE comment_post_ID = AND comment_approved = '1';

    Which you will be able to see on the site. What am I doing wrong?

  • obeattie
    Member
    Posted 6 years ago #

    OK So now by replacing the_content with the_excerpt I do get the content.. now the error seems to be in the meta section.

  • Michael Bishop
    Forum Concierge
    Posted 6 years ago #

    Hmm. I'm guessing it's because the comments all need to be within the loop, and the get_posts is just a modified loop, and doesn't pull those in.
    I didn't pay attention to all of your original code, and since most asides I've seen don't show comment counts, I didn't think about that caveat. If you want to do show them, then you will need to go back to the original code you were using, and find the problem there.
    Much apologies.

  • obeattie
    Member
    Posted 6 years ago #

    Is there a way of displaying comment count outside the loop?

  • obeattie
    Member
    Posted 6 years ago #

    OK.. I am using the_excerpt, but it strips all links from my asides. which slightly defeats the purporse... is there a way of using the_content outside of the loop or modifying the_excerpt so it shows links?

  • obeattie
    Member
    Posted 6 years ago #

    Found it! Needed a <?php rewind_posts(); ?>!

    Thanks for all your help so far everyone!

  • obeattie
    Member
    Posted 6 years ago #

    Came across more problems so just put in a <?php if(is_home()) { ?> to stop it showing in other pages.

    Thanks for all your kind help,
    Oliver

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags