WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude sticky posts at the top (10 posts)

  1. breathe-out
    Member
    Posted 1 year ago #

    I want to make sticky posts behave like normal posts, my php skills are pretty bad so i hope someone out here could help me with that. From what I understand I have to edit the index.php file from the theme I'm using, if this is not correct please let me know.

    This is the unedited index.php file from my theme

    <?php
    /**
     * The main template file.
     *
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists.
     * Learn more: http://codex.wordpress.org/Template_Hierarchy
     *
     * @package Skylark
     * @since Skylark 1.6
     */
    		$latest_sticky_id = array();
    
    get_header(); ?>
    
    		<div id="primary" class="site-content">
    			<div id="content" role="main">
    
    			<?php if ( have_posts() ) : ?>
    
    				<?php /* Start the Loop */ ?>
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<?php
    						/* Include the Post-Format-specific template for the content.
    						 * If you want to overload this in a child theme then include a file
    						 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
    						 */
    						get_template_part( 'content', get_post_format() );
    					?>
    
    				<?php endwhile; ?>
    
    				<?php skylark_content_nav( 'nav-below' ); ?>
    
    			<?php elseif ( current_user_can( 'edit_posts' ) ) : ?>
    
    				<?php get_template_part( 'no-results', 'index' ); ?>
    
    			<?php endif; ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary .site-content -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Someone overhere said he has found a solution, the problem is i have no clue how to implement it.

    The suggested code that should work according to the person who opened the a thread about this.

    <?php
    $i = 1;
    if ( !empty($_GET['sort']) ) {
      $orderby = trim($_GET['sort']);
      $order = trim($_GET['order']);
      $key = trim($_GET['key']);
    }
      query_posts($query_string . '&caller_get_posts=1&orderby=' . $orderby . '&meta_key=' . $key . '&order=' . $order);
    
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php
      if ( $i % 2 == 0 ) { $alt = " class=\"alt\""; } else { $alt = " class=\"no\""; }
      echo "<div" . $alt;
      if ( is_sticky() ) { echo " id='sticky' "; } echo ">";
    ?>
    
      <?php the_title(); ?>
    
    <?php endwhile; endif;
      wp_reset_query();
    ?>

    Ive tried to copy the code shown above (solution code) over <?php if ( have_posts() ) : ?> without a succes -> error about unexpected endwhile. Then I removed one of the endwhiles in that example, after that i ended up with only showing the sticky post and not the others.

    can someone please help me, I'm very close now to finishing my blog.

    thanks in advance!

  2. pixelrow
    Member
    Posted 1 year ago #

    Can you please post your site address - Then it would be easier to see what you are asking.

    I am not sure sticky posts are your problem. Also which theme are you using.

  3. breathe-out
    Member
    Posted 1 year ago #

    My website: breathe-out
    theme: skylark

    To be able to show the posts in the slider you have to make them sticky + use a featured image. Also in the demo version the content of the slider are the top posts.

    thanks in advance

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Have you tried asking on http://help.blankthemes.com/

  5. breathe-out
    Member
    Posted 1 year ago #

    Sure have, you can see my question in their question section. But there is little to no activity. Have posted the question 2 days ago, but no response. Also they state that if you want code help, you probably have to pay them ;)

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    I'm sorry but as you are using what appears to be a commercially supported theme, you do need to seek support from the theme's developers. It is not our place to deprive theme developers of any income that they may derive from support services.

  7. breathe-out
    Member
    Posted 1 year ago #

    Hmm oke so I can't get any help here.

  8. alchymyth
    Forum Moderator
    Posted 1 year ago #

    even if you use a commercial supported theme, you can still get general support here;

    to 'remove' sticky posts from a loop, you need to edit the query parameters;

    http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters
    http://codex.wordpress.org/Class_Reference/WP_Query#Sticky_Post_Parameters

    totally untested, add this before the start of the loop, i.e. before this line <?php if ( have_posts() ) : ?>:

    <?php global $query_string; query_posts( $query_string . '&ignore_sticky_posts=1'); ?>

  9. breathe-out
    Member
    Posted 1 year ago #

    awesome! it worked, what only a small line of code can do, thank you very much!

  10. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    Follow-up: please do not use query_posts(). Filter the query via pre_get_posts instead. For example (this goes in functions.php):

    function breatheout_filter_pre_get_posts( $query ) {
        if ( $query->is_main_query ) {
            $query->set( 'ignore_sticky_posts', '1' );
        }
    }
    add_action( 'pre_get_posts', 'breatheout_filter_pre_get_posts' );

    Then, remove any references to query_posts() in your template files.

    Using this method will avoid stomping on the main query, and will preserve proper loop pagination.

Topic Closed

This topic has been closed to new replies.

About this Topic