使用页面标题+正文的第一句话
-
-
尽管这可能不能直接帮助解决问题,但可以为您提供下一步的了解.[WordPress中的中继标记](https://codex.wordpress.org/Meta_Tags_in_WordPress).YoastSEO还是您可以研究的使用最广泛的SEO插件之一.我*相信*它有一个自动填充的元描述字段.While this may not help solve the problem directly, it might provide you with insight on where to go next. [Meta Tags in WordPress](https://codex.wordpress.org/Meta_Tags_in_WordPress). YoastSEO is also one of the more widely used SEO plugins that you could look into. I *believe* it has an auto-populate meta description field.
- 0
- 2016-11-10
- Greg McMullen
-
感谢您的评论.我打算激活yoast,但目前yoast正在使用OG描述作为社交媒体等,而不是普通的meta description标签(我俩都在这之后)感谢Greg的建议Thank you for your comment. I am planning to activate yoast but currently yoast is using OG description for social media etc and not the normal meta description tag (i'm after both) Appreciate the suggestion Greg
- 0
- 2016-11-10
- d.ariel
-
您可以在"设置">"标题和元"中设置"元描述"模板,但是在"关键字"设置下可以选择调整元描述的选项.You can setup a Meta Description template in the settings > titles & metas, but it does have the option under the post "Keywords" settings to adjust the meta description.
- 0
- 2016-11-10
- Greg McMullen
-
1 个回答
- 投票数
-
- 2016-11-10
您可以使用
wp_head
操作将一些内容添加到head部分.您可以根据需要更改输出.<?php add_action('wp_head','add_meta_des'); function add_meta_des() { if(is_single()) { global $wp_query; $post = $wp_query->post; $page_id = $post->ID; $page_object = get_page( $page_id ); $content = wp_trim_words($page_object->post_content,30); $output="<meta name='description' content='".get_the_title()."--".$content."'/>"; echo $output; } } ?>
You can use
wp_head
action to add something to head section. You can change the output according your needs.<?php add_action('wp_head','add_meta_des'); function add_meta_des() { if(is_single()) { global $wp_query; $post = $wp_query->post; $page_id = $post->ID; $page_object = get_page( $page_id ); $content = wp_trim_words($page_object->post_content,30); $output="<meta name='description' content='".get_the_title()."--".$content."'/>"; echo $output; } } ?>
-
我认为,在这种特殊情况下,使用is_singular()代替使用is_single()更为广泛.I think, instead of using `is_single()`, using `is_singular()` is more broad in this particular situation.
- 2
- 2016-11-10
- Mayeenul Islam
-
Ranuka我把它放在我的functions.php文件的底部,它确实显示了,但是有些错误. #1由于某种原因,它显示在页面底部,而不是其他元数据所在的顶部, #2它没有摘录.我不是100%不确定.get_the_exceprt()应该如何工作,或者这仅仅是一个示例,但是get标题确实按预期工作了.当使用`.get_the_excerpt()`时,是否自动获取主体内容的前几个单词?Ranuka I put this at the bottom of my functions.php file and it did display but there were a couple of things wrong. #1 It showed up for some reason at the bottom of the page instead of at the top where my other meta data is located and #2 It did not get an excerpt. I'm not 100% sure how the `.get_the_exceprt()` is supposed to work or if that was just a sample but the get the title did work as expected. When using `.get_the_excerpt()` does that automatically get the first few words of the main body content?
- 0
- 2016-11-10
- d.ariel
-
有关WordPress摘录的更多信息:https://codex.wordpress.org/Excerpt,如果您想从内容中获取它,请阅读http://wordpress.stackexchange.com/questions/141466/wordpress-function-template-tag-to获取内容的第一个n字.并且`add_action`还有另外两个可选参数.在此处检查:https://developer.wordpress.org/reference/functions/add_action/您可以在正确的位置使用这些参数.More info about WordPress excerpt : https://codex.wordpress.org/Excerpt and if you want to get it from content read http://wordpress.stackexchange.com/questions/141466/wordpress-function-template-tag-to-get-first-n-words-of-the-content . And `add_action` has two another optional Parameters. Check it here: https://developer.wordpress.org/reference/functions/add_action/ You can use those parameters display in right place.
- 0
- 2016-11-10
- Ranuka
-
@Ranuka我仔细阅读了您发送给我的添加操作页面,但无法从该页面确定如何更改元描述在页面上的插入位置.可能优先吗?但是,这将决定整个页面或所有功能的优先级吗?如果我将优先级设置为1,那是否意味着它会在标头顶部附近的CSS等之前加载到顶部?我也阅读了有关摘录的其他链接,使用您的代码应该可以正常工作,并从页面中提取了"自动"摘录,但由于某种原因,它没有,仅使用了标题?非常感谢附加信息@Ranuka I read over the add action page you sent me but was unable to identify from that page how I can change where on the page the meta description is inserted. Possibly priority? But will that dictate the priority for the entire page or all the functions? If I set priority to 1 would that mean it loads at the top before css etc near the top of the header? Also I read the other links regarding the excerpt, using your code should have worked and pulled the "automatic" excerpt from the page but for some reason it didn't and it only used the title? Additional info very appreciated
- 0
- 2016-11-10
- d.ariel
-
@ d.ariel Ya,出现问题了.我更新了代码.我对其进行了测试,并在我的网站上正常运行.现在,请尝试更新的代码,并让我知道结果.@d.ariel Ya, there was a problem. I updated the code. I tested it and it worked fine in my site. Now please try the updated code and let me know the result.
- 0
- 2016-11-10
- Ranuka
-
@Ranuka您更新的代码完全正确.您能否帮助将元数据放置在页面上的更高位置,最好更靠近标记.@Ranuka Your updated code is exactly right. Could you help with positioning the meta data higher up on the page preferably closer to the tag.
- 0
- 2016-11-10
- d.ariel
-
尝试使用add_action('wp_head','add_meta_des',1);而不是add_action('wp_head','add_meta_des');.如果不合适,则必须使用不同的数字进行检查..Try to use add_action('wp_head','add_meta_des',1); instead of add_action('wp_head','add_meta_des');. If it is not suitable you have to use different different numbers to check it.
- 0
- 2016-11-10
- Ranuka
-
谢谢,效果很好.我会将这个问题标记为已回答.如果您不介意@Ranuka,那么我对此主题还有一个疑问.我的元描述现在看起来像这样:
我想摆脱&#8211,我认为这是将其转换为特殊字符的方法,从标题到摘录中删除双连字符,并允许最多70个字符. Thank you it works great. I'll mark this question answered. I've got one more question though regarding this subject if you don't mind @Ranuka . My meta description now looks like this: `` I'd like to get rid of the – which I think is what it is converting into special characters, remove the double hyphen when it goes from title to excerpt and allow max 70 char.- 0
- 2016-11-10
- d.ariel
-
@Ranuka如果您认为这超出了最初的问题,我会很乐意将其标记为"已回答",如果您提出的是其他问题,请尝试我的运气.非常感谢您到目前为止所做的一切!@Ranuka If you think this is out of the scope of the initial question i'll gladly marked as answered and try my luck with another question if that's what you suggest. Thank you so much for everything this far!
- 0
- 2016-11-10
- d.ariel
-
我认为现在WordPress部分已经结束.您需要应用一些PHP函数来使其成为所需.阅读以下内容:http://stackoverflow.com/questions/4880930/how-to-convert-html-entities-like-8211-to-their-character-equivalents要获得前70个字符,您可以使用substr函数.阅读:http://stackoverflow.com/questions/3787540/how-to-get-first-5-characters-from-string.最好像在上一则评论中从http://stackoverflow.com/询问正常的编程问题.I think now WordPress part is over. You need to apply few PHP functions to make it as you want. Read this : http://stackoverflow.com/questions/4880930/how-to-convert-html-entities-like-8211-to-their-character-equivalents And to get first 70 chars you can use `substr` function. Read : http://stackoverflow.com/questions/3787540/how-to-get-first-5-characters-from-string. It is better to ask normal programming questions like you have asked in last comment, from http://stackoverflow.com/.
- 0
- 2016-11-10
- Ranuka
元描述将包含页面标题以及主要帖子内容中的几个单词或句子.
据我所知,目前没有生成元描述.您能推荐一种解决方法吗?我想避免使用肿的插件等,因为每个博客都有成千上万的帖子和页面,所以手动逐个浏览每个帖子是不可行的.