• Resolved tastymouse

    (@tastymouse)


    I have searched for hours and found several outdated solutions. I’m using WP 3.0 and Twenty Ten. I don’t want a sticky post on my front page, but in some of my category pages. How can I do this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • First, I would strongly suggest that you create a child theme before making any changes. If you do not, all your changes will be lost the next time Twenty Ten is upgraded.

    I assume that your category pages are already showing sticky posts. Is this true?

    To ignore sticky posts on the front page, you can modify your child’s index.php by changing this (UNTESTED – watch for typos):

    <?php
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    			 get_template_part( 'loop', 'index' );
    			?>

    to this:

    <?php
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    			if (is_front_page()) {
    				$args = array('caller_get_posts' => 1);
    				query_posts(array_merge(
    					$wp_query->query, $args));
    			}
    			 get_template_part( 'loop', 'index' );
    			?>
    Thread Starter tastymouse

    (@tastymouse)

    Hi, thanks. I get the following error: Warning: array_merge() [function.array-merge]: Argument #1 is not an array in wp/wp-content/themes/twentyten/index.php on line 36

    And yes, I know I’ll have to create a child theme, I still have to learn that.

    Just rename the twentyten folder to something else, like “twentyten-mod”. Also that error is just a warning and does not always mean that the code isn’t working. Is it doing what you want?

    Check to be sure you have the code correct for the query:

    query_posts(array_merge(
    	$wp_query->query, $args));

    If so, try adding the following line just above the query:

    global $wp_query;

    Thread Starter tastymouse

    (@tastymouse)

    I entered the correct code and it doesn’t work. The sticky post is not sticky in the category. Which seems logical to me since it makes the posts sticky on the front-page, not sticky in general.
    The error is probably because of another script which I use to show only posts of one category on the front-page.
    <?php
    if (is_home()) {
    query_posts(“cat=-5,-6,-7,-8”);
    }
    ?>

    I get the feeling that I do not have all the info I need to help.

    Lets start with the front page. You have two loops? What are the two queries: what do they do and what do you want to change?

    Thread Starter tastymouse

    (@tastymouse)

    What I want is:
    -A homepage with posts form one category only
    -Several pages, all displaying one category, which each have one sticky post

    For the homepage I added the above code to exclude the other categories.

    To show the stickies on the category pages, you need to modify the loop.php in your child theme. Change this:

    * Without further ado, the loop:
    	 */ ?>
    
    <?php while ( have_posts() ) : the_post(); ?>

    to this:

    * Without further ado, the loop:
    	 */ ?>
    
    <?php // Code to display sticky posts on category first page
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $stickies = array();
       if (is_category() && $paged < 2) {
          $stickies = get_option('sticky_posts');
          if ($stickies) {
             $sticky_posts = new WP_Query(array_merge($wp_query->query, array('post__in' => $stickies)));
             if ( $sticky_posts->have_posts() ) {
                while ( $sticky_posts->have_posts() ) {
                   $sticky_posts->the_post(); ?>
                   <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                   	<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                   	<div class="entry-summary">
                   		<?php the_excerpt(); ?>
                   	</div><!-- .entry-summary -->
                <?php }
                wp_reset_query();
             }
          }
       }
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    <?php if (in_array($post->ID,$stickies) ) continue; ?>
    Thread Starter tastymouse

    (@tastymouse)

    Thanks, this works. But now the sticky post also shows up on the front-page. How do I exclude it from the front-page?

    You showed code earlier where you exclude some categories from the front page. Change that from this:

    <?php
    if (is_home()) {
    query_posts("cat=-5,-6,-7,-8");
    }
    ?>

    to this:

    <?php
    if (is_home()) {
    $stickies = get_option('sticky_posts');
    $args = array(
       'cat' => '-5,-6,-7,-8',
       'post__not_in' => $stickies
    );
    query_posts($args);
    }
    ?>
    Thread Starter tastymouse

    (@tastymouse)

    Yes :)! This did it. Many thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Sticky within a category’ is closed to new replies.