如何在WordPress中实现自定义友情链接功能?

玖比肆 WordPress 66

描述如何使用WordPress代码实现自定义友情链接功能的方法和注意事项。

回复

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

    在WordPress中,可以使用以下步骤实现自定义友情链接功能:

    1. 创建自定义链接类型:在functions.php文件中添加以下代码:

    ```
    function custom_link_type() {
    register_post_type('custom_links',
    array(
    'labels' => array(
    'name' => __('Custom Links'),
    'singular_name' => __('Custom Link')
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'custom_links'),
    'supports' => array('title', 'author', 'thumbnail', 'custom-fields'),
    'taxonomies' => array('category')
    )
    );
    }

    add_action('init', 'custom_link_type');
    ```

    2. 添加自定义链接:在WordPress后台中点击“新增”,选择“自定义链接”类型,输入链接地址和名称。

    3. 获取友情链接列表:在任意页面或模板文件中使用以下代码获取友情链接列表:

    ```
    $args = array( 'post_type' => 'custom_links', 'posts_per_page' => -1 );
    $custom_links = new WP_Query( $args );

    if ( $custom_links->have_posts() ) {
    while ( $custom_links->have_posts() ) {
    $custom_links->the_post();
    $link_url = get_post_meta( get_the_ID(), 'link_url', true );
    $link_title = get_the_title();
    echo '' . $link_title . '';
    }
    }

    wp_reset_postdata();
    ```

    在上述代码中,我们使用了WP_Query类来获取自定义链接,然后使用get_post_meta获取链接地址和get_the_title获取链接名称。

    需要注意的是,自定义链接类型默认是没有“友情链接”管理页面的,可以通过添加自定义菜单来实现友情链接的管理。同时,需要在functions.php文件中进行样式和脚本的引入和注册。

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