duanpa2143 2012-04-15 18:26
浏览 100
已采纳

the_content之后的wordpress自定义字段输出

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)

  • 写回答

2条回答 默认 最新

  • dqh19413 2012-04-15 18:59
    关注
    $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题