0

I am trying to display the numerical post order ranking on the home page, however, I am using template_parts and am not aware of how to do this when the html is separated from the wordpress loop.

Here's what I want to see:

ranking concept

Here is my loop in index.php

<div class="most-recent-feed">
    <?php
    if ( have_posts() ) :
        if ( is_home() && ! is_front_page() ) : ?>
        <header>
            <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
        </header>
        <?php
        endif;
        $post_ranking = 1;
        /* Start the Loop */
        while ( have_posts() ) : the_post();
            ?> <h1> <?php echo $post_ranking ?> </h1> <?php
            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override 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( 'template-parts/content', get_post_format() );
        endwhile;
        the_posts_navigation();
    else :
        get_template_part( 'template-parts/content', 'none' );
    endif; 
wp_reset_query();

?>
</div>

And here is my relevant html in template_parts/content.php (note the comment where the ranking number should be placed:

<div class="album-dark-overlay play">
    <div class="ranking-triangle">
    <!-- Ranking number should be placed here -->
    </div>
    <div id="<?php echo $youtube_id ?>" data-youtube-id="<?php echo $youtube_id ?>" class="media-circle">
        <i class="icon ion-ios-play"></i><i class="icon ion-pause"></i>
    </div>
</div>
1

It is not straightforward to pass a variable to a template part. However, when you are in a loop, WP has a counter called current_post that you can use in this way in your template part:

global $post;
$ranking = $post->$current_post + 1; // +1 because the counter starts at 0
  • I guess I'm just sort of confused on how I'm able to relate this $ranking variable that is initialized in my index.php to the html that is found in content.php @cjbj – Jordan Lewallen Jan 21 '17 at 4:22
-1

There are two ways I suggest you do that:

  • You can store the post rankings in your database wp_postmeta or any table of your choice, and then retrieve the value via WP_QUERY or any of your choice, which will then be echoed in your desired location.

OR

  • You can use PHP for loop. Get the length of the posts (posts_per_page) and then iterate through each of your result, which will then echo the ranking based on the first-second-third-fourth etc.

Here's an example:

// Get the length of the items (which is determined by the number you enter in 'posts_per_page'
$loop_length = 6 // using this as an example
for( $i = 0; $i <= $loop_length; $i++ ) {
  echo $i;
}
// Your result will be 1, 2, 3, 4, 5, 6.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.