0

Sorry upfront if I'm not explaining this clearly. I'm using WP-PostRatings and it has this function:

<?php if (function_exists('get_most_rated')): ?>
    <ul>
        <?php get_most_rated(); ?>
    </ul>
<?php endif; ?>

Throwing that code in a template page shows a linked list of the top posts by user ratings but it's not showing the full content of each post which is what I want. I believe I need to tap into the loop or create a custom query for this function?

For example, I made a custom page template to show a list of all my posts but doing this with a plugin's function is where I get lost. How do I wrap the following page template in with this get_most_rated(); function so it uses the WP-PostRatings function and displays the top rated post as full content?

<?php
  // set up or arguments for our custom query
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'post',
    'post_status'=>'publish',
    'posts_per_page' => 11,
    'paged' => $paged
  );
  // create a new instance of WP_Query
  $the_query = new WP_Query( $query_args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('group post-standard'); ?>>
    <div class="post-inner">
        <div class="post-thumbnail">
            <a href="<?php the_permalink(); ?>">
                <?php if ( has_post_thumbnail() ): ?>
                    <?php hu_the_post_thumbnail('beatpost-thumb'); ?>
                <?php elseif ( hu_is_checked('placeholder') ): ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/front/img/thumb-standard.png" alt="<?php the_title(); ?>" />
                <?php endif; ?>
                <?php if ( has_post_format('video') && !is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-play"></i></span>'; ?>
                <?php if ( has_post_format('audio') && !is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-volume-up"></i></span>'; ?>
                <?php if ( is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-star"></i></span>'; ?>
            </a>
            <?php if ( comments_open() && ( hu_is_checked( 'comment-count' ) ) ): ?>
                <a class="post-comments" href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i><?php comments_number( '0', '1', '%' ); ?></span></a>
            <?php endif; ?>
        </div><!--/.post-thumbnail-->
        
        <div class="post-content">
        
            <h2 class="post-title entry-title">
                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h2><!--/.post-title-->
            
            <div class="beat-share"><span class="beat-social-button-fb"><a href="javascript:void(0);" onclick="PopupCenter('https://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php the_title(); ?>', 'myPop1',531,545);"><i class="fa fa-facebook"></i></a></span> <span class="beat-social-button-twit"><a href="javascript:void(0);" onclick="PopupCenter('https://twitter.com/share?text=<?php the_title();?>&gt;&gt; Buy and Download Hundreds of Rap Beats&amp;via=RockItPro&amp;hashtags=buyrapbeats&amp;url=<?php the_permalink();?>', 'myPop1',539,253);"><i class="fa fa-twitter"></i></a></span></div>
            
            <div class="entry excerpt"><?php the_content(); ?></div>
            
            <div class="post-meta group">
                <p class="post-byline" style="display:none;"><span class="vcard author"><span class="fn"><a href="https://www.rockitpro.com/author/Sho-Down/" title="Posts by Sho-Down" rel="author">Sho-Down</a></span></span></p><p class="post-category"><?php the_category(', '); ?></p><p class="post-date date updated published"><i class="fa fa-angle-double-right"></i><?php the_time('m/d/Y'); ?><span class="anglemobile"><i class="fa fa-angle-double-right"></i></span></p><p class="cat-posts"><a href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i> <?php comments_number( '0', '1', '%' ); ?><span class="comment-text"> Comments</span></span></a></p>
            </div><!--/.post-meta-->
        </div><!--/.post-content-->
    </div><!--/.post-inner-->
</article><!--/.post-->

<?php endwhile; ?>

<div id="navigation">
<?php $big = 999999999; // need an unlikely integer
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'mid_size' => 2,
    'end_size' => 1,
    'total' => $the_query->max_num_pages
) ); ?>
</div>

<?php else: ?>
  <article>
    <h1>Sorry...</h1>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
  </article>
<?php endif; ?>

    </div><!--/.hu-pad-->
</section><!--/.content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • The get_most_rated(); function you list at the top is a function that resides in the plugin... ...the developer has decided that the function should only show some data. My advice is to pop open that plugin, find the code that generates that function and copy it over to your theme or your own plugin. Make your own function called get_most_rated_full(); based on the plugins function and look to change the_excerpt() to use the_content(). Or just look at how they query for the most rated posts, use their query and code your own output. – Tony Djukic Sep 24 at 15:19
  • @TonyDjukic Yea this is a bit confusing, I may need to hire someone to do this for me. These don't seem like normal function calls that I'm used to seeing. I found two instances of get_most_rated in link and link – Nate M. Sep 25 at 22:24
1

I opened up that plugin (WP-PostRatings) and located the get_most_rated() function.

It's in the following file: includes/postratings-stats.php

In that file, on line 57 you get the following:

foreach ($most_rated as $post) {
     $output .= expand_ratings_template($temp, $post, null, $chars, false)."\n";
}

From that, I went searching for the expand_ratings_template() function.

Found that in the following file: wp-postratings.php

In that file starting at line 1182 you get the following:

if ( strpos( $template, '%POST_EXCERPT%') !== false ) {
        if ( get_the_ID() !== $post_id ) {
            $post = get_post($post_id);
        }
        $post_excerpt = ratings_post_excerpt( $post_id, $post->post_excerpt, $post->post_content );
        $value = str_replace('%POST_EXCERPT%', $post_excerpt, $value );
    }
    if ( strpos( $template, '%POST_CONTENT%' ) !== false ) {
        if ( get_the_ID() !== $post_id ) {
            $post = get_post( $post_id );
        }
        $value = str_replace('%POST_CONTENT%', get_the_content(), $value );
    }
    if ( strpos( $template, '%POST_THUMBNAIL%') !== false ) {
        if ( get_the_ID() !== $post_id ) {
            $post = get_post( $post_id );
        }
        $value = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( $post, 'thumbnail' ), $value );
    }

What I see there is that it is actually looking for both the excerpt and the full post content which leads me to believe that there may be a setting somewhere in the plugin on what to display.

If it's not an option then check your posts to see if they have a manual 'excerpt' set... ...it may be checking if you specified an 'excerpt' and using that.

If you need to change the way this functions you're best off cloning those two functions, adding them to your theme's functions.php file naming them something different and then using those instead of the plugin's outputs.

| improve this answer | |
  • Thanks for looking into it. The plugin has a "template" settings page but very minimal customization, here's the top part of the page snipboard.io/LCHnXE.jpg and then at the bottom is this setting's box snipboard.io/zOSJbT.jpg but when I add %POST_CONTENT% to that setting box and then add this code to my custom template page <?php if (function_exists('get_most_rated')): ?><ul><?php get_most_rated(); ?></ul> <?php endif; ?> it's not showing the full content of the post. – Nate M. Sep 27 at 4:22
  • It may be because you’re placing the post content in a ‘<span>’ tag. That may be messing things up. Post content will output full HTML so place it in a ‘<div>’ instead and see if that helps. If that doesn’t work, copy/paste your settings somewhere and just try testing it out by using just %POST_CONTENT% and see what result that provides. Looks like the facility is there to accomplish what you want, just a matter of fiddling with the plugin settings until it works. – Tony Djukic Sep 27 at 4:27
  • Putting it in a div didn't work, the %POST_EXCERPT% works but not %POST_CONTENT% which is weird. I guess I'll do some digging and mess around with the code until I can figure it out or give up on it lol. Thanks for the help. – Nate M. Sep 27 at 4:45

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.