实施Wordpress自定义功能

时间:2018-08-10 21:32:07

标签: php wordpress

我正在处理的站点是广播节目。我有许多自定义字段,由Advanced Custom Fields提供,用于链接到音乐家的网站,社交资料等。在过去的几年中,我们使用了Genesis子主题来显示它们,并{{3} }效果很好。广播节目的主持人/制作人现在想转到非“创世纪”主题,而让它工作起来有些麻烦。

the code I adapted来自我们正在迁移的网站,其中包含有效的代码。 (“艺术家链接”框浮动在主副本的右侧。)

Here’s an example,此代码尚未显示。 (它将显示在同一位置。)FWIW,主题为Here’s the same post at the new site

这是代码:

 <?php

 add_action ('if_artist_links');

 function if_artist_links() {

// These are the custom field keys defined in the dashboard:
$metas_links = array(
    'website',
    'album', 
    'tour', 
    'twitter', 
    'facebook',
    'bandcamp',
    'soundcloud',
    'youtube',
    'instagram',
    'linkedin',
    'myspace',
    'image',
);

// If _any_ ACF field is filled in, it's a go.
$has_meta = false; // init
foreach ($metas_links as $test ) {
    if ( get_field($test) ) {
        $has_meta = true;
        continue; // Just need one meta field filled to create the div.
    }
}
if ( $has_meta ) {
    echo '<div class="custom-data artist-links">';
    echo '<h2 class="artistname">' ?> <?php the_field('name') ?></h2>
    <center><strong> <?php the_field('tribe') ?></strong></center> <?php
    foreach ( $metas_links as $meta_links ) {
        $$meta_links = get_field($meta_links);
        if ( $$meta_links ) {
            $f_object = get_field_object($meta_links);
            $label = $f_object['label'];
            echo '<div class="' . $meta_links . '">' .
            '<a href="'. $$meta_links .'" target="blank"><span class="label">' . $label . '</a></span>' . 
            '</div>';
        }
    }
    echo '</div><!-- /.custom-data -->';
}
}

?>`

在有ACF值的地方,将创建一个链接。没有ACF值的地方就没有链接。仅需一个链接即可显示该框。

据我所知,我并没有告诉代码运行或以正确的方式运行。如果您可以帮助我做到这一点,我将在整个周末为您赞美。

如果可能的话,我真的想将其包装在WP插件中,以简化和维护,并在single.php中放入最少的代码。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要使用WordPress the_content钩子。将其替换为主题的“ functions.php”。

function single_add_to_content( $content ) {    
    if( is_single() ) {        
        // These are the custom field keys defined in the dashboard:
        $metas_links = array(
            'website',
            'album', 
            'tour', 
            'twitter', 
            'facebook',
            'bandcamp',
            'soundcloud',
            'youtube',
            'instagram',
            'linkedin',
            'myspace',
            'image',
        );

        // If _any_ ACF field is filled in, it's a go.
        $has_meta = false; // init
        foreach ($metas_links as $test ) {
            if ( get_field($test) ) {
                $has_meta = true;
                continue; // Just need one meta field filled to create the div.
            }
        }
        if ( $has_meta ) {
            $content.= '<div class="custom-data artist-links">';
            $content.= '<h2 class="artistname">'.get_field('name').'</h2>
            <center><strong>'.get_field('tribe').'</strong></center>';
            foreach ( $metas_links as $meta_links ) {
                $$meta_links = get_field($meta_links);
                if ( $$meta_links ) {
                    $f_object = get_field_object($meta_links);
                    $label = $f_object['label'];
                    $content.= '<div class="' . $meta_links . '">' .
                    '<a href="'. $$meta_links .'" target="blank"><span class="label">' . $label . '</a></span>' . 
                    '</div>';
                }
            }
            $content.= '</div><!-- /.custom-data -->';
        }
    }
    return $content;
}
add_filter( 'the_content', 'single_add_to_content' );

或者您也可以将“ single.php”复制到您的子主题中,然后直接添加此代码,而无需任何功能。