如何添加一段 html 代码以在每个 WordPress 页面上运行 [英] How can I add a piece of html code to run on every WordPress page

查看:40
本文介绍了如何添加一段 html 代码以在每个 WordPress 页面上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的挑战(我不是 WordPress 插件开发人员).我正在使用一个以 OptimizePress 作为主题的网站.Optimizepress 在构建页面时多次调用 the_content.

I have an interesting challenge that I'm coming up short on (I'm not a WordPress plugin developer). I am using a site that has OptimizePress as a theme. Optimizepress calls the_content many times in building a page.

我想在每个页面的正文中包含一个小代码片段.

I want to include a little code snippet in the body of every page.

if ( function_exists( "transposh_widget" ) ) {
    transposh_widget(array(), array(
        'title'       => 'Translation',
        'widget_file' => 'flags/tpw_flags_css.php',
    ) );
}

这会显示几个标志,让您可以在网站上切换语言.所以我需要一个 div 设置为固定位置/0,0(我可以稍后调整外观),其中包含它.

That shows a couple of flags and lets you switch languages on the site. So I need a div set to fixed position / 0,0 (I can adjust later for looks) that has that in it.

除了在 the_content 中输出一些东西,这似乎是一种流行的方法,在 wordpress 网站的每个页面上实现这一点的最佳方法是什么?

Other than outputting something in the_content, which seems to be the popular way to do this, what is the best way to achieve getting this on every page of a wordpress site?

更新

下面的代码开始 - 这是我的最终代码,它只在每个页面的网站右上角输出标志.

The code below started - here was my final code that output flags only in the top right corner of the site on every page.

<?php
/*
Plugin Name: Transposh Flags in Header
Description: Embed Transposh Header Code (in Footer of Page)
*/

function insert_my_footer() {
    echo '<div style="display:block; top:30px; right: 50px; position: fixed; color:white;">';
  if (function_exists("transposh_widget")) {
    transposh_widget(array(),
                     array('title'=>'',
                           'widget_file' => 'flags/tpw_flags.php') );
    }
    echo '</div>';
}

add_action('wp_footer', 'insert_my_footer');
?>

推荐答案

好吧,我对 transposh_widget 一点也不熟悉,所以我真的不知道您希望这段代码在何处或何时运行.但是,制作一个向页面底部添加内容的插件很容易,所以也许您可以尝试一下.

OK, I'm not at all familiar with transposh_widget, so I don't really know where or when you want this code to run. However, it's easy to make a plugin that adds things to the bottom of a page, so perhaps you could try that.

这是一个让您入门的基本插件:

Here's a basic plugin to get you started:

<?php
/*
Plugin Name: html in foot
Description: embed html at the foot of every page
*/

function insert_my_footer() {
  if (function_exists("transposh_widget")) {
    transposh_widget(array(),
                     array('title'=>'Translation',
                           'widget_file' => 'flags/tpw_flags_css.php') );
    }
    echo <<<END_SCRIPT
<!-- This will be inserted at the foot of every page -->
END_SCRIPT;
  }
}

add_action('wp_footer', 'insert_my_footer');
?>

要安装此插件,请将其保存在您的 /wp-content/plugins 目录中(具有适当的所有权和权限),然后通过 Wordpress 管理页面激活它.

To install this plugin, save it in your /wp-content/plugins directory (with appropriate ownership and permissions), and then activate it via the Wordpress admin pages.

这篇关于如何添加一段 html 代码以在每个 WordPress 页面上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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