the_content 后的 wordpress 自定义字段输出 [英] wordpress custom field output after the_content
问题描述
我想要做的是在 the_content 之后和插件之前输出一个自定义字段内容(这是一个带有动态链接的按钮,该链接被插入到每个帖子的自定义字段的值中).
这是自定义字段的代码:
在 wordpress codex 上,我还找到了这个示例,说明如何将过滤器应用于 the_content 以获得与我想要的类似的内容.这是代码:
add_filter( 'the_content', 'my_the_content_filter', 20 );函数 my_the_content_filter( $content ) {如果 ( is_single() )//在每一页的开头添加图片$content = sprintf('<img class="post-icon" src="%s/images/post_icon.png" alt="发布图标" title=""/>%s',get_bloginfo('stylesheet_directory'),$内容);//返回内容.返回 $content;}
问题是我不知道 PHP,我不知道我应该如何编辑上面的代码以适用于我的特定情况.
我稍微修改了一下,我设法列出了按钮,但只在 the_content 之前,并且没有启用自定义字段的 PHP.
add_filter( 'the_content', 'my_the_content_filter', 20 );函数 my_the_content_filter( $content ) {如果 ( is_single() )//在每页末尾添加按钮$content = sprintf('<img class="button-link" src="%s/images/button.png" alt="Link" title=""/>%s',get_bloginfo('stylesheet_directory'),$内容);//返回内容.返回 $content;}
您可以在此处查看输出:http://digitalmediaboard.com/?p=6583(这是右上角的show-me"按钮)
$content .= sprintf(...);//将在内容之后添加按钮.
在你的例子中
//每页末尾添加按钮$content = sprintf('<img class="button-link" src="%s/images/button.png" alt="Link" title=""/>%s',get_bloginfo('stylesheet_directory'),$内容);
改成
$lnk=get_bloginfo('stylesheet_directory');$content .= '<img class="button-link" src=$lnk."/images/button.png" alt="Link" title=""/>';
在内容之后添加新内容/按钮.您还需要为该按钮添加一些 css
样式,以便根据您在内容内/之后的需要放置.
我认为您可以轻松编辑 index.php
,并且可以在内容之后添加您随问题提供的代码.
更新:
add_filter( 'the_content', 'my_the_content_filter', 20 );函数 my_the_content_filter( $content ) {如果 ( is_single() ){全球 $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>';}返回 $content;}
What I want to do is output a custom field content (which is a button with a dynamic link that's being inserted in the value of the custom field of each posts) right after the_content and before the plugins.
This is the code for the custom field:
<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>
On wordpress codex I also found this example of how to apply a filter to the_content in order to obtain something similar to what I want. This is the code:
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;
}
The problem is I don't know PHP and I have no idea how am I supposed to edit the above code to apply on my specific case.
I modified it a bit and I manage to list the button, but only before the_content and without the PHP that enables the custom field.
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;
}
You can see the output here: http://digitalmediaboard.com/?p=6583 (it's the top-right 'show-me' button)
$content .= sprintf(...); // will add the button right after content.
In your example
// 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
);
change it to
$lnk=get_bloginfo( 'stylesheet_directory' );
$content .= '<img class="button-link" src=$lnk."/images/button.png" alt="Link" title=""/>';
to add new content/button right after content. Also you need to add some css
style for that button to be placed according to your desired need within/after content.
I think you can easily edit the index.php
and can add the code you've provided with your question right after content.
Update:
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;
}
这篇关于the_content 后的 wordpress 自定义字段输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!