douejuan9162 2016-05-13 09:12
浏览 12

如何查询此函数中的前2个帖子但是单独调用它们?

Can someone show me how to add 2 posts separately using a single query? For example when calling the code: { $content_block[2] .= (fphp_get_related_posts() ); show a post, then { $content_block[4] .= (fphp_get_related_posts() ); show the next post (offset by 1). How can I do this?

At the moment I know I have set 'max' => '1' I'm no php warrior, I was thinking you could 'max' => '2' and call each post like this: (fphp_get_related_posts(1) or (fphp_get_related_posts(2) but that is not right..

//Insert Related Posts Into Content Function
add_filter('the_content', 'mte_add_incontent_post');
function mte_add_incontent_post($content)
{ if(is_single()){
$content_block = explode('<p>',$content);
if(!empty($content_block[2]))
{ $content_block[2] .= (fphp_get_related_posts() );
}
if(!empty($content_block[4]))
{ $content_block[4] .= (fphp_get_related_posts() );
}
for($i=1;$i<count($content_block);$i++)
{ $content_block[$i] = '<p>'.$content_block[$i];
}
$content = implode('',$content_block);
}
return $content;  
}

function fphp_get_related_posts($atts) {

$atts = shortcode_atts( array(
'max' => '1',
), $atts, 'relatedposts' );

$reset_post = $post;

global $post;
$post_tags = wp_get_post_tags($post->ID);

if ($post_tags) {
    $post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
    $args=array(
        'tag__in' => $post_tag_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page' => $atts['max'],
      'orderby' => 'DESC'
    );

$related_query = new wp_query( $args );
if (intval($related_query->post_count) > 0) {

$html = '<div class="related_post"><b>SEE ALSO:</b>';

while( $related_query->have_posts() ) {
  $related_query->the_post();
  $html .= '<a rel="external" href="'. get_the_permalink(). '">';
  $html .= get_the_title() . '</a>';
}
}
$post = $reset_post;
wp_reset_query();

$html .= '</div>';

return $html;
}
}

Would appreciate your help so I can understand how to do this.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?