在所有 WordPress 帖子底部添加一个按钮 link 以设置自定义字段 links

Including a button at the bottom of all WordPress posts to link to set custom fields links

我想添加一个按钮,显示在我网站上每个 WordPress post 的底部。此按钮应 link 到使用自定义字段插件设置的特定 URL,然后在创建时在每个 post 上选择。

我已经成功创建了显示在每个 post 底部的按钮,但是,我无法引入我定义的自定义字段设置。

这是我的代码,但目前 link 回到显示按钮的 post,而不是在个人 [= 上设置的 URL 25=].

function wpb_after_post_content( $content ) {
    if ( is_single() ) {
        $content .= '<a href="'.$franchise_profile_url.'" target="_blank" class="franchise-profile-btn">Contact Franchise &rarr;</a>';
    }
    return $content;
}

add_filter( "the_content", "wpb_after_post_content" );

通过将此代码添加到主题本身,我目前可以在某些 post 类型上使用它,但它不适用于我使用的所有 posts 类型,我也不是能够为其他 post 复制它,所以这就是为什么我试图将它添加到 functions.php 文件以在每个 post 内容之后显示。

<?php if( get_field('franchise_profile_url') ) : ?>
    <a href="<?php the_field('franchise_profile_url'); ?>" target="_blank" class="franchise-profile-btn">Contact Franchise &rarr;</a>
<?php endif; ?>

任何帮助都非常受欢迎,因为我已经为此绞尽脑汁了一段时间。

试试这个


function wpb_after_post_content($content){
if (is_single() && get_field('franchise_profile_url')) {
    $content .= '<a href="'.get_field('franchise_profile_url').'" target="_blank" class="franchise-profile-btn">Contact Franchise &rarr;</a>';
}
    return $content;
}
add_filter( "the_content", "wpb_after_post_content" );

这将仅在单个帖子上显示 link,并且如果 franchise_profile_url 字段存在。