dongxian4531 2016-10-20 08:55
浏览 47
已采纳

如何在帖子中的第2段后显示Wordpress自定义字段

am using genesis framework. used this code in functions.php file. but displayed data in a table format at the end of post only. But i want to display data after Nth paragraph in a post only.

function add_product_spec_to_content($content) {
if(get_field('befestigung') || get_field('gurtsystem') || get_field('gruppe') || get_field('typ') || get_field('amazon') || get_field('bezug') || get_field('gewicht')) { $content = $content . '<table id="product-specification" cellspacing="0" border="0" style="border-collapse:collapse;"><tbody>'; }
if(get_field('befestigung')) { $content = $content . '<tr><td>Befestigung</td><td>' . get_field('befestigung') . '</td></tr>'; }
if(get_field('gurtsystem')) { $content = $content . '<tr><td>Gurtsystem</td><td>' . get_field('gurtsystem') . '</td></tr>'; }
if(get_field('bezug')) { $content = $content . '<tr><td>Bezug</td><td>' . get_field('bezug') . '</td></tr>'; }
if(get_field('gruppe')) { $content = $content . '<tr><td>Gruppe</td><td>' . get_field('gruppe') . '</td></tr>'; }
if(get_field('typ')) { $content = $content . '<tr><td>Typ</td><td>' . get_field('typ') . '</td></tr>'; }
if(get_field('gewicht')) { $content = $content . '<tr><td>Gewicht</td><td>' . get_field('gewicht') . ' kg</td></tr>'; }
if(get_field('amazon')) { $content = $content . '<tr><td>Preis</td><td>' . get_field('amazon') . '</td></tr>'; }
if(get_field('befestigung') || get_field('gurtsystem') || get_field('gruppe') || get_field('typ') || get_field('amazon') || get_field('bezug') || get_field('gewicht')) { $content = $content . '</tbody></table>'; }

return $content; } add_filter( 'the_content', 'add_product_spec_to_content', 1150 );
  • 写回答

1条回答 默认 最新

  • dongyin6576 2016-10-20 09:18
    关注

    You can create a shortcode for this und place it where you want:

    function sc_my_table( $atts ) {
    
        $fields = array(
            'befestigung' => 'Befestigung',
            'gurtsystem' => 'Gurtsystem',
            'bezug' => 'Bezug',
            'gruppe' => 'Gruppe',
            'typ' => 'Typ',
            'gewicht' => 'Gewicht',
            'amazon' => 'Preis',        
        );
    
        $table_open = false;
        $content = '';
    
        foreach ( $fields as $field => $name ) {
    
            if ( $value = get_field( $field ) ) {
                if ( ! $table_open ) {
                    $content .= '<table id="product-specification" cellspacing="0" border="0" style="border-collapse:collapse;"><tbody>';
                    $table_open = true;
                }
    
                $content .= '<tr><td>'. $name .'</td><td>' . $value . '</td></tr>';
            }
        }
    
        if ( $table_open ) {
            $content .= '</tbody></table>';     
        }
    
        return $content;
    
    } 
    
    add_shortcode('my-table', 'sc_my_table');
    

    Then use [my-table] in text editor at the desired place.

    Reference: add_shortcode()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 MATLAB四叉树处理长方形tif文件
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了