如何在wordpress的热门帖子中显示内容?

时间:2014-09-15 10:54:42

标签: php wordpress

我正在使用排名前10位的帖子插件。

我添加了以下几行,但它不显示......

$output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';

代码:

function tptn_pop_posts( $args ) {
    global $wpdb, $siteurl, $tableposts, $id, $tptn_settings;

    $defaults = array(
        'is_widget' => FALSE,
        'daily' => FALSE,
        'echo' => FALSE,
        'strict_limit' => FALSE,
        'posts_only' => FALSE,
        'is_shortcode' => FALSE,
        'heading' => 1,
    );
    $defaults = array_merge( $defaults, $tptn_settings );

    // Parse incomming $args into an array and merge it with $defaults
    $args = wp_parse_args( $args, $defaults );

    // OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
    extract( $args, EXTR_SKIP );

    if ($daily) {
        $table_name = $wpdb->prefix . "top_ten_daily";
    } else {
        $table_name = $wpdb->prefix . "top_ten";
    }

    $limit = ( $strict_limit ) ? $limit : ( $limit * 5 );

    $exclude_categories = explode( ',', $exclude_categories );

    $target_attribute = ( $link_new_window ) ? ' target="_blank" ' : ' ';   // Set Target attribute
    $rel_attribute = ( $link_nofollow ) ? ' nofollow' : ''; // Set nofollow attribute

    parse_str( $post_types, $post_types );  // Save post types in $post_types variable

    if ( ! $daily ) {
        $args = array();
        $sql = "SELECT postnumber, cntaccess as sumCount, ID, post_type, post_status ";
        $sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
        $sql .= "AND post_status = 'publish' ";
        if ( '' != $exclude_post_ids ) {
            $sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
        }
        $sql .= "AND ( ";
        $multiple = false;
        foreach ( $post_types as $post_type ) {
            if ( $multiple ) $sql .= ' OR ';
            $sql .= " post_type = '%s' ";
            $multiple = true;
            $args[] = $post_type;   // Add the post types to the $args array
        }
        $sql .= " ) ";
        $sql .= "ORDER BY sumCount DESC LIMIT %d";
        $args[] = $limit;
    } else {
        $current_time = current_time( 'timestamp', 0 );
        $current_time = $current_time - ( $daily_range - 1 ) * 3600 * 24;
        $current_date = date( 'Y-m-j', $current_time );

        $args = array(
            $current_date,
        );
        $sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status ";
        $sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
        $sql .= "AND post_status = 'publish' AND dp_date >= '%s' ";
        if ( '' != $exclude_post_ids ) {
            $sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
        }
        $sql .= "AND ( ";
        $multiple = false;
        foreach ( $post_types as $post_type ) {
            if ( $multiple ) $sql .= ' OR ';
            $sql .= " post_type = '%s' ";
            $multiple = true;
            $args[] = $post_type;   // Add the post types to the $args array
        }
        $sql .= " ) ";
        $sql .= "GROUP BY postnumber ";
        $sql .= "ORDER BY sumCount DESC LIMIT %d";
        $args[] = $limit;
    }

    if ( $posts_only ) {    // Return the array of posts only if the variable is set
        return apply_filters( 'tptn_pop_posts_array', $wpdb->get_results( $wpdb->prepare( $sql , $args ) , ARRAY_A ) );
    }

    $results = $wpdb->get_results( $wpdb->prepare( $sql , $args ) );

    $counter = 0;

    $output = '';

    $shortcode_class = $is_shortcode ? ' tptn_posts_shortcode' : '';
    $widget_class = $is_widget ? ' tptn_posts_widget' : '';

    if ( $heading ) {
        if ( ! $daily ) {
            $output .= '<div id="tptn_related" class="tptn_posts ' . $widget_class . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title );
        } else {
            $output .= '<div id="tptn_related_daily" class="tptn_posts_daily' . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title_daily );
        }
    } else {
        if ( ! $daily ) {
            $output .= '<div class="tptn_posts' . $widget_class . $shortcode_class . '">';
        } else {
            $output .= '<div class="tptn_posts_daily' . $widget_class . $shortcode_class . '">';
        }
    }

    if ( $results ) {
        $output .= apply_filters( 'tptn_before_list', $before_list );
        foreach ( $results as $result ) {
            $sumcount = $result->sumCount;
            $result = get_post( apply_filters( 'tptn_post_id', $result->ID ) ); // Let's get the Post using the ID

            $categorys = get_the_category( apply_filters( 'tptn_post_cat_id', $result->ID ) );  //Fetch categories of the plugin
            $p_in_c = false;    // Variable to check if post exists in a particular category

            foreach ( $categorys as $cat ) {    // Loop to check if post exists in excluded category
                $p_in_c = ( in_array( $cat->cat_ID, $exclude_categories ) ) ? true : false;
                if ( $p_in_c ) break;   // End loop if post found in category
            }

            $title = tptn_max_formatted_content( get_the_title( $result->ID ), $title_length );

            if ( ! $p_in_c ) {
                $output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );

                if ( 'after' == $post_thumb_op ) {
                    $output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
                    $output .= '<span class="tptn_title">' . $title . '</span>'; // Add title if post thumbnail is to be displayed after
                    $output .= '</a>'; // Close the link
                }
                if ( 'inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op ) {
                    $output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
                    $output .= tptn_get_the_post_thumbnail( array(
                        'postid' => $result->ID,
                        'thumb_height' => $thumb_height,
                        'thumb_width' => $thumb_width,
                        'thumb_meta' => $thumb_meta,
                        'thumb_html' => $thumb_html,
                        'thumb_default' => $thumb_default,
                        'thumb_default_show' => $thumb_default_show,
                        'thumb_timthumb' => $thumb_timthumb,
                        'thumb_timthumb_q' => $thumb_timthumb_q,
                        'scan_images' => $scan_images,
                        'class' => "tptn_thumb",
                        'filter' => "tptn_postimage",
                    ) );
                    $output .= '</a>'; // Close the link
                }
                if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
                    $output .= '<span class="tptn_after_thumb">';
                    $output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
                    $output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
                    // $output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
                    $output .= '</a>'; // Close the link
                }
                if ( $show_author ) {
                    $author_info = get_userdata( $result->post_author );
                    $author_name = ucwords( trim( stripslashes( $author_info->display_name ) ) );
                    $author_link = get_author_posts_url( $author_info->ID );

                    $output .= '<span class="tptn_author"> ' . __( ' by ', TPTN_LOCAL_NAME ).'<a href="' . $author_link . '">' . $author_name . '</a></span> ';
                }
                if ( $show_date ) {
                    $output .= '<span class="tptn_date"> ' . mysql2date( get_option( 'date_format', 'd/m/y' ), $result->post_date ).'</span> ';
                }
                if ( $show_excerpt ) {
                    $output .= '<span class="tptn_excerpt"> ' . tptn_excerpt( $result->ID, $excerpt_length ).'</span>';
                }
                if ( $disp_list_count ) $output .= ' <span class="tptn_list_count">(' . number_format_i18n( $sumcount ) . ')</span>';

                if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
                    $output .= '</span>';
                }

                $output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
                $counter++;
            }
            if ( $counter == $limit/5 ) break;  // End loop when related posts limit is reached
        }
        if ( $show_credit ) {
            $output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
            $output .= sprintf( 'Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/" rel="nofollow">Top 10 plugin</a>', TPTN_LOCAL_NAME );
            $output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
        }
        $output .= apply_filters( 'tptn_after_list', $after_list );
    } else {
        $output .= ( $blank_output ) ? '' : $blank_output_text;
    }
    $output .= '</div>';

    return apply_filters( 'tptn_pop_posts', $output );
}

2 个答案:

答案 0 :(得分:0)

您似乎正在返回$output。您需要使用echo才能显示HTML。

参考:http://php.net/manual/en/function.echo.php

答案 1 :(得分:0)

to show content in popular post by adding the following line below the code.
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';

top10.php (top10 popular post plugin)
code:
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
                    $output .= '<span class="tptn_after_thumb">';
                    $output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
                    $output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
                    $output .= '</a>'; // Close the link
                    $output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
                }