最新公告
  • 欢迎您光临大资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • 如何将WordPress面包屑添加到您的网站主题

    如何将WordPress面包屑添加到您的网站主题 最后编辑:2021-04-26
    增值服务: 自动发货 使用说明 安装指导 环境配置二次开发BUG修复

    如何将WordPress面包屑添加到您的网站主题

    网站导航不足会导致不良的用户体验(UX)和跳出率上升。您的主菜单可能需要一些补充功能,以帮助用户筛选您的内容,例如WordPress面包屑。
    面包屑是一系列连接的导航链接,这些链接显示了您浏览网站页面的路径。它们在您浏览站点时出现,并形成一个层次结构,该层次结构从您访问的第一页开始,随后是每个后续站点。

    如前所述,面包屑还可以提高您网站的SEO。他们通过协助搜索引擎浏览页面并了解其层次结构和链接结构来做到这一点,无论您发布了多少内容。

    一、选择一个包含面包屑的主题

    可以说,以面包屑添加到你的网站最简单的方法是通过安装带有此功能的内置主题。
    如果您不满意直接编辑主题文件,或者不想安装新插件,那么这可能是您的最佳选择。但是,在已建立的网站上切换主题可能很困难,并且通常需要在一定程度上进行品牌重塑。
    此外,可能找不到适合自己口味且具有面包屑功能的主题。如果是这种情况,您将要探索以下其他方法之一。

    二、通过自定义代码实现面包屑

    如果您的主题没有面包屑功能,您也可以自己实现该功能。这涉及编辑当前主题的functions.php文件。
    跳到此方法之前,请确保为您的站点创建备份。这样,如果发生故障,您可以回滚到干净的版本。您还应该使用子主题,以防止主题更新期间所做的更改被覆盖。
    您还需要考虑使自己熟悉微数据,以帮助您获得面包屑的全部SEO好处。
    您可以通过三种方式访问​​主题文件,以便对其进行编辑:
    使用文件传输协议(FTP)和FTP客户端(例如FileZilla)连接到服务器。
    使用您的网站托管服务商的文件管理器工具,该工具应可从您的帐户仪表板访问。

    在线提供了许多有关如何为WordPress 编写面包屑的示例。您可以随意进行任何研究和工作,而无需进行任何研究。但是,如果您需要快速修复,则可以将以下代码添加到活动主题的functions.php文件中:

    <?php
    // WordPress Breadcrumb Function
    // Add this code into your theme function file.

    function ah_breadcrumb() {

    // Check if is front/home page, return
    if ( is_front_page() ) {
    return;
    }

    // Define
    global $post;
    $custom_taxonomy = ”; // If you have custom taxonomy place it here

    $defaults = array(
    ‘seperator’ => ‘»’,
    ‘id’ => ‘ah-breadcrumb’,
    ‘classes’ => ‘ah-breadcrumb’,
    ‘home_title’ => esc_html__( ‘Home’, ” )
    );

    $sep = ‘<li class=”seperator”>’. esc_html( $defaults[‘seperator’] ) .'</li>’;

    // Start the breadcrumb with a link to your homepage
    echo ‘<ul id=”‘. esc_attr( $defaults[‘id’] ) .'” class=”‘. esc_attr( $defaults[‘classes’] ) .'”>’;

    // Creating home link
    echo ‘<li class=”item”><a href=”‘. get_home_url() .'”>’. esc_html( $defaults[‘home_title’] ) .'</a></li>’ . $sep;

    if ( is_single() ) {

    // Get posts type
    $post_type = get_post_type();

    // If post type is not post
    if( $post_type != ‘post’ ) {

    $post_type_object = get_post_type_object( $post_type );
    $post_type_link = get_post_type_archive_link( $post_type );

    echo ‘<li class=”item item-cat”><a href=”‘. $post_type_link .'”>’. $post_type_object->labels->name .'</a></li>’. $sep;

    }

    // Get categories
    $category = get_the_category( $post->ID );

    // If category not empty
    if( !empty( $category ) ) {

    // Arrange category parent to child
    $category_values = array_values( $category );
    $get_last_category = end( $category_values );
    // $get_last_category = $category[count($category) – 1];
    $get_parent_category = rtrim( get_category_parents( $get_last_category->term_id, true, ‘,’ ), ‘,’ );
    $cat_parent = explode( ‘,’, $get_parent_category );

    // Store category in $display_category
    $display_category = ”;
    foreach( $cat_parent as $p ) {
    $display_category .= ‘<li class=”item item-cat”>’. $p .'</li>’ . $sep;
    }

    }

    // If it’s a custom post type within a custom taxonomy
    $taxonomy_exists = taxonomy_exists( $custom_taxonomy );

    if( empty( $get_last_category ) && !empty( $custom_taxonomy ) && $taxonomy_exists ) {

    $taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );
    $cat_id = $taxonomy_terms[0]->term_id;
    $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
    $cat_name = $taxonomy_terms[0]->name;

    }

    // Check if the post is in a category
    if( !empty( $get_last_category ) ) {

    echo $display_category;
    echo ‘<li class=”item item-current”>’. get_the_title() .'</li>’;

    } else if( !empty( $cat_id ) ) {

    echo ‘<li class=”item item-cat”><a href=”‘. $cat_link .'”>’. $cat_name .'</a></li>’ . $sep;
    echo ‘<li class=”item-current item”>’. get_the_title() .'</li>’;

    } else {

    echo ‘<li class=”item-current item”>’. get_the_title() .'</li>’;

    }

    } else if( is_archive() ) {

    if( is_tax() ) {
    // Get posts type
    $post_type = get_post_type();

    // If post type is not post
    if( $post_type != ‘post’ ) {

    $post_type_object = get_post_type_object( $post_type );
    $post_type_link = get_post_type_archive_link( $post_type );

    echo ‘<li class=”item item-cat item-custom-post-type-‘ . $post_type . ‘”><a href=”‘ . $post_type_link . ‘”>’ . $post_type_object->labels->name . ‘</a></li>’ . $sep;

    }

    $custom_tax_name = get_queried_object()->name;
    echo ‘<li class=”item item-current”>’. $custom_tax_name .'</li>’;

    } else if ( is_category() ) {

    $parent = get_queried_object()->category_parent;

    if ( $parent !== 0 ) {

    $parent_category = get_category( $parent );
    $category_link = get_category_link( $parent );

    echo ‘<li class=”item”><a href=”‘. esc_url( $category_link ) .'”>’. $parent_category->name .'</a></li>’ . $sep;

    }

    echo ‘<li class=”item item-current”>’. single_cat_title( ”, false ) .'</li>’;

    } else if ( is_tag() ) {

    // Get tag information
    $term_id = get_query_var(‘tag_id’);
    $taxonomy = ‘post_tag’;
    $args = ‘include=’ . $term_id;
    $terms = get_terms( $taxonomy, $args );
    $get_term_name = $terms[0]->name;

    // Display the tag name
    echo ‘<li class=”item-current item”>’. $get_term_name .'</li>’;

    } else if( is_day() ) {

    // Day archive

    // Year link
    echo ‘<li class=”item-year item”><a href=”‘. get_year_link( get_the_time(‘Y’) ) .'”>’. get_the_time(‘Y’) . ‘ Archives</a></li>’ . $sep;

    // Month link
    echo ‘<li class=”item-month item”><a href=”‘. get_month_link( get_the_time(‘Y’), get_the_time(‘m’) ) .'”>’. get_the_time(‘M’) .’ Archives</a></li>’ . $sep;

    // Day display
    echo ‘<li class=”item-current item”>’. get_the_time(‘jS’) .’ ‘. get_the_time(‘M’). ‘ Archives</li>’;

    } else if( is_month() ) {

    // Month archive

    // Year link
    echo ‘<li class=”item-year item”><a href=”‘. get_year_link( get_the_time(‘Y’) ) .'”>’. get_the_time(‘Y’) . ‘ Archives</a></li>’ . $sep;

    // Month Display
    echo ‘<li class=”item-month item-current item”>’. get_the_time(‘M’) .’ Archives</li>’;

    } else if ( is_year() ) {

    // Year Display
    echo ‘<li class=”item-year item-current item”>’. get_the_time(‘Y’) .’ Archives</li>’;

    } else if ( is_author() ) {

    // Auhor archive

    // Get the author information
    global $author;
    $userdata = get_userdata( $author );

    // Display author name
    echo ‘<li class=”item-current item”>’. ‘Author: ‘. $userdata->display_name . ‘</li>’;

    } else {

    echo ‘<li class=”item item-current”>’. post_type_archive_title() .'</li>’;

    }

    } else if ( is_page() ) {

    // Standard page
    if( $post->post_parent ) {

    // If child page, get parents
    $anc = get_post_ancestors( $post->ID );

    // Get parents in the right order
    $anc = array_reverse( $anc );

    // Parent page loop
    if ( !isset( $parents ) ) $parents = null;
    foreach ( $anc as $ancestor ) {

    $parents .= ‘<li class=”item-parent item”><a href=”‘. get_permalink( $ancestor ) .'”>’. get_the_title( $ancestor ) .'</a></li>’ . $sep;

    }

    // Display parent pages
    echo $parents;

    // Current page
    echo ‘<li class=”item-current item”>’. get_the_title() .'</li>’;

    } else {

    // Just display current page if not parents
    echo ‘<li class=”item-current item”>’. get_the_title() .'</li>’;

    }

    } else if ( is_search() ) {

    // Search results page
    echo ‘<li class=”item-current item”>Search results for: ‘. get_search_query() .'</li>’;

    } else if ( is_404() ) {

    // 404 page
    echo ‘<li class=”item-current item”>’ . ‘Error 404’ . ‘</li>’;

    }

    // End breadcrumb
    echo ‘</ul>’;

    }

    然后,您还需要将以下行添加到主题的header.php文件中:

    <?php
    // Call the breadcrumb function where you want to display
    ah_breadcrumb();

    第一个片段将面包屑添加到您的主题。第二个“调用”相关功能,以便导航链接出现在标题中。请注意,您可能需要删除开头的<?php,此代码才能与主题的现有文件一起使用。
    根据您的代码流利程度,您可以编写自定义函数来在站点上启用面包屑。您还可以使用CSS修改其外观。

    三、使用插件添加面包屑

    向您的WordPress网站添加面包屑功能的最后一种方法需要使用插件。有专门的工具可用于此目的,例如Breadcrumb NavXT。
    另外,也许您已经在使用Yoast SEO。如果是这样,此插件还提供了一种启用面包屑的方法。这不足为奇,因为我们已经注意到此功能可以增强您网站的SEO。
    我们将继续向您展示该流程如何与Yoast SEO一起使用。首先,您需要安装并激活它。接下来,导航到SEO>搜索外观。
    在此处,从“ 面包屑”选项卡启用面包屑功能:

    启用面包屑后,您将可以访问用于配置它们的多个选项。在大多数情况下,默认设置就足够了。但是,请随时进行更改以适合您的口味。之后,单击“ 保存更改”按钮。
    如果您的主题不支持面包屑,则仍需要包含一些代码以完成启用它们。在您的子主题的header.php文件末尾添加以下代码段:

    <?php
    if ( function_exists(‘yoast_breadcrumb’) ) {
    yoast_breadcrumb( ‘<p id=”breadcrumbs”>’,'</p>’ );
    }
    ?>

    请注意,根据您的主题,您可能不需要包括<?php和?>标记。此外,您可以在其中包含上述代码的其他文件是single.php或page.php,位于页面标题之后。

    结论

    面包屑可以作为WordPress网站上主要导航菜单的必要补充。这个漂亮的功能改善了您网站的用户体验,并帮助搜索引擎了解您的内容及其整体结构。

    猜你在找

    • 2021-04-26Hi,初次和大家见面了,请多关照!

    售后服务:

    • 售后服务范围 1、商业模板使用范围内问题免费咨询
      2、源码安装、模板安装(一般 ¥50-300)服务答疑仅限SVIP用户
      3、单价超过200元的模板免费一次安装,需提供服务器信息。
      付费增值服务 1、提供dedecms模板、WordPress主题、discuz模板优化等服务请详询在线客服
      2、承接 WordPress、DedeCMS、Discuz 等系统建站、仿站、开发、定制等服务
      3、服务器环境配置(一般 ¥50-300)
      4、网站中毒处理(需额外付费,500元/次/质保三个月)
      售后服务时间 周一至周日(法定节假日除外) 9:00-23:00
      免责声明 本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: 3482249445@qq.com),我们会及时删除,给您带来的不便,我们深表歉意!

    Hi, 如果你对这款模板有疑问,可以跟我联系哦!

    联系作者
    升级SVIP尊享更多特权立即升级
    My title page contents
    召唤伊斯特瓦尔