wordpress设置独立的描述和关键字(如何关键字(WordPress自定义页面)

Wordpress1年前 (2023)发布 SUYEONE
570 0 0

When you download a WordPress theme, it’s common to find that the provided meta tags for keywords and description are either inadequate or entirely missing, which negatively impacts search engine optimization (seo). This guide will walk you through adding unique descriptions and keywords to your homepage, categories, pages, and indiVidual blog posts without relying on plugins, specifically the All in One SEO Pack, which some users find problematic.

To address this issue, we’ll use code snippets that you can incorporate into your WordPress theme’s header.php file between the “ tags. Save the file as UTF-8 to prevent any Chinese characters from displaying incorrectly:

“`php
ID, ‘Description’, true);
$description2 = str_replace(“\n”, “”, mb_strimwidth(strip_tags($post->post_content), 0, 200, ‘…’, ‘utf-8’));
// Display custom field description if set, otherwise use the first 200 characters of the article content
$description = $description1 ? $description1 : $description2;
// Display custom field keywords if set, otherwise use post tags
$keywords = get_post_meta($post->ID, ‘Keywords’, true);
if ($keywords == ”) {
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag) {
$keywords .= $tag->name . “, “;
}
$keywords = rtrim($keywords, ‘, ‘);
}
} elseif (is_category()) {
// EDiT category descriptions in WordPress dashboard > Posts > Categories
$description = category_description();
$keywords = single_cat_title(”, false);
} elseif (is_tag()) {
// Edit tag descriptions in WordPress dashboard > Posts > Tags
$description = tag_description();
$keywords = single_tag_title(”, false);
}
$description = trim(strip_tags($description));
$keywords = trim(strip_tags($keywords));
?>
<meta name="description" content="” />
<meta name="keywords" content="” />
“`

Now, when writing blog posts, add two custom fields (if not visible, click ‘Screen Options’ in the top right corner). The first custom field should be named ‘Keywords,’ where you’ll write the post’s keywords. For the second custom field, name it ‘Description’ and enter the post’s description. Once used, these custom fields will appear as dropdown options for future posts, allowing each article to have tAIlored keywords and descriptions, eliminating the need for the All in One SEO Pack plugin.

For your homepage and pages, you can directly modify the PHP code with your own descriptions and keywords. Category and tag pages will inherit their descriptions from the respective category or tag descriptions, editable in the WordPress backend. If you wish to manually define descriptions and keywords for each category, modify lines 27-28 like this:

“`php
if (is_category(‘Music’)) {
$description = ‘Fill in the description for the Music category here’;
$keywords = ‘Music category keywords, separated by commas’;
} elseif (is_category(‘Movies’)) {
$description = ‘Fill in the description for the Movies category here’;
$keywords = ‘Movies category keywords, separated by commas’;
} // Repeat for additional categories
“`

Remember to save your changes in UTF-8 format and replace the original header.php file. For more information, explore other WordPress-related articles on Host Reference.

© 版权声明

相关文章

暂无评论

暂无评论...
☺一键登录开启个人书签等功能!