0

When a user registers on my Wordress site, a custom post (Athlete) is automatically created, with the user being assigned as the author. The custom post essentially acts as a profile page.

On their profile page, users fill out a bunch or info, and a total_score is calculated and saved as user meta. If they do not complete all of the forms - they won't have a total_score as it is calculated on submission.

I have created a custom archive page for the posts (athletes), and used Settings > Reading > Posts Page to set it as the default posts archive.

On the post preview template (created and looped using Ele Custom Skins and Elementor) I have added an element called #total_score_circle- seen in the screenshot below.

I would like to hide #total_score_circle on the post preview layout if there is no total_score in the author meta for that post.

The below code currently hides the #total_score_circle across all post previews, and not just the ones where total_score doesn't exist in the author meta. So my query is clearly off.

Any help would be greatly appreciated.

function total_score_display(){
        
    if (is_home()){
        
        global $post;
        $author_id=$post->post_author;
        
        $total_score = get_the_author_meta('total_score', $author_id);
        
        if(empty($total_score)) : ?>
            <style type="text/css">
                        #total_score_circle   {
                            display: none !important;
                        }
                    </style>
            <?php endif; 
        }
    }
        add_action( 'wp_head', 'total_score_display', 10, 1 );

enter image description here

The frontend has been created with Elementor Pro, and I have used Elementor Custom Skin to create the loop that displays the search results.

Andrew
  • 65
  • 5
  • 1- When you check for `is_home`, `get_queried_object_id` will return the id of the home page NOT the user id! 2- It's not clear how `total_score` gets created and by whom? Is every user an author? Why did you use `get_the_author_meta`? 3- This could easily be done in your template file, in the loop section, why are you trying to do it through `wp_head` action hook? **Please edit your question and include all f the details and the loop structure/php-html markup in your template!** – Ruvee Oct 27 '21 at 14:53
  • Hi Ruvee. Thanks for the help! I have updated the question and changed the code to a previous version I used, as it seemed more relevant (I misunderstood the functionality of get_queried_object_id()). I'm not too clued up on archive pages, which is why I'm here. – Andrew Oct 27 '21 at 16:06
  • That's ok! I understand, I was just trying to "reproduce" it on my end so that I could help you, then I saw some discrepancies! Again that's ok. The only thing that matters is a clear and well-explained question so that people could help you debug your code. – Ruvee Oct 27 '21 at 16:12
  • I worked on your task, and came across more questions. So you have a list of custom posts that you queried from database. Right? Using that query, you populated your `html template` which are those cards with `total score` on them. Right? Can you include the **HTML TEMPLATE** you used for those cards? AND also include the **QUERY** you used to get the data for those cards. – Ruvee Oct 27 '21 at 20:45
  • 1
    If an author of a post does not have a score set, then there should not be any empty green circle on the card. Is that right? If so, then the way you're trying to solve it using the code you provided is incorrect. We would need to do it through "your custom template and query" **NOT** through "wp_head and css". – Ruvee Oct 27 '21 at 21:15
  • Hi Ruvee. Thanks again for the assistance. I've populated the page using Elementor as the frontend plugin, and Elementor Custom Skins to create the loop, so I'm not sure how much the HTML code will help - but I've included the HTML in a question edit anyway. RE the score, you're exactly right - if the total_score doesn't exist, then the green circle should disappear. The query is the standard Wordpress search query. – Andrew Oct 29 '21 at 07:36

1 Answers1

0

Thanks to Ruvee's guidance, I managed to solve my problem through implementing a shortcode on the actual custom skin page using Elementor and the below PHP.

Essentially I created a shortcode that displayed the value with a custom CSS class .total_score_circle and then used another shortcode to run the if/else statement.

If total_score exists, return do_shortcode(), if not return a separate, irrelevant CSS class.

I'm sure it's not the elegant way to do it, but worked a treat with Elementor.

    // Create shortcode to show total_score
    
    add_shortcode('total_score_sc', 'total_score_sc');
    function total_score_sc ($atts) {

    $total_score = get_the_author_meta( 'total_score');
    
    $total_score_class =  '<div class="total_score_circle" >';
    $total_score_class .= $total_score;
    $total_score_class .= '</div>'; 
    return $total_score_class;
    }
    
    // Create shortcode to replace above shortcode if total_score not present
    
    add_shortcode('final_total_score_sc', 'final_total_score_sc');

    function final_total_score_sc() { 
    global $post;
    
    $author_id = get_post_meta( get_queried_object_id(), 'author_id' );
    $total_score = get_the_author_meta( 'total_score', $author_id );

    $sR =  '<div class="total_score_circle_empty" >';
    $sR .= '</div>'; 

    if(empty($total_score))
        return $sR;
    else
        return do_shortcode( '[total_score_sc]' );
    }
Andrew
  • 65
  • 5