wordpress自定义字段输出后the_content
我想要做的是在the_content之后和插件之前输出一个自定义字段内容(这是一个动态链接插入到每个职位的自定义字段的值的按钮) 。wordpress自定义字段输出后the_content
这是自定义字段代码:
<div class="button">
<a href="<?php echo get_post_meta($post->ID, 'Button', true); ?>">
<img src="<?php echo get_template_directory_uri() . '/images/button.png'; ?>" alt="link" />
</a>
</div>
WordPress的抄本我也发现了如何应用过滤器,以获得类似于我想要的东西the_content这个例子。这是代码:
add_filter('the_content', 'my_the_content_filter', 20);
function my_the_content_filter($content) {
if (is_single())
// Add image to the beginning of each page
$content = sprintf(
'<img class="post-icon" src="%s/images/post_icon.png" alt="Post icon" title=""/>%s',
get_bloginfo('stylesheet_directory'),
$content
);
// Returns the content.
return $content;
}
的问题是我不知道PHP,我不知道我怎么修改上面的代码适用于我的具体情况。
我修改了一下,我设法列出按钮,但只在the_content之前,没有启用自定义字段的PHP。
add_filter('the_content', 'my_the_content_filter', 20);
function my_the_content_filter($content) {
if (is_single())
// Add button to the end of each page
$content = sprintf(
'<img class="button-link" src="%s/images/button.png" alt="Link" title=""/>%s',
get_bloginfo('stylesheet_directory'),
$content
);
// Returns the content.
return $content;
}
您可以在这里看到的输出:http://digitalmediaboard.com/?p=6583(它的右上方 '见真章' 按钮)
$content .= sprintf(...); // will add the button right after content.
在您的例子
// Add button to the end of each page
$content = sprintf(
'<img class="button-link" src="%s/images/button.png" alt="Link" title=""/>%s',
get_bloginfo('stylesheet_directory'),
$content
);
改变它
$lnk=get_bloginfo('stylesheet_directory');
$content .= '<img class="button-link" src=$lnk."/images/button.png" alt="Link" title=""/>';
添加新内容/ bu内容之后。此外,您还需要为该按钮添加一些css
样式,以根据您在内容之后/之后的需要进行放置。
我认为您可以轻松编辑index.php
,并且可以在内容之后立即添加您提供的代码。
更新:
add_filter('the_content', 'my_the_content_filter', 20);
function my_the_content_filter($content) {
if (is_single())
{
global $post;
$imgLnk=get_bloginfo('stylesheet_directory');
$pgLnk=get_post_meta($post->ID, 'Button', true);
$content .= '<a href="'.$pgLnk.'"><img class="button-link" src=$lnk."/images/button.png" alt="Link" title=""/></a>';
}
return $content;
}
我得到一个解析错误:语法错误,意外的')'后,我替换了代码。我在PHP上真的很糟糕...... – 2012-04-15 20:02:18
您应该检查发生错误的行。 – 2012-04-15 20:04:17
删除sprintf并尝试。 – 2012-04-15 20:06:33
只是一个小的修改上面的代码。之前的'img'属性在Safari上输出'image not found'图标(问号)。我简单地用'span'替换了'img',并将图像添加为CSS中的背景。
add_filter('the_content', 'my_the_content_filter', 0);
function my_the_content_filter($content) {
if (is_single())
{
global $post;
$pgLnk=get_post_meta($post->ID, 'Button', true);
$content .= '<div id="button-link"><a href="'.$pgLnk.'" target="_blank"><span class="button-link"></span></a></div>';
}
return $content;
}
你为什么不编辑content.php文件? – 2012-04-15 18:51:36
这个主题没有一个;这不是问题,因为我可以创建页面,但我不确定它会解决问题;我有插件自动插入他们的元素后the_content,我已经试图把代码放在single.php中,但它出现在插件后的最后一页。从我阅读的内容来看,只有两种方法可以做我想做的事情:在content.之前或之后手动将每个插件包含在single.php中,因此可以完全控制所列内容或在the_content上添加过滤器函数,这是最优雅的解。 :) – 2012-04-15 20:11:08