• I have a custom plugin to to display some author’ info in the posts, in this plugin I did a duplicate of a function in order to display some more content, different from the author description, so I tryed with

    // Get author's biographical information or description
    $user_description = get_the_author_meta( 'user_description', $post->post_author );
    
    // Get agency period of rentals
    $periodo_affitti = get_the_author_meta( 'periodo_affitti', $post->post_author );
    

    $periodo_affitti is clearly wrong, but I cant use $user_description because if I do the second content overwrites the first one

    Here is the custom extra field tied to the second function:

    th><label for="periodo_affitti">Periodo affitti</label></th>
                <td>
                   <textarea id="periodo_affitti" name="periodo_affitti"rows="5" cols="30" class="regular-text" ><?php echo esc_attr( get_the_author_meta( 'periodo_affitti', $user->ID ) ); ?></textarea><br />
                    <span class="periodo_affitti">periodo affitti</span>
    

    So which $…?

    Another problem is that I’d like to print those 2 infos in 2 different positions of the posts, how can I manage the position/priority of each post content?

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 21 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s not clear to me why $periodo_affitti is wrong. It looks like the information you want to display. You do need to get author meta within a template loop so that $post->post_author contains a valid value. Even then, you may need to declare global $post; somewhere on the template. Once the meta value is assigned to $periodo_affitti, you can echo it out anywhere you like within the scope of the loop it’s used in.

    Thread Starter sacconi

    (@sacconi)

    Here is the complete code:

    <?php
    /*
    Plugin Name: MySite-plugin
    Description: Site specific code changes for example.com
    */
    /* Start Adding Functions Below this Line */
    function wpb_author_info_box( $content ) {
     
    global $post;
     
    // Detect if it is a single post with a post author
    if ( is_single() && isset( $post->post_author ) ) {
     
    // Get author's display name
    $display_name = get_the_author_meta( 'display_name', $post->post_author );
     
    // If display name is not available then use nickname as display name
    if ( empty( $display_name ) )
    $display_name = get_the_author_meta( 'nickname', $post->post_author );
     
    // Get author's biographical information or description
    $user_description = get_the_author_meta( 'user_description', $post->post_author );
    
    // Get agency period of rentals
    $periodo_affitti = get_the_author_meta( 'periodo_affitti', $post->post_author );
     
    // Get author's website URL
    $user_website = get_the_author_meta('url', $post->post_author);
     
    // Get link to the author archive page
    $user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
     
    if ( ! empty( $display_name ) )
     
    $author_details = '<p class="author_name">Condizioni di pagamento ID:' . get_the_ID(). '</p>';
     
    if ( ! empty( $user_description ) )
    
    // Author avatar and bio
     
    $author_details .= '<p class="author_details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</p>';
     
    $author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';  
     
    // Check if author has a website in their profile
    if ( ! empty( $user_website ) ) {
     
    // Display author website link
    $author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>';
     
    } else {
    // if there is no author website then just close the paragraph
    $author_details .= '</p>';
    }
    
     
    // Pass all this info to post content
    $content = $content . '<footer class="author_bio_section" >' . $author_details . '</footer>';
    }
    return $content;
    }
     
    // Add our function to the post content filter
    add_action( 'the_content', 'wpb_author_info_box' );
     
    // Allow HTML in author bio section
    remove_filter('pre_user_description', 'wp_filter_kses');
     
    /* Stop Adding Functions Below this Line */
    ?>

    I just glanced at the code but you might be more likely to get help if you indented things correctly. And, search “PHP: brackets or no brackets with if/for/while/…?” then do what it says (use brackets always).

    Thread Starter sacconi

    (@sacconi)

    I found this plugin on the web, I wouldnt be able to put brackets where needed, sorry

    Thread Starter sacconi

    (@sacconi)

    Anyway just the following doesnt work:

    // Get agency period of rentals
    $periodo_affitti = get_the_author_meta( 'periodo_affitti', $post->post_author );
    Thread Starter sacconi

    (@sacconi)

    Should I write:

    // Get agency period of rentals
    $periodo_affitti
    
    {  get_the_author_meta( 'periodo_affitti', $post->post_author );
    }
    • This reply was modified 1 year, 3 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    What to do depends on where you want the output to occur. This part is fine:

    // Get agency period of rentals
    $periodo_affitti = get_the_author_meta( 'periodo_affitti', $post->post_author );

    This can go anywhere within the if is_single() conditional, as long as it’s before where you want the output.

    I say “output” but here you’re actually only queuing content for output. The actual output occurs later. So where ever you want to queue output, do something like this:
    $author_details .= '<p>'.'Periodo affitti: '. $periodo_affitti .'</p>';

    Thread Starter sacconi

    (@sacconi)

    maybe I have to add $periodo_affitti in this part of the plugin?

    // Pass all this info to post content
    $content = $content . '<footer class="author_bio_section" >' . $author_details . '</footer>';
    }
    return $content;
    }
    Moderator bcworkz

    (@bcworkz)

    You could. For example, before or after $author_details, like this:
    '<footer class="author_bio_section" >'. $author_details . $periodo_affitti .'</footer>'
    If that’s an acceptable location for you. I had suggested earlier that you concatenate it to $author_details because you’d have more control over exactly where it occurs among the other details. But if before or after works for you, you can use the line you suggested.

    Thread Starter sacconi

    (@sacconi)

    It works. The 2 contents are in the same section. Is it possible to create other custom section? I mean printing the content in a different position or just to style it in a different way. In the post now they have the same violet background https://test.sacconicase.com/lignano-sabbiadoro-alloggio-5-posti-ristrutturato-nel-2013-vicinanze-mare-e-centro/

    Thread Starter sacconi

    (@sacconi)

    I have for example this selection in author edit profile

      <select name="saldo" id="saldo">
    <option value="option0" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option0' ); ?>>-</option>
                        <option value="15 gg. prima dell'arrivo" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option1' ); ?>>15 gg. prima dell'arrivo</option>
                        <option value="All'arrivo" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option2' ); ?>>All'arrivo</option>
                        <!-- Add more options as needed -->
                    </select>
                    <br />
                    <span class="description">Seleziona le condizioni di pagamento per il saldo</span>
    

    but in the post I print option1 or option2 but not the value of the option1 or option2

    In the plug in to print on post page I have

    // Get saldo
    $saldo = get_the_author_meta( 'saldo', $post->post_author );
    

    and

    // Pass all this info to post content
    $content = $content . '<footer class="author_bio_section" >'.   $periodo_affitti . $author_details . $saldo .'</footer>';
    }
    return $content;
    }
    
    Thread Starter sacconi

    (@sacconi)

    I’d like also to know I can print the content in this position, ater the post description: https://ibb.co/x8kDDQL

    Moderator bcworkz

    (@bcworkz)

    The grid of photos is part of post content, so you’d want the violet box inserted between text and photo content. To do that you need a shortcode (or custom block). Shortcodes are something I understand well. Custom blocks not so much.

    The wpb_author_info_box() function can be made into a shortcode handler with something like:
    add_shortcode('auth_info', 'wpb_author_info_box');
    The function will need some modification as well (details below). As a shortcode, you will remove the related add_action() call. (FWIW, “the_content” is really a filter, not an action. The two are closely related so add_action() can work, but it’s semantically incorrect.)

    To use the shortcode, add “[auth_info]” to the post’s content where you want the violet box to appear.

    Documentation on creating shortcodes is available. You only need to be concerned with the most basic form of shortcode. There doesn’t need to be any attributes, nor is there a need for the self-closing form.

    I think maybe the only change you really need in your function is to remove $content from the return value. For the line before return, do something like:
    $content = '<footer class="author_bio_section" >'. $periodo_affitti . $author_details . $saldo .'</footer>';

    If your $saldo value is not what you expect, the issue lies in the code used to save the selected value. It’s saving “option2” instead of “All’arrivo”. If you’d rather not deal with altering the saving code, you could make use of the existing scheme and place the logic in the output code. Something like:

    switch ( get_the_author_meta( 'saldo', $post->post_author )){
      case 'option1':
        $saldo = '15 gg. prima dell'arrivo';
        break;
      // more option values could be added here
      default:
        $saldo = 'All'arrivo';
    };

    However, this is only workable if all posts have all the same possible selections. If the options vary by post, then you would need to deal with altering the saving code.

    You can break out $saldo and $periodo_affitti into different sections. Do you want different sections within the same <footer> element or outside of it? Either way, you wrap the meta value in appropriate HTML tags, like so:

    // Get saldo
    $saldo = '<div class="saldo">'. get_the_author_meta( 'saldo', $post->post_author ) .'</div>';

    You can then style the saldo div as desired using .saldo as the CSS selector.

    Whether $saldo is inside or outside of <footer> depends on where you insert $saldo in the final assignment to $content before return. The above example assumes you’ve corrected the saving code to save the correct value instead of “option1” and the previously suggested switch/case logic is not used.

    • This reply was modified 1 year, 3 months ago by bcworkz. Reason: code format fixed
    Thread Starter sacconi

    (@sacconi)

    This

    `// Get saldo
    $saldo = ‘<div class=”saldo”>’. get_the_author_meta( ‘saldo’, $post->post_author ) .'</div>’;

    broke the site

    Thread Starter sacconi

    (@sacconi)

    lets imagine another element than <footer>, a container which is above the <footer> where I can display other user/author custom extra fields. The title of this container is “Il prezzo non include” (the price doesnt include). More in general how can I say to the system to display a container(element above or under another? there is a system of weights? So the higher number puts the element in an upper position?

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘which is che correct $ ?’ is closed to new replies.