显示多站点子页面 WordPress

Show Multisite Subpages WordPress

我有一个多站点安装:
1: 主站点 - domain.com
2: 其他站点-域.com/some-url

已修复: 到目前为止一切顺利。我的脚本会自动在 header 下方显示内容,而不是在插入短代码的位置显示内容。

function subsites_list_sites() {
    $subsites = get_sites();    
    if ( ! empty ( $subsites ) ) {  
        echo '<div class="subsites-container">';
        foreach( $subsites as $subsite ) {
            $subsite_id = get_object_vars( $subsite )["blog_id"];
            $subsite_name = get_blog_details( $subsite_id )->blogname;
            $subsite_link = get_blog_details( $subsite_id )->siteurl;
            echo '<div class="subtite-item site-' . $subsite_id . '">
            <a class="thumb" href="' . $subsite_link . '">Here i want the featured image/thumbnail to display</a>
            <a class="title-link" href="' . $subsite_link . '"><h3 class="title">' . $subsite_name . '</h3></a>
            <a class="excerpt" href="' . $subsite_link . '"><p>Here i want excerpt<p></a>
            <a class="btn-link" href="' . $subsite_link . '">GO TO WEBSITE</a>
            </div>';
        }
        echo '</div>';
    }
}
add_shortcode( 'subsites_list_sites_sc', 'subsites_list_sites' );

短代码应该 return html 不在短代码函数中打印它。

这样试试:

function subsites_list_sites() {
    $subsites = get_sites();
    $returnHtml = '';    
    if ( ! empty ( $subsites ) ) {  
        $returnHtml .= '<div class="subsites-container">';
        foreach( $subsites as $subsite ) {
            $subsite_id = get_object_vars( $subsite )["blog_id"];
            $subsite_name = get_blog_details( $subsite_id )->blogname;
            $subsite_link = get_blog_details( $subsite_id )->siteurl;
            $returnHtml .= '<div class="subtite-item site-' . $subsite_id . '">
            <a class="thumb" href="' . $subsite_link . '">Here i want the featured image/thumbnail to display</a>
            <a class="title-link" href="' . $subsite_link . '"><h3 class="title">' . $subsite_name . '</h3></a>
            <a class="excerpt" href="' . $subsite_link . '"><p>Here i want excerpt<p></a>
            <a class="btn-link" href="' . $subsite_link . '">GO TO WEBSITE</a>
            </div>';
        }
        $returnHtml .= '</div>';
    }
    return $returnHtml;
}

Output
The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page. https://codex.wordpress.org/Shortcode_API#Output