Extract Sticky Posts from _Index.php

Hey there,

I’m looking to do a loop above the main Loop on _Index.php, grabbing all Sticky posts. The container for those posts will be 100%; the remainder of the archive will be the normal width + a sidebar.

I’m look at code like this:

$sticky = get_option( 'sticky_posts' );
if(!empty($sticky)){
 $args = array(
    'posts_per_page' => -1, //get all post
    'post__in'  => $sticky, //are they sticky post
 );

 // The Query
 $the_query = new WP_Query( $args );

 // The Loop //we are only getting a list of the title as a li see the loop docs for details on the loop or copy this from index.php (or posts.php)
 while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
 }
 wp_reset_query(); //reset the original WP_Query
}

//now get the not sticky post
$args2 = array(
    'posts_per_page' => -1, //get all post
    'post__not_in'  => $sticky, //are they NOT sticky post
);
// The Query
$the_query2 = new WP_Query( $args2 );

// The Loop....

The above doesn’t look right, when compared with the _index.php file—seems too granular. I’d prefer not to break this child theme! Any assistance is appreciated.

Thanks.

Hi Luke,

Thanks for reaching out.

May I know what view you’re changing? It’s because index.php is a generic template used by many, blog index, archives, searches, and so on. And it appears you’re modifying it to display two queries and this seems overriding them all. Would you mind providing the entire content of your template file? And should it be the blog loop?

Thanks!

Hey Rad,

I haven’t implemented any code yet. If you take a look at https://recyclingdev.wpengine.com/news/, you’ll see what I’m trying to. Currently, it’s duct taped together with CSS (absolute positioning the Sidebar, lol) but would like to make it viable.

My thinking was that I could do something like:

<?php

// =============================================================================
// VIEWS/GLOBAL/_INDEX.PHP
// -----------------------------------------------------------------------------
// Includes the index output.
// =============================================================================

$stack = x_get_stack();

if ( is_home() ) :
  $style     = x_get_option( 'x_blog_style' );
  $cols      = x_get_option( 'x_blog_masonry_columns' );
  $condition = is_home() && $style == 'masonry';
elseif ( is_archive() ) :
  $style     = x_get_option( 'x_archive_style' );
  $cols      = x_get_option( 'x_archive_masonry_columns' );
  $condition = is_archive() && $style == 'masonry';
elseif ( is_search() ) :
  $condition = false;
endif;

?>

<?php if ( $condition ) : ?>

  <?php x_get_view( 'global', '_script', 'isotope-index' ); ?>

<div id="sticky-post-container">

<?php $sticky = get_option( 'sticky_posts' );
if(!empty($sticky)){
 $args = array(
    'posts_per_page' => -1, //get all post
    'post__in'  => $sticky, //are they sticky post
 );

 // The Query
 $the_query = new WP_Query( $args );

 // The Loop //we are only getting a list of the title as a li see the loop docs for details on the loop or copy this from index.php (or posts.php)
 while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo "title featured excerpt etc.";
 }
 wp_reset_query(); //reset the original WP_Query
}
?>
</div> 

  <div id="x-iso-container" class="x-iso-container x-iso-container-posts cols-<?php echo $cols; ?>">

    <?php if ( have_posts() ) : ?>
      <?php while ( have_posts() ) : the_post(); ?>
        <?php if ( $stack != 'ethos' ) : ?>
          <?php x_get_view( $stack, 'content', get_post_format() ); ?>
        <?php else : ?>
          <?php x_ethos_entry_cover( 'main-content' ); ?>
        <?php endif; ?>
      <?php endwhile; ?>
    <?php else : ?>
      <?php x_get_view( 'global', '_content-none' ); ?>
    <?php endif; ?>

  </div>

<?php else : ?>

  <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
      <?php x_get_view( $stack, 'content', get_post_format() ); ?>
    <?php endwhile; ?>
  <?php else : ?>
    <?php x_get_view( 'global', '_content-none' ); ?>
  <?php endif; ?>

<?php endif; ?>

<?php pagenavi(); ?>

But maybe there’s an easier way/place to implement this? I will just need this on the News page.

Thanks!

Hello Luke,

Editing the wp-content/themes/x-child/framework/views/global/_index.php file will not work out for you because all the contents of this file will already be inside the x-main element. The best place you insert your custom code will be in wp-content/themes/x-child/framework/views/renew/wp-index.php.

<?php

// =============================================================================
// VIEWS/RENEW/WP-INDEX.PHP
// -----------------------------------------------------------------------------
// Index page output for Renew.
// =============================================================================

get_header();

?>

  <div class="x-container max width offset">


  	<div id="sticky-post-container" class="mtm mbm">
  		<p>This will be fullwidth!!!!</p>
  	</div>



    <div class="<?php x_main_content_class(); ?>" role="main">

      <?php x_get_view( 'global', '_index' ); ?>

    </div>

    <?php get_sidebar(); ?>

  </div>

<?php get_footer(); ?>

Please note that custom coding is outside the scope of our support. Issues that might arise from the use of custom code and further enhancements should be directed to a third party developer.

We would love to know if this has worked for you. Thank you.

Ah, excellent. I will give this a shot and let you know!

Thanks.

No Problem.
If you need anything else we can help you with, don’t hesitate to open another thread.

Hey folks,

Figured it out and in the interest of helping out the next person, below is the code I ended up with.

I was able to tie into most of the X standard classes, which is pretty great, and wrote some new CSS flexbox stuff for the rest. One thing this code does not do is extract the Sticky posts from the regular feed—but you can display: none; those elements.

Enjoy!

<?php

// =============================================================================
// VIEWS/RENEW/WP-INDEX.PHP
// -----------------------------------------------------------------------------
// Index page output for Renew.
// =============================================================================

get_header();

?>

  <div class="x-container max width offset">

    <div id="sticky-post-container" class="sticky-flex-container">
      <?php

$sticky = get_option( 'sticky_posts' );
rsort( $sticky );

$args = array(
    'post__in' => $sticky,
    'posts_per_page' => 10
    );

$sticky_query = new WP_Query( $args );

while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>

              <div class="single-sticky-post">
                <div class="sticky-image">
                  <div class="entry-featured">
                          <a href="<?php the_permalink() ?>" class="entry-thumb" title="<?php the_title(); ?>" alt="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(full) ?>" class="attachment-entry size-entry wp-post-image" alt=""></a>
                  </div>
                  </div>
                <div class="sticky-headline"><h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3></div>
                <div class="sticky-entry">
  	               <?php the_excerpt(); ?>
                </div></div>
              <?php endwhile; ?>

        <?php rewind_posts(); ?>

</div>
    <div class="<?php x_main_content_class(); ?>" role="main">

      <?php x_get_view( 'global', '_index' ); ?>

    </div>

    <?php get_sidebar(); ?>

  </div>

<?php get_footer(); ?>

Glad it’s working now and thanks for sharing :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.