admin管理员组文章数量:1122832
I'd like to add some code from a single post theme file into a filter but I'm not sure how to do it because it includes the get_post_meta() function. I've searched but can't seem to figure it out.
Code in single-post.php
theme file:
$apoth_original_title = get_post_meta( get_the_ID(), 'original_blog_title', true);
$apoth_original_url = get_post_meta( get_the_ID(), 'original_url', true);
?>
<a id="apoth_readmore" class="w-btn style_solid size_medium color_primary icon_none" href="<?php echo $apoth_original_url?>"><span class="w-btn-label">Read More at <em><?php echo $apoth_original_title ?></em></span></a>
<?php
Attempted filter in functions.php
:
function apoth_readmore_link( $content ) {
if( is_single() ) {
$content .= '<a href="' . get_post_meta(get_the_ID(), 'original_url', true) . '"> Read More</a>' ;
}
return $content;
}
add_filter('the_content', 'apoth_readmore_link' )
I get that this isn't working because get_post_meta()
doesn't echo the custom field data, but I don't know how to assign that to a variable and then echo it in the context of a filter function. Or maybe there is some other way of doing this that I'm missing?
I'd like to add some code from a single post theme file into a filter but I'm not sure how to do it because it includes the get_post_meta() function. I've searched but can't seem to figure it out.
Code in single-post.php
theme file:
$apoth_original_title = get_post_meta( get_the_ID(), 'original_blog_title', true);
$apoth_original_url = get_post_meta( get_the_ID(), 'original_url', true);
?>
<a id="apoth_readmore" class="w-btn style_solid size_medium color_primary icon_none" href="<?php echo $apoth_original_url?>"><span class="w-btn-label">Read More at <em><?php echo $apoth_original_title ?></em></span></a>
<?php
Attempted filter in functions.php
:
function apoth_readmore_link( $content ) {
if( is_single() ) {
$content .= '<a href="' . get_post_meta(get_the_ID(), 'original_url', true) . '"> Read More</a>' ;
}
return $content;
}
add_filter('the_content', 'apoth_readmore_link' )
I get that this isn't working because get_post_meta()
doesn't echo the custom field data, but I don't know how to assign that to a variable and then echo it in the context of a filter function. Or maybe there is some other way of doing this that I'm missing?
1 Answer
Reset to default 0Try get_queried_object()
to reference the WP_Post
that created the page.
- if you're on a single post, it will return the post object
- if you're on a page, it will return the page object
- if you're on an archive page, it will return the post type object
- if you're on a category archive, it will return the category object
- if you're on an author archive, it will return the author object
- etc.
function apoth_readmore_link( $content ) {
$post = get_queried_object();
if ( get_class($post)==='WP_Post' ) {
$url = get_post_meta($post->ID, 'original_url', true);
$content .= sprintf( '<a href="%s">Read More</a>', $url, $url );
}
return $content;
}
add_filter('the_content', 'apoth_readmore_link' );
本文标签: Using custom fields in a filter hook
版权声明:本文标题:Using custom fields in a filter hook 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281705a1926403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
original_blog_title
andoriginal_url
fromfunctions.php
then render the link with filtered content? – jgraup Commented Nov 16, 2016 at 4:55