WordPress-前端上的帖子标题与url中的帖子标题不同 [英] Wordpress - different post title on frontend than in url

查看:111
本文介绍了WordPress-前端上的帖子标题与url中的帖子标题不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改Wordpress主题function.php文件,以便它将在前端显示帖子的自定义名称,这与帖子编辑器/URL中的标题不同.

I need to change Wordpress theme function.php file so it will show custom name for post on frontend which is different than the title in post editor/URL.

例如:文本编辑器/URL中的帖子标题为"New York",但在前端显示为"New York-never sleep city".这样做的原因是将URL缩短为基本元数据,并仅在前端(帖子,档案,站点地图,搜索结果...)上保留花式名称".对于花式名称",我想使用Yoast SEO插件中的SEO标题.

For instance: Post title in text editor/url is "New York" but on front end will be shown "New York - never sleeping city". The reason for this is to shorten URL to basic metadata and keep "fancy name" on frontend only (posts, archives, sitemap, search results...). For "fancy name" I would like to use SEO title from Yoast SEO plugin.

在我的情况下,页面基于Wordpress帖子类型.因此,我需要在某个地方定义不应该受到影响的帖子(页面)ID池.最好的解决方案也不是影响列出的ID下的所有后代页面.

In my case the pages are based on Wordpress posts types. So I need to define somewhere pool of posts(pages) IDs which should not be affected. The best solution is also NOT to affect all descendant pages under listed IDs.

问题是我应该如何修改function.php文件?

The question is how should I modify function.php file?

社区注释:如果您认为我的问题不清楚,请先写评论,然后再将我的问题标记为离题,以便我对其进行调整和完善.

推荐答案

如果我正确理解了您想要的内容,则可以将此代码放在主题的functions.php

If I understand what you want correctly, you can place this code in the functions.php of your theme

function set_my_seo_title($title, $id)
{
    global $post;
    $seo_title=get_post_meta($id, '_yoast_wpseo_title', true);         
    return ((!empty($seo_title)&&$post->post_type=='post') ? $seo_title : $title);
}

add_filter('the_title', 'set_my_seo_title', 15, 2);

这将检查帖子是否设置了SEO标题.如果是,则使用它;否则,则使用常规的帖子标题.

This will check if post got SEO title set. If yes, it will be used, if no - it will use regular post title.

这篇关于WordPress-前端上的帖子标题与url中的帖子标题不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆