gpt4 book ai didi

php - 如何将内容添加到 wordpress 循环?

转载 作者:行者123 更新时间:2023-12-04 02:20:35 24 4
gpt4 key购买 nike

我的插件有一些 if 语句,我想为每个帖子执行这些语句。

现在,假设我想在每个帖子中添加“Hello World”

我已经尝试了很多东西,但我似乎不太明白这一点。

很简单,在我的插件中我有:

add_filter('the_post','testing');

function testing($content){
echo "Hello World";
}

显然我做错了什么,因为 Hello World 没有这样做。谁能指出我正确的方向?

这是我主题的 index.php 页面上的工作代码: http://pastebin.com/xd1ree8W

我喜欢将 if 语句放在顶部,在我的插件的一个函数中,因此它会为每个帖子加载。

最佳答案

所以这适用于我在 functions.php

中使用它的安装

我没有修改 get_the_ID() 部分,但您可以像这样轻松地使用对象 $obj->ID

function everypost_func($obj){
echo 'This is sort of cool';

if (get_post_meta(get_the_ID(), 'other-link', true))
{
$url = get_post_meta(get_the_ID(), 'other-link', true);
$status = 200;
$price = NULL;
//echo "Looks like you've got other-link selected";
}
elseif (get_post_meta(get_the_ID(), 'generic-asin', true))
{
$status = 100;
$asin = get_post_meta(get_the_ID(), 'generic-asin', true);
//echo "Looks like you've got a generic-asin";

if($_COOKIE['countrycode'] == "GB")
{
$reg = "co.uk";
//echo "and we've detected you're in the UK <br>";
}
else
{
$reg = "com";
//echo "and we've detected you're not from the UK <br>";
}
}
else
{
if(($_COOKIE['countrycode'] == "GB") && get_post_meta(get_the_ID(), 'link-uk', true))
{
$asin = get_post_meta(get_the_ID(), 'link-uk', true);
$reg = "co.uk";
$status = 100;
//echo "looks like you're in the UK, and we have a UK link<br>";
}
elseif (get_post_meta(get_the_ID(), 'link-us', true))
{
$asin = get_post_meta(get_the_ID(), 'link-us', true);
$reg = "com";
$status = 100;
//echo "looks like you're not in the UK, but we have a US link for you<br>";
}
elseif (get_post_meta(get_the_ID(), 'link-uk', true)) {
$asin = get_post_meta(get_the_ID(), 'link-uk', true);
$reg = "co.uk";
$status = 100;
//echo "looks like you're not the UK, but we have a UK link<br>";
}
else
{
$status = 404;
//echo "looks like nothing is here for you<br>";
}
}


if($status == 100)
{
$results = get_aws_details($reg, $asin);
$price = $results[0][0];
$url = $results[1][0];
$wishlist = $results[2][0];
//echo "Success";
}
elseif($status == 404)
{
$price = NULL;
$url = get_the_permalink();
$wishlist = NULL;
//echo "404";
}
}

add_action('the_post','everypost_func');

因此考虑到您是在插件中执行此操作,这将取决于您构建它的上下文。

OOP 中的示例:add_action('the_post',array($this,'everypost_func'));

也许尝试让它在主题文件中工作,即 functions.php 然后如果它像你期望的那样工作然后将它移动到你的插件中并包含所有必要的好东西。

-- 将根据 OP 评论更新更多内容

关于php - 如何将内容添加到 wordpress 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30243358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com