function wpb_remove_version() { return ''; } add_filter('the_generator', 'wpb_remove_version');有关详细说明,请参阅我们的指南,了解删除WordPress版本号的正确方法。
function wpb_custom_logo() { echo ' <style type="text/css"> #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before { background-image: url(' . get_bloginfo('stylesheet_directory') . '/images/custom-logo.png) !important; background-position: 0 0; color:rgba(0, 0, 0, 0); } #wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon { background-position: 0 0; } </style> '; } //hook into the administrative header output add_action('wp_before_admin_bar_render', 'wpb_custom_logo');有关其他方法和更多详细信息,请参阅有关如何在WordPress中添加自定义仪表板徽标的指南。
function remove_footer_admin () { echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | WordPress Tutorials: <a href="https://www.wpbeginner.com" target="_blank">WPBeginner</a></p>'; } add_filter('admin_footer_text', 'remove_footer_admin');您可以随意更改要添加的文本和链接。以下是我们测试网站的外观。
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets'); function my_custom_dashboard_widgets() { global $wp_meta_boxes; wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help'); } function custom_dashboard_help() { echo '<p>Welcome to Custom Blog Theme! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials visit: <a href="https://www.wpbeginner.com" target="_blank">WPBeginner</a></p>'; }这是它的样子: 有关详细信息,请参阅有关如何在WordPress中添加自定义仪表板小部件的教程。
add_filter( 'avatar_defaults', 'wpb_new_gravatar' ); function wpb_new_gravatar ($avatar_defaults) { $myavatar = 'http://example.com/wp-content/uploads/2017/01/wpb-default-gravatar.png'; $avatar_defaults[$myavatar] = "Default Gravatar"; return $avatar_defaults; }现在,您可以转到设置»讨论页面并选择您的默认头像。 有关详细说明,请参阅有关如何更改WordPress中默认gravatar的指南。
function wpb_copyright() { global $wpdb; $copyright_dates = $wpdb->get_results(" SELECT YEAR(min(post_date_gmt)) AS firstdate, YEAR(max(post_date_gmt)) AS lastdate FROM $wpdb->posts WHERE post_status = 'publish' "); $output = ''; if($copyright_dates) { $copyright = "© " . $copyright_dates[0]->firstdate; if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) { $copyright .= '-' . $copyright_dates[0]->lastdate; } $output = $copyright; } return $output; }添加此功能后,您需要打开footer.php文件并在以下任何位置添加以下代码以显示动态版权日期:
<?php echo wpb_copyright(); ?>此功能会查找您第一篇文章的日期以及上一篇文章的日期。然后,无论你在哪里调用这个函数,它都会回响起来。 有关详细信息,请参阅有关如何在WordPress中添加动态版权日期的指南。
function wpb_bg() { $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'); $color ='#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)]. $rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)]; echo $color; }接下来,您需要编辑主题中的header.php文件。找到
<body>
标记并添加替换为此行:
<body <?php body_class(); ?> style="background-color:<?php wpb_bg();?>">>您现在可以保存更改并访问您的网站以查看此操作。 有关更多详细信息和替代方法,请参阅有关如何在WordPress中随机更改背景颜色的教程。
update_option( 'siteurl', 'http://example.com' ); update_option( 'home', 'http://example.com' );不要忘记用您自己的域名替换example.com。 登录后,您可以转到“设置”并在其中设置URL。之后,您应该删除添加到函数文件中的代码,否则只要访问您的站点,它就会不断更新这些URL。
add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode此代码创建三个具有不同大小的新图像大小。随意调整代码以满足您自己的要求。 您可以使用此代码在主题中的任何位置显示图像大小。
<?php the_post_thumbnail( 'homepage-thumb' ); ?>有关详细说明,请参阅有关如何在WordPress中创建其他图像大小的指南。
function wpb_custom_new_menu() { register_nav_menu('my-custom-menu',__( 'My Custom Menu' )); } add_action( 'init', 'wpb_custom_new_menu' );您现在可以转到外观»菜单,您将看到“我的自定义菜单”作为主题位置选项。 现在,您需要将此代码添加到要显示导航菜单的主题中。
<?php wp_nav_menu( array( 'theme_location' => 'my-custom-menu', 'container_class' => 'custom-menu-class' ) ); ?>有关详细说明,请参阅有关如何在WordPress主题中添加自定义导航菜单的指南。
function wpb_new_contactmethods( $contactmethods ) { // Add Twitter $contactmethods['twitter'] = 'Twitter'; //add Facebook $contactmethods['facebook'] = 'Facebook'; return $contactmethods; } add_filter('user_contactmethods','wpb_new_contactmethods',10,1);此代码将Twitter和Facebook字段添加到WordPress中的用户配置文件。 您现在可以在作者模板中显示这些字段,如下所示:
<?php echo $curauth->twitter; ?>您可能还希望查看有关如何在WordPress注册中添加其他用户配置文件字段的指南。
// Register Sidebars function custom_sidebars() { $args = array( 'id' => 'custom_sidebar', 'name' => __( 'Custom Widget Area', 'text_domain' ), 'description' => __( 'A custom widget area', 'text_domain' ), 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', ); register_sidebar( $args ); } add_action( 'widgets_init', 'custom_sidebars' );您现在可以访问外观»窗口小部件页面,您将看到新的自定义窗口小部件区域。 要在主题中显示此侧边栏或小部件就绪区域,请添加以下代码:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('custom_sidebar') ) : ?> <!–Default sidebar info goes here–> <?php endif; ?>有关更多详细信息,请参阅我们的指南,了解如何在WordPress中添加动态小部件就绪区域和侧边栏。
function wpbeginner_postrss($content) { if(is_feed()){ $content = 'This post was written by Syed Balkhi '.$content.'Check out WPBeginner'; } return $content; } add_filter('the_excerpt_rss', 'wpbeginner_postrss'); add_filter('the_content', 'wpbeginner_postrss');有关更多信息,请参阅有关如何添加内容和完全操作RSS源的指南。
function rss_post_thumbnail($content) { global $post; if(has_post_thumbnail($post->ID)) { $content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . get_the_content(); } return $content; } add_filter('the_excerpt_rss', 'rss_post_thumbnail'); add_filter('the_content_feed', 'rss_post_thumbnail');有关详细信息,请参阅有关如何将后缩略图添加到WordPress RSS源的指南。
function no_wordpress_errors(){ return 'Something is wrong!'; } add_filter( 'login_errors', 'no_wordpress_errors' );现在,用户在输入错误的用户名或密码时会看到一般消息。 有关更多信息,请参阅我们的教程,了解如何在WordPress登录错误消息中禁用登录提示。
remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );有关更多信息,请参阅我们的指南,了解如何在WordPress中禁用通过电子邮件登录功能。
function fb_filter_query( $query, $error = true ) { if ( is_search() ) { $query->is_search = false; $query->query_vars[s] = false; $query->query[s] = false; // to error if ( $error == true ) $query->is_404 = true; } } add_action( 'parse_query', 'fb_filter_query' ); add_filter( 'get_search_form', create_function( '$a', "return null;" ) );有关更多信息,请参阅有关如何在WordPress中禁用搜索功能的教程。
function publish_later_on_feed($where) { global $wpdb; if ( is_feed() ) { // timestamp in WP-format $now = gmdate('Y-m-d H:i:s'); // value for wait; + device $wait = '10'; // integer // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff $device = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR // add SQL-sytax to default $where $where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait "; } return $where; } add_filter('posts_where', 'publish_later_on_feed');在这段代码中,我们使用10分钟作为$ wait或延迟时间。随意将其更改为您想要的任意分钟数。 有关插件方法和更多信息,请参阅有关如何延迟在WordPress RSS提要中显示帖子的详细指南。
function modify_read_more_link() { return '<a class="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>'; } add_filter( 'the_content_more_link', 'modify_read_more_link' );
function fb_disable_feed() { wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') ); } add_action('do_feed', 'fb_disable_feed', 1); add_action('do_feed_rdf', 'fb_disable_feed', 1); add_action('do_feed_rss', 'fb_disable_feed', 1); add_action('do_feed_rss2', 'fb_disable_feed', 1); add_action('do_feed_atom', 'fb_disable_feed', 1);有关插件方法和更多信息,请参阅我们的指南,了解如何在WordPress中禁用RSS源。
function new_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'new_excerpt_length');将100更改为要在摘录中显示的单词数。 对于替代方法,您可能需要查看我们的指南,了解如何自定义WordPress摘录(无需编码)。
function wpb_admin_account(){ $user = 'Username'; $pass = 'Password'; $email = 'email@domain.com'; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); $user = new WP_User( $user_id ); $user->set_role( 'administrator' ); } } add_action('init','wpb_admin_account');不要忘记填写用户名,密码和电子邮件字段。登录WordPress网站后,请不要忘记删除功能文件中的代码。 有关此主题的更多信息,请查看有关如何使用FTP在WordPress中添加管理员用户的教程。
remove_action('welcome_panel', 'wp_welcome_panel');有关其他方法和更多详细信息,请查看有关如何在WordPress仪表板中删除欢迎面板的指南。
// Function to return user count function wpb_user_count() { $usercount = count_users(); $result = $usercount['total_users']; return $result; } // Creating a shortcode to display user count add_shortcode('user_count', 'wpb_user_count');此代码创建一个短代码,允许您显示站点上注册用户的总数。现在,您只需将此短代码添加到[user_count]您希望显示用户总数的帖子或页面。 有关更多信息和插件方法,请参阅我们的教程,了解如何在WordPress中显示注册用户总数。
function exclude_category($query) { if ( $query->is_feed ) { $query->set('cat', '-5, -2, -3'); } return $query; } add_filter('pre_get_posts', 'exclude_category');
// Enable shortcodes in text widgets add_filter('widget_text','do_shortcode');有关替代方法和更多信息,请查看我们的指南,了解如何在WordPress边栏小部件中使用短代码。
function oddeven_post_class ( $classes ) { global $current_class; $classes[] = $current_class; $current_class = ($current_class == 'odd') ? 'even' : 'odd'; return $classes; } add_filter ( 'post_class' , 'oddeven_post_class' ); global $current_class; $current_class = 'odd';此代码只是为WordPress帖子添加奇数或偶数类。您现在可以添加自定义CSS以不同方式设置它们。以下是帮助您入门的示例代码。
.even { background:#f0f8ff; } .odd { background:#f4f4fb; }最终结果将如下所示: 需要更详细的说明?看一下我们的教程,了解如何在WordPress主题中为你的帖子添加奇数/偶数类。
function my_myme_types($mime_types){ $mime_types['svg'] = 'image/svg+xml'; //Adding svg extension $mime_types['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files return $mime_types; } add_filter('upload_mimes', 'my_myme_types', 1, 1);此代码允许您将SVG和PSD文件上载到WordPress。您需要Google找出您要允许的文件类型的mime类型,然后在代码中使用它。 有关此主题的更多信息,请查看我们的教程,了解如何添加要在WordPress中上载的其他文件类型。
function wpb_imagelink_setup() { $image_set = get_option( 'image_default_link_type' ); if ($image_set !== 'none') { update_option('image_default_link_type', 'none'); } } add_action('admin_init', 'wpb_imagelink_setup', 10);现在,当您在WordPress中上传新图像时,它将不会自动链接。如果需要,您仍然可以将其链接到文件或附件页面。 您可能需要查看我们的教程,了解如何在WordPress中删除默认图像链接以获取备用插件方法和更多信息。
function wpb_author_info_box( $content ) { global $post; // Detect if it is a single post with a post author if ( is_single() && isset( $post->post_author ) ) { // Get author's display name $display_name = get_the_author_meta( 'display_name', $post->post_author ); // If display name is not available then use nickname as display name if ( empty( $display_name ) ) $display_name = get_the_author_meta( 'nickname', $post->post_author ); // Get author's biographical information or description $user_description = get_the_author_meta( 'user_description', $post->post_author ); // Get author's website URL $user_website = get_the_author_meta('url', $post->post_author); // Get link to the author archive page $user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author)); if ( ! empty( $display_name ) ) $author_details = '<p class="author_name">About ' . $display_name . '</p>'; if ( ! empty( $user_description ) ) // Author avatar and bio $author_details .= '<p class="author_details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</p>'; $author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>'; // Check if author has a website in their profile if ( ! empty( $user_website ) ) { // Display author website link $author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>'; } else { // if there is no author website then just close the paragraph $author_details .= '</p>'; } // Pass all this info to post content $content = $content . '<footer class="author_bio_section" >' . $author_details . '</footer>'; } return $content; } // Add our function to the post content filter add_action( 'the_content', 'wpb_author_info_box' ); // Allow HTML in author bio section remove_filter('pre_user_description', 'wp_filter_kses');接下来,您需要添加一些自定义CSS以使其看起来更好。您可以使用此示例CSS作为起点。
.author_bio_section{ background: none repeat scroll 0 0 #F5F5F5; padding: 15px; border: 1px solid #ccc; } .author_name{ font-size:16px; font-weight: bold; } .author_details img { border: 1px solid #D8D8D8; border-radius: 50%; float: left; margin: 0 10px 10px 0; }这就是你的作者框的样子: 有关插件方法和更详细的说明,请查看有关如何在WordPress帖子中添加作者信息框的文章。
add_filter('xmlrpc_enabled', '__return_false');您可能需要阅读我们关于如何在WordPress中禁用XML-RPC的文章以获取更多信息。
function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) { If (! is_singular()) { $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>'; return $html; } else { return $html; } } add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );您可能需要阅读我们的文章,了解如何自动将特色图像链接到WordPress中的帖子。 目前为止就这样了。 我们希望本文能帮助您学习WordPress中functions.php文件的一些新的有用技巧。您可能还希望看到我们提升WordPress速度和性能的终极指南。
发现、改变
探知、求新
共享,感恩一路相伴
昱远品牌形象已完成全面升级
点击访问新官网