最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

user meta - get_the_author_meta i want to write in a loop

matteradmin10PV0评论

I'm working on WordPress I'm trying to write foreach to the get_the_author_meta here href and <i class="fa fa-facebook"></i> coming dynamically how should i write in a loop this is my code

echo '<ul class="at__social">';
if ( get_the_author_meta( 'google' ) ) :
    echo '<li><a target="_blank" class="author__social" href="/' . esc_url( get_the_author_meta( 'google' ) ) . '?rel=author"><i class="fa fa-google-plus"></i></a></li>';
endif;
if ( get_the_author_meta( 'pinterest' ) ) :
    echo '<li><a target="_blank" class="author__social" href="/' . esc_url( get_the_author_meta( 'pinterest' ) ) . '"><i class="fa fa-pinterest"></i></a></li>';
endif;
if ( get_the_author_meta( 'tumblr' ) ) :
    echo '<li><a target="_blank" class="author__social" href="http://' . esc_url( get_the_author_meta( 'tumblr' ) ) . '.tumblr/"><i class="fa fa-tumblr"></i></a></li>';
endif;*/
echo '</ul>';

So far i have tried like this

<?php
    $at_social = '<ul class="at__social">';
    if(!empty($at_social)){
        echo $at_social;
        $author_sociables  = array(
            'facebook',
            'twitter',
            'instagram',
            'google',
            'pinterest',
            'tumblr'
            );
        //$at_link = ;
        //$at_icon = ;
        foreach($author_sociables as $value){
            if ( get_the_author_meta( $value ) ) :
                echo '<li><a target="_blank" class="author__social" href=" . $at_link . ' . esc_url( get_the_author_meta( $value ) ) . '"><i class="fa fa-facebook"></i></a></li>';
            endif;
        }
        echo '</ul>';
    }

I'm working on WordPress I'm trying to write foreach to the get_the_author_meta here href and <i class="fa fa-facebook"></i> coming dynamically how should i write in a loop this is my code

echo '<ul class="at__social">';
if ( get_the_author_meta( 'google' ) ) :
    echo '<li><a target="_blank" class="author__social" href="http://plus.google/' . esc_url( get_the_author_meta( 'google' ) ) . '?rel=author"><i class="fa fa-google-plus"></i></a></li>';
endif;
if ( get_the_author_meta( 'pinterest' ) ) :
    echo '<li><a target="_blank" class="author__social" href="http://pinterest/' . esc_url( get_the_author_meta( 'pinterest' ) ) . '"><i class="fa fa-pinterest"></i></a></li>';
endif;
if ( get_the_author_meta( 'tumblr' ) ) :
    echo '<li><a target="_blank" class="author__social" href="http://' . esc_url( get_the_author_meta( 'tumblr' ) ) . '.tumblr/"><i class="fa fa-tumblr"></i></a></li>';
endif;*/
echo '</ul>';

So far i have tried like this

<?php
    $at_social = '<ul class="at__social">';
    if(!empty($at_social)){
        echo $at_social;
        $author_sociables  = array(
            'facebook',
            'twitter',
            'instagram',
            'google',
            'pinterest',
            'tumblr'
            );
        //$at_link = ;
        //$at_icon = ;
        foreach($author_sociables as $value){
            if ( get_the_author_meta( $value ) ) :
                echo '<li><a target="_blank" class="author__social" href=" . $at_link . ' . esc_url( get_the_author_meta( $value ) ) . '"><i class="fa fa-facebook"></i></a></li>';
            endif;
        }
        echo '</ul>';
    }
Share Improve this question edited Oct 29, 2018 at 14:58 Krunal Pathak 1034 bronze badges asked Oct 29, 2018 at 10:54 HusnaHusna 1033 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your loop rewrite is good so far, yet you can make it better:

<?php
    $at_social_html = '<ul class="at__social">';
    /* array elements: social-media-name/meta-key => ['href-before-string', 'href-after-string', 'fa-class'] */
    $author_sociables  = array(
        'facebook' => ['', '', 'fa-fb'],
        'twitter' => ['', '', 'fa-twitter'],
        'instagram' => ['', '', 'fa-instagram'],
        'google' => ['//plus.google/', '', 'fa-google-plus'],
        'pinterest' => ['//pinterest/', '', 'fa-pinterest'],
        'tumblr' => ['//', '.tumblr/', 'fa-tumblr']
        );
    /* Loop */
    foreach($author_sociables as $key=>$value){
        $at_author_meta_value = get_the_author_meta( $key );
        if ( $at_author_meta_value ) :
            $at_social_html .= '<li><a target="_blank" class="author__social" href="'. $value[0] . esc_url( $at_author_meta_value ) . $value[1]'"><i class="fa '. $value[2] .'"></i></a></li>';
        endif;
    }
    $at_social_html .= '</ul>';

    echo $at_social_html;
Post a comment

comment list (0)

  1. No comments so far