最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Create a page template for "top rated posts" but show full content and not just a list

programmeradmin3浏览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('.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('=<?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="/" 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(); ?>

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/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/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/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(); ?>
Share Improve this question asked Sep 24, 2020 at 0:50 Nate M.Nate M. 774 silver badges11 bronze badges 2
  • 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 Commented Sep 24, 2020 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. Commented Sep 25, 2020 at 22:24
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论