WordPress简码以显示相关帖子

时间:2019-01-27 06:19:46

标签: php jquery wordpress post

WordPress短代码以显示相关帖子

如何在WordPress中手动显示相关帖子

这是我的代码:

function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
    'limit' => '5',
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {
    $retval = '<ul>';
    // Get tags
    $tags = wp_get_post_tags($post->ID);
    $tagsarray = array();
    foreach ($tags as $tag) {
        $tagsarray[] = $tag->term_id;
    }
    $tagslist = implode(',', $tagsarray);

    // Do the query
    $q = "SELECT p.*, count(tr.object_id) as count
        FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id  = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
            AND p.post_status = 'publish'
            AND p.post_date_gmt < NOW()
        GROUP BY tr.object_id
        ORDER BY count DESC, p.post_date_gmt DESC
        LIMIT $limit;";

    $related = $wpdb->get_results($q);
    if ( $related ) {
        foreach($related as $r) {
            $retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>';  
   }
    } else {
        $retval .= '<li>No related posts found</li>';
    }
    $retval .= '</ul>
  ';
    return $retval;
    }
    return;
 }
 add_shortcode('related_posts', 'related_posts_shortcode');

它没有我想要的。我在做什么错了?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用WP类查询获取自定义循环 wp query classShortcode API

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

plt.xlabel('Distance')
plt.ylabel('Height')
plt.title('Object Trajectory \n')

plt.legend(loc="upper right", markerscale=4, fontsize=10)
plt.grid()

text=ax.text(3,1,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
text2=ax.text(0,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

def init():
    ax.set_xlim(0,10)
    ax.set_ylim(0,10)
    return text,text2

def update(frame):        
    #Moving a text
    text=ax.text(3,1+(int(frame))/30,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
    text2=ax.text(0+(int(frame))/30,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

    return text,text2

anim = animation.FuncAnimation(fig, update, init_func=init, frames=120, interval=10, blit=True)

anim.save('try_animation.mp4',dpi=160,fps=30, writer="ffmpeg")

plt.show()