【问题标题】:Including a button at the bottom of all WordPress posts to link to set custom fields links在所有 WordPress 帖子的底部包含一个按钮以链接以设置自定义字段链接
【发布时间】:2019-10-16 15:39:21
【问题描述】:

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

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

这是我拥有的代码,但它当前链接回显示按钮的帖子,而不是在单个帖子上设置的 URL。

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" );

通过将此代码添加到主题本身,我目前在某些帖子类型上工作,但它不适用于我使用的所有帖子类型,我无法将其复制到其他帖子,所以这就是原因我正在尝试将其添加到 functions.php 文件中,以便在每个帖子内容之后显示。

<?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; ?>

任何帮助都会受到极大的欢迎,因为我已经为此绞尽脑汁了一段时间了。

【问题讨论】:

  • 为什么不将这第二部分代码添加到 single.php 模板文件中?
  • 您的所有帖子类型中都有franchise_profile_url,还是只是链接呈现的自定义帖子类型?只是通过阅读您的问题,我的第一个猜测是他们不会,因为您是根据该字段有条件地呈现链接

标签: php wordpress custom-fields


【解决方案1】:

试试这个


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" );

如果franchise_profile_url 字段存在,则只会在单个帖子上显示链接。

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多