在第二段中显示帖子列表

时间:2021-09-03 作者:Godwin

您好,伟大的WordPress开发人员,我想在帖子的第二段或中间段列出特定类别的帖子。

我尝试向函数中添加一些脚本。php来实现这一点,但我无法让文章的第二段或中间段(这是关键部分)发挥作用。

function custom_related_posts( $content ) {
    global $post;
    $current_post_id = get_the_ID();
    $html = \'\';

    if ( is_singular( \'post\' ) ) {

        //get the categories of the current post
        $cats = get_the_category( $current_post_id );
        $cat_array = array();

        foreach ( $cats as $key1 => $cat ) {
            $cat_array[ $key1 ] = $cat->slug;
        }

        //get the tags of the current post
        $tags = get_the_tags( $current_post_id );
        $tag_array = array();

        foreach ( $tags as $key2 => $tag ) {
            $tag_array[ $key2 ] = $tag->slug;
        }

        $related_posts = new WP_Query(
            array(
                \'post_type\' => \'post\',
                \'post_status\' => \'publish\',
                \'tax_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'taxonomy\' => \'category\',
                        \'field\' => \'slug\',
                        \'terms\' => $cat_array
                    ),
                    array(
                        \'taxonomy\' => \'post_tag\',
                        \'field\' => \'slug\',
                        \'terms\' => $tag_array
                    )
                ),
                \'showposts\' => 6,
                \'post_type\' => array( \'post\' ),
                \'category_name\' => \'songs\',
                \'orderby\' => 1 
            )
        );
        if ( $related_posts->have_posts() ) {
            $html .= \'<div class="related-posts"><h3>Related posts</h3><ul class="related_post wp_rp">  \';

            while ( $related_posts->have_posts() ) {
                $related_posts->the_post();
                $html .= \'<li data-position="0" data-poid="in-215276" data-post-type="none"><a rel="bookmark" class="wp_rp_thumbnail" href="\';
                $html .= get_the_permalink();
                $html .= \'">\';

                if ( has_post_thumbnail( $post->ID ) ) {
                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'medium\' );
                    $html .= \'<img class="featured-image" src="\' . esc_url( $image[0] ) . \'">\';
                }

                $html .= \'<a class="wp_rp_title" style="color:#333333" href="\';
                $html .= get_the_permalink();
                $html .= \'">\';
                $html .= \'\' . get_the_title() . \'\';
                $html .= \'</a>\';
                $html .= \'</li>\';
            }
            $html .= \'</ul></div>\';
            wp_reset_postdata();
        } else {
            $html .= \'<div class="related-posts">\' . esc_html__( \'No related posts were found.\', \'textdomain\' ) . \'</div>\';
        }
        $content .= $html;
    }
    return $content;
}
add_filter( \'the_content\', \'custom_related_posts\' ); 
帖子链接:https://www.voxlyrics.com/drake-fountains-ft-tems/

1 个回复
SO网友:Antti Koskinen

可能是其中一个较早的WPSE/SO Q&;A帮助您找到合适的解决方案。这些可能不会为您提供复制和粘贴解决方案,但提供了一些示例,您可以将其应用于您的特定需求。

相关推荐

GET_POSTS显示当前帖子,而不是带有参数的已定义帖子

我想显示已定义的帖子,但get\\u posts显示当前帖子,而不使用args中的定义。为什么,如何解决?get\\u posts和wp query之间有什么区别?谢谢add_action( \'template_redirect\', \'check_shop\' ); function check_shop() { if (is_shop() ) { $args1 = array( \'post_a