wordpress 自定义_用于主题自定义的10个WordPress片段

当您使用WordPress时,由于主题的缘故,更改网站外观非常容易。 有很多(像一堆一样)免费或溢价的WordPress主题 。 选择一个主题,然后安装它,您可以在几分钟内为您的网站带来全新的外观。

除了提供外观之外,WordPress主题还可以通过多种方式扩展。 您可以使用插件构建新功能,但在本篇文章中,我们正在研究可以方便地使用主题的WordPress函数 。 您只需要将这些函数放在主题的functions.php文件中即可生效。

推荐读物: 如何在WordPress中管理和使用代码段

1.更改摘要的长度

摘录是您所见内容的一小部分。 在这种情况下,WordPress默认将摘录设置为55个字长。 但是,WordPress允许我们通过excerpt_length过滤器自定义默认长度,如下所示。

function my_excerpt_length( $length ) {
   return 30;
}
add_filter( 'excerpt_length', 'my_excerpt_length', 999 );

返回值是指将作为摘要显示的单词总数。 在上面的示例中,我们在摘录中显示了每个帖子价值30个单词。

2.减少职位修订

WordPress使作家和博客作者可以回顾他们以前的作品。 但是,随着修订版本数量的增加,它们也会影响网站的性能,因为每个新记录的修订版本都会向数据库添加新的一行。 随着时间的流逝,这个问题只会变得更糟。

要解决此问题,您可以设置要在数据库中保存多少个修订。 为此,请打开wp-config.php并在下面添加此代码段。 更改数量以限制要保存的修订版本数。

define('WP_POST_REVISIONS', 5);

如果您想禁用WordPress版本,则将值切换为FALSE如下所示:

define('WP_POST_REVISIONS', false);
3.在帖子上自动设置特色图片

显示代表或描述帖子的特色图片是一种常见的做法。 WordPress要求我们手动设置此特色图像。 为了使过程更有效率,我们可以通过将帖子中的第一个图像作为特色图像来自动设置特色图像。 使用以下代码段。

function autoset_featured() {
    global $post;
    $already_has_thumb = has_post_thumbnail($post->ID);
    if (!$already_has_thumb)  {
    $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    	if ($attached_image) {
            foreach ($attached_image as $attachment_id => $attachment) {
            	set_post_thumbnail($post->ID, $attachment_id);
            }
       	}
     }
}
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
4.强制最小评论长度

不喜欢1-2个字的评论,例如“ Nice Post!”,“ Good Job!”吗? 尽管评论可能不是垃圾评论,但这些评论通常不会鼓励进一步的讨论。 如果您想让评论者说话,可以设置读者要求的最小单词长度,以便读者发表评论。

以下是用于设置注释字符或单词的最小数量的代码段。 将其放在functions.php中

function minimal_comment_length( $commentdata ) {
    $minimalCommentLength = 20;
    if ( strlen( trim( $commentdata['comment_content'] ) )

$minimalCommentLength value is the minimum number of characters that is required, make your changes to this value to tweak this.

5. Disable Links From User Comments

Links that are included in the comments form will instantly become a clickable link once they are posted and approved. This can be exploited by spammers, encouraging them to flood your comment section with a link to their "spammy" page.

To counter this, you can add this filter to disable the click-ability of the link(s) and retain them simply as plain text.

remove_filter('comment_text', 'make_clickable', 9);
6. Remove Class And ID's From Custom Menus

If you look at the custom menu in WordPress, you will find a bunch of classes and ids on every menu item. Use the snippet below to remove the classes you don't want and to keep the classes that you need.

function my_css_attributes_filter($var) {
	return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);

In the above code, we are keeping the current-menu-item class.

7. Exclude Pages From Search

Doing searches in WordPress will pull results from both pages and posts, sometimes with not much relevance to your search query. To prevent this, you can filter the search results to show only those found in posts. Add this snippet to function.php to do this.

function SearchFilter($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
	}
	return $query;
}
add_filter('pre_get_posts','SearchFilter');
8. Replace Howdy Text

Bored with the 'Howdy' text? If you want to change that to a special salutation on your site, just add this to functions.php and the effect will take place immediately.

function change_howdy($translated, $text, $domain) {
	if (false !== strpos($translated, 'Howdy'))
	return str_replace('Howdy', 'Hello', $translated);
	return $translated;
}
add_filter('gettext', 'change_howdy', 10, 3);
9. Add Additional Menu Removal For Particular Roles

You can hide menus that are in the Dashboard, for non-administrators, with this snippet.

function remove_admin_menus(){
	if(is_user_logged_in() && !current_user_can('administrator')){
		remove_menu_page( 'index.php' );                  //Dashboard
		remove_menu_page( 'edit.php' );                   //Posts
		remove_menu_page( 'upload.php' );                 //Media
		remove_menu_page( 'edit.php?post_type=page' );    //Pages
		remove_menu_page( 'edit-comments.php' );          //Comments
		remove_menu_page( 'themes.php' );                 //Appearance
		remove_menu_page( 'plugins.php' );                //Plugins
		remove_menu_page( 'users.php' );                  //Users
		remove_menu_page( 'tools.php' );                  //Tools
		remove_menu_page( 'options-general.php' );        //Settings
	}
}
add_action('admin_init', 'remove_admin_menus');

Please note that this just removes the menus from the screen, but does not filter the user's permission to access these menu. It does not prevent a user from accessing those menus directly through the browser address bar. Remember to add them to your functions.php .

10. Remove Admin Bar Link For Non-Adminstrators

In the WordPress Dashboard, besides the main menu on the sidebar, you will also find a couple of menu links at the top. You can restrict access to this admin bar link from specific roles or users. This snippet below will remove the menu in the admin bar for users who aren't an Administrator – adjust accordingly.

function remove_admin_bar_links() {
	global $wp_admin_bar;
	if (!current_user_can('administrator')) {
		$wp_admin_bar->remove_menu('wp-logo');          // Remove the WordPress logo
		$wp_admin_bar->remove_menu('about');            // Remove the about WordPress link
		$wp_admin_bar->remove_menu('wporg');            // Remove the WordPress.org link
		$wp_admin_bar->remove_menu('documentation');    // Remove the WordPress documentation link
		$wp_admin_bar->remove_menu('support-forums');   // Remove the support forums link
		$wp_admin_bar->remove_menu('feedback');         // Remove the feedback link
		$wp_admin_bar->remove_menu('site-name');        // Remove the site name link
		$wp_admin_bar->remove_menu('view-site');        // Remove the visit site link
		$wp_admin_bar->remove_menu('updates');          // Remove the updates link
		$wp_admin_bar->remove_menu('comments');         // Remove the comments link
		$wp_admin_bar->remove_menu('new-content');      // Remove the new content link
		$wp_admin_bar->remove_menu('my-account');       // Remove the user details tab
	}
}
add_action('wp_before_admin_bar_render', 'remove_admin_bar_links');

翻译自: https://www.hongkiat.com/blog/wordpress-snippets-customize-theme/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值