如何覆盖 WordPress 中的标题和元标签?

How do I override title and meta tags in WordPress?

我的目标很简单...我在 WordPress 根目录中创建了一个名为 download.php:

的页面
<?php
    $heading_bs = 'My bla bla heading..';
    require_once('wp-load.php');
    get_header();
    // My code here...
    get_footer();
?>

如何在 <title>meta title 中插入 $heading_bs?

如何进行自定义描述?

典型的方法是使 download.php 成为模板,否则它将无法获得对 get_header() 和 get_footer() 所需的 WordPress 支持。

<title> 标签是在您的 header.php 模板文件中创建的。您可以在 header.php 中测试您的特殊模板并进行相应调整:

<title><?php echo (get_page_template() == 'download.php') ? $heading_bs : wp_title() ?></title>

您还可以测试是否存在特殊标题变量:

<title><?php echo (isset($heading_bs) && $heading_bs) ? $heading_bs : wp_title() ?></title>

或者如果您希望保留标准的标题声明:

<?php if (isset($heading_bs) && $heading_bs) : ?>
<title><?php echo $heading_bs ?></title>
<?php else : ?>
<title>...your current title...</title>
<?php endif; ?>