Forums

[resolved] Can I include the sidebar in header.php? (5 posts)

  1. sigdrifa
    Member
    Posted 2 years ago #

    Hi,

    I'm writing a new theme. The layout is:
    Sidebar floating to the left
    A container with the header and the content floating next to it.

    The markup:

    <div id="sidebar" style="float:left"></div>
    <div id="main" style="float:left">
    <div id="header>Blog title goes here</div>
    <!-- end of header.php -->
    <!-- begin index.php -->
    <div id="content">The Loop goes here</div>
    <!-- end index.php -->
    <!-- begin footer.php -->
    </div>

    This works fine on the main index, but it doesn't work for pages and single articles. Whenever I click a link for an article or a page it would only show the contents of the latest post instead of the actual article or page.

    When I moved the sidebar to footer.php and floated everything to the right it suddenly worked.

    Does that mean I can't include the sidebar in the header? Or is it something else I'm missing here?

  2. alchymyth
    The Sweeper
    Posted 2 years ago #

    it probabaly depends on what you are running in the sidebar.

    do you have anything there that is using queries or loops, such as 'recent post' widgets?

    often themes are coded on purpose (for SEO for instance) to run the main loop first, and then sidebars etc.

    and for the same reason, unfortunately some plugins and widgets don't take too much care not to disturb any query.

  3. sigdrifa
    Member
    Posted 2 years ago #

    I have a recent post list in there:

    <ul class="toplevel">
    
    <li><span class="heading">Neueste Beitr&auml;ge</span></li>
    	<?php $posts = get_posts('numberposts=10&orderby=post_date'); foreach($posts as $post) : setup_postdata($post); ?>
    	<li class="recent">
    		&raquo; < a  href="<?php the_permalink(); ?"><?php the_title(); ?>< /a >
    
    	<?php endforeach; $post = $posts[0]; ?>

    Note: I had to put the space around the a-tag to prevent it from being converted into a link.

    This is the first thing in my sidebar.

  4. alchymyth
    The Sweeper
    Posted 2 years ago #

    i guess, this $post = $posts[0]; could be the reason you only get the latest post on all other pages.

    maybe you could change the name in your code?

  5. sigdrifa
    Member
    Posted 2 years ago #

    Unfortunately I didn't write that piece of code, and I can't remember where I found it. I really don't have a clue of how this works.

    I'm simply sticking with including the sidebar in the footer after the "main" div and floating everything to the right, which works just fine.

    But still, thanks for the reply; at least I now know the likely cause.

Topic Closed

This topic has been closed to new replies.

About this Topic