如何在WordPress中实现自定义网站地图功能?

玖比肆 WordPress 29

描述如何使用WordPress代码实现自定义网站地图功能的方法和注意事项。

回复

共1条回复 我来回复
  • 玖比肆的头像
    玖比肆 评论

    要在WordPress中实现自定义网站地图功能,可以遵循以下步骤:

    1. 首先,在functions.php文件中添加以下代码段,该代码段将创建自定义的网站地图页面模板:

    ```php
    /**
    * Create a sitemap page template.
    */
    add_action( 'template_redirect', function() {
    if ( is_page_template( 'sitemap.php' ) ) {
    header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
    echo ''; ?>
    >

    <?php echo esc_html( get_bloginfo( 'name' ) ); ?>


    1.2


    <?php the_title_rss(); ?>


    ]]>
    ]]>





    'sitemap',
    'post_type' => 'page',
    'post_status' => 'publish',
    'numberposts' => 1,
    'no_found_rows' => true,
    'cache_results' => true,
    'update_post_meta_cache' => true,
    'update_post_term_cache' => true,
    ] );

    if ( ! empty( $sitemap_page ) ) {
    $sitemap_url = get_permalink( $sitemap_page[0]->ID );
    wp_redirect( $sitemap_url, 301 );
    exit;
    }
    }
    } );
    ```

    2. 接下来,创建一个名为“sitemap.php”的模板文件,并在其中添加以下代码段,以显示所有文章的标题和URL:

    ```php

    'post',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC',
    ];
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) : ?>

      have_posts() ) : $query->the_post(); ?>




    ```

    3. 在WordPress后台中,创建新的页面,并将其模板设置为“Sitemap”。

    4. 最后,在网站上访问“/sitemap”路径,即可看到自定义的网站地图页面。

    注意事项:

    1. 指定模板名称时要确保与代码片段中的名称一致。
    2. 确认主题中是否存在“header.php”和“footer.php”文件,并正确使用它们。
    3. 如果需要在网站地图页面中包含页面、自定义帖子类型或分类目录,请在$args变量中指定相应的参数。

    2023年 5月 11日 下午1:45 0条评论
微信客服
分享本页
返回顶部
玖比肆(9Bi4)专注于帮助企业实现数字化转型,提供全方位的网站建设、网络运营、网络推广、网络营销和SEO优化等一系列数字化营销服务。