• Resolved srpd

    (@srpd)


    Hi, i have a problem, i want to add a post loop in header in order to show the last new post in one singol category and then in the index the rest of.

    i’m trying by this code in header.php:

    <?php query_posts('cat=248&posts_per_page=1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div  id="post-<?php the_ID(); ?>">
    <?php the_content(''); ?>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    in this way category id is 248 and show only the last post.

    and in index i’m using this:

    <?php query_posts($query_string . "&cat=-248"); ?>
    
    <?php if (have_posts()) : ?>
    
    <?php $c = 0; ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <?php if ( !is_paged() && ++$c == 1 )  { ?>
    <div  id="post-<?php the_ID(); ?>">
    <?php the_content(''); ?>
    </div>
    <?php } else { ?>
    <div  id="post-<?php the_ID(); ?>">
    <?php the_content(''); ?>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    this code, in order to assign a different style to the last inserted post in category different from 248 just in homepage, but this doesn’t work. Post are showed correctly but it’s impossible to access to single post. any suggestion?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Maybe use a new WP_Query for that first loop–something like:

    <?php
    $cat_id = 248;
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thread Starter srpd

    (@srpd)

    hi MichaelH it works, really thanks, and there is a way to exclude category from single.php?
    thanks again 🙂

    Please better describe what you saying…

    Example: “When displaying a single post don’t display a post in the header that belongs to a certain category”

    Thread Starter srpd

    (@srpd)

    yes you are right, sorry.
    Ok i try to explain: i have category 248, and category’s post must be showed only in the header on blog not in single.php. post details must not be showed, when i click on permalink in header’s post i’ll see a page like an archive a list of post in 248 category and not a link to single post.
    uhmmmm sorry for my english, i hope you can understand what i’m saying 🙂

    I’m not sure this is what your are asking but this will not show the post in the header when displaying a single post view:

    <?php
    if (!is_single()) {
    $cat_id = 248;
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    
    ?>

    Conditional_Tags

    Thread Starter srpd

    (@srpd)

    thanks, but i don’t want to hide post in header when i’m displaying single.php but i need to exclude category 248 from list in single.php. for example when i’m reading other post there is a next post link that redirect to a 248 category post, and i want to hide this category from dirrect access to post…

    Thread Starter srpd

    (@srpd)

    really thanks for your help and for your patience 🙂

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘how to add loop in header’ is closed to new replies.