如何在 WordPress 中的摘录中添加额外的链接

发布于 2024-09-18 14:31:06 字数 1509 浏览 3 评论 0 原文

我试图让这个链接看起来像这样:

评论这个节目>> |听这个节目>>

其中“对此节目发表评论>>”正确填充其永久链接。

“听这个节目>>”链接应填充帖子“立即收听”自定义字段值。

function holylandmoments_comment_link() {
return ' <a class="read-more-link" href="'. get_permalink() . '">' . __( 'Comment on this show &raquo;', 'holylandmoments-show' ) . '</a> &nbsp;|&nbsp; <a class="read-more-link" href="'. get_post_meta($post->ID, 'Audio File',true); . '">' . __( 'Listen to this episode &raquo;', 'holylandmoments' ) . '</a>';
}

问题是我没有获得“立即收听”的自定义字段值的路径来填充第二个链接...有什么想法吗?

自定义字段值是音频文件的链接。因此,对于属于该类别的所有帖子,显示有一个名为“音频文件”的自定义字段,该字段的值是:

http://www.mydomain.org/audio/sample.mp3

因此,当调用摘录来显示存档页面时,我需要两个链接来显示一个指向帖子的链接,另一个链接指向 MP3 文件。

所以在我的functions.php 文件中,我有上面的函数,然后我用以下内容调用它:

function holylandmoments_custom_excerpt_more( $output ) {
if ( has_excerpt() && in_category( _x('devotionals', 'devotionals category slug', 'holylandmoments') ) &&! is_attachment() ) {
    $output .= holylandmoments_read_more_link();
}
else
if ( has_excerpt() && in_category( _x('shows', 'shows category slug', 'holylandmoments') ) &&! is_attachment() ) {
    $output .= holylandmoments_comment_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'holylandmoments_custom_excerpt_more' );

谢谢!

马特

I'm trying to get this link to look like this:

Comment on this show >> | Listen to this show >>

Where "Comment on this show >>" gets populated properly with its permalink.

"Listen to this show >>" link should be populated with that posts 'Listen Now' custom field value.

function holylandmoments_comment_link() {
return ' <a class="read-more-link" href="'. get_permalink() . '">' . __( 'Comment on this show »', 'holylandmoments-show' ) . '</a>  |  <a class="read-more-link" href="'. get_post_meta($post->ID, 'Audio File',true); . '">' . __( 'Listen to this episode »', 'holylandmoments' ) . '</a>';
}

Problem is I don't get the path to the custom field value of Listen Now to populate the second link... any ideas??

The custom field value is a link to an audio file. So for all posts that fall under the category shows there is a custom field named 'Audio File' the value of that field is:

http://www.mydomain.org/audio/sample.mp3

So when the excerpt is called for archive pages to display I need two links to display one that points back to the post and another that points to the MP3 file.

So in my functions.php file I have the function above and then I call it with:

function holylandmoments_custom_excerpt_more( $output ) {
if ( has_excerpt() && in_category( _x('devotionals', 'devotionals category slug', 'holylandmoments') ) &&! is_attachment() ) {
    $output .= holylandmoments_read_more_link();
}
else
if ( has_excerpt() && in_category( _x('shows', 'shows category slug', 'holylandmoments') ) &&! is_attachment() ) {
    $output .= holylandmoments_comment_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'holylandmoments_custom_excerpt_more' );

Thanks!

Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-09-25 14:31:06

那里有一个额外的分号。

href="'. get_post_meta($post->ID, 'Listen Now',true); . '">'
                                                    ^

更改为:

href="'. get_post_meta($post->ID, 'Listen Now',true) . '">'

$post变量可能不在当前范围内,因此尝试将全局$post引入其中。

function holylandmoments_comment_link() {
   global $post;
   return ' <a class="read-more-link" href="'. get_permalink() . '">' . __( 'Comment on this show »', 'holylandmoments-show' ) . '</a>  |  <a class="read-more-link" href="'. get_post_meta($post->ID, 'Audio File',true); . '">' . __( 'Listen to this episode »', 'holylandmoments' ) . '</a>';
}

我相信函数 the_ID() 也会返回当前帖子的 ID,因此如果另一个函数不起作用,请尝试以下操作:

function holylandmoments_comment_link() {
   return ' <a class="read-more-link" href="'. get_permalink() . '">' . __( 'Comment on this show »', 'holylandmoments-show' ) . '</a>  |  <a class="read-more-link" href="'. get_post_meta(the_ID(), 'Audio File',true); . '">' . __( 'Listen to this episode »', 'holylandmoments' ) . '</a>';
}

You have an extra semicolon in there.

href="'. get_post_meta($post->ID, 'Listen Now',true); . '">'
                                                    ^

Change to:

href="'. get_post_meta($post->ID, 'Listen Now',true) . '">'

The $post variable may not be in the current scope, so try bringing in the global $post into it.

function holylandmoments_comment_link() {
   global $post;
   return ' <a class="read-more-link" href="'. get_permalink() . '">' . __( 'Comment on this show »', 'holylandmoments-show' ) . '</a>  |  <a class="read-more-link" href="'. get_post_meta($post->ID, 'Audio File',true); . '">' . __( 'Listen to this episode »', 'holylandmoments' ) . '</a>';
}

I believe the function the_ID() also returns the ID of the current post, so try the following if it the other one doesn't work:

function holylandmoments_comment_link() {
   return ' <a class="read-more-link" href="'. get_permalink() . '">' . __( 'Comment on this show »', 'holylandmoments-show' ) . '</a>  |  <a class="read-more-link" href="'. get_post_meta(the_ID(), 'Audio File',true); . '">' . __( 'Listen to this episode »', 'holylandmoments' ) . '</a>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文