• I have a loop:

    <div id="custom">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if (!in_category('3')) continue; ?>
    <?php the_content(); ?>
    <hr />
    <?php endwhile; else: ?>
    <p><?php _e('No announcements'); ?></p>
    <?php endif; ?>
    test
    </div>

    that shows only posts from category 3 (announcements) on this div with an id of “custom” that shows on top. This code is inserted into header.php

    With index.php, archive.php, search.php I can see this BUT NOT with page.php or single.php. I dunno why since I can see <div id=”custom”> and </div> in the html.

    And the test is to test if there’s anything wrong with the code. Apparently in index.php, archive.php and search.php I can see the announcement, followed by a hr and a test.

    Whereas for the rest I can see a test only.

    Help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi – the problem is the query you have in your header is not always asking for what you think it is asking for.

    When you are on single.php or page.php, the default query is for only one post, the post that is going to display on that “single” or “page” page. Thus the posts in category 3 are not included in those query results, so they don’t display.

    Try this code in your header instead:

    <div id="custom">
    <?php
    $cat3Posts = new WP_Query();
    $cat3Posts->query('cat=3');
      while ($cat3Posts->have_posts()) : $cat3posts->the_post();
         the_content(); ?>
      <hr />
    <?php endwhile; ?>
    test
    </div>

    also take a look at
    https://codex.www.ads-software.com/Template_Tags/query_posts

    Thread Starter gameboyz

    (@gameboyz)

    Great, I reinstalled wordpress because of this LOL

    So there goes all my edits to the core files and CSS including customizing my plugins …

    Crap ??

    update: I got this – Fatal error: Call to a member function the_post() on a non-object in /home/fabcode/public_html/wordpress/wp-content/themes/gameboyz/header.php on line 92

    Thread Starter gameboyz

    (@gameboyz)

    Tried `<div id=”custom”>
    <?php
    if (is_home()) {
    query_posts(“cat=3”);
    }
    ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if (!in_category(‘3’)) continue; ?>
    <?php the_content(); ?>
    <hr />
    <?php endwhile; else: ?>
    <p><?php _e(‘No announcements’); ?></p>
    <?php endif; ?>
    </div>`

    but then only posts from cat3 are displayed on the front page :O

    Hi

    The error in the original code I gave you is this:

    I said $cat3posts->the_post();
    It should be $cat3Posts->the_post();
    capital letter P

    sorry

    Thread Starter gameboyz

    (@gameboyz)

    Fixed.

    header.php:

    <div id="custom">
    <?php query_posts('cat=3&amp;showposts=3'); ?>
    <?php $posts = get_posts('category=3&amp;numberposts=3&amp;offset=0');
    foreach ($posts as $post) : start_wp(); ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>
    
    </div>

    index.php:

    <?php
    query_posts('showposts=10');
    ?>

    single.php:

    <?php
    
    function selfURL() {
    $s = empty($_SERVER["HTTPS"]) ? ''
    : ($_SERVER["HTTPS"] == "on") ? "s"
    : "";
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? ""
    : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2) {
    return substr($s1, 0, strpos($s1, $s2));
    }
    
    $url=selfURL();
    $postslug=substr($url,43);
    $postslug=substr_replace($postslug ,"",-1);
    query_posts('name='.$postslug);
    ?>

    Brilliant! Free for all to use.

    But page.php gives Not found error .

    Thread Starter gameboyz

    (@gameboyz)

    Thanks stvwlf…

    Your small chunk of code solves everything rather than adding one complex chunk of code to single.php and stuff like that.

    So it works now no problem! ^.^

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘The loop doesn’t work on some pages’ is closed to new replies.