[Resolved] Adding html for recipe price

Home Forums Support [Resolved] Adding html for recipe price

Home Forums Support Adding html for recipe price

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #1283336
    Mitch and Justine

    Hi,

    We are trying to add the total cost of recipes and cost per serving right below our H1 in single blog posts, but above the breadcrumbs.

    We want it to look similar to this blog: https://www.budgetbytes.com/vegetable-barley-soup/. We found this code by inspecting the website, but are not sure how to add it, how to change the color, and how to change the font.

    Here is the code we found: <div class="post-meta"> <span class="cost">$6.67 recipe / $1.11 serving</span></div>

    Thanks in advance,
    Justine and Mitch

    #1283786
    David
    Staff
    Customer Support

    Hi there,

    You can try following these steps:

    1. Create a Custom Field in your Post editor:

    https://wordpress.org/support/article/custom-fields/

    1.1 For the example give the field a Name of cost and put in your value eg. $6.67 recipe / $1.11 serving

    2. Now create a Hook Element in Appearance > Elements:

    https://docs.generatepress.com/article/hooks-element-overview/

    2.1 Add this code to the content:

    <?php
    $cost = get_post_meta( get_the_ID(), 'cost', true );
    if ( $cost ) {
        echo '<span class="recipe-cost">' . $cost . '</span>';
    }
    ?>

    2.2 Select after_entry_title from the Hook List.
    2.3 Check execute PHP.
    2.4 Set the Display Rules to Post > All Posts – i am assuming you’re using Posts for your recipes
    2.5 Publish your hook.

    3. Now we can use the recipe-cost class in our CSS:

    .recipe-cost {
        display: block;
        font-size: 16px;
        font-weight: 400;
        color: orange;
    }
    #1285895
    Mitch and Justine

    I’ve done everything, except add in the CSS code. Where do I add that in? Do I add it in from the main customization panel, or in each individual post?

    #1285943
    David
    Staff
    Customer Support

    You can add that once in Customizer > Additional CSS

    #1287570
    Mitch and Justine

    Okay we followed all those steps, but it’s not working still.

    #1287818
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Can you show us a screenshot of the custom field filled out in the back-end of the post so we can make sure it all looks right?

    #1291095
    Mitch and Justine

    Okay. How do I send a screenshot on here?

    #1291457
    David
    Staff
    Customer Support

    Save the screen shot to your Media Library or cloud/share drive and then share with us the URL to the image

    #1291921
    Mitch and Justine
    #1292245
    David
    Staff
    Customer Support

    Looks ok.
    For the Hook content code can you try this instead temporarily:

    <p>If you see this then the hook is working</p>
    <?php
    $cost = get_post_meta( get_the_ID(), 'cost', true );
    echo '<span class="recipe-cost">' . $cost . '</span>';
    ?>
    #1292259
    Mitch and Justine

    Okay, so the “if you see this then the hook is working” is showing up now!. But how do we change it to be individual prices for each post?

    #1292415
    David
    Staff
    Customer Support

    Silly question … when you added the Custom field did you click the Add Custom Field afterwards ?

    #1292630
    Mitch and Justine

    I did, yes

    #1292859
    David
    Staff
    Customer Support

    Aaah i think its because the Custom Field is case sensitive.
    Can you edit the custom field name. Change it from Cost to cost and hit update.

    #1294254
    Mitch and Justine

    Okay, that worked! Thanks so much. How can we also change the font size, weight, and color? We want it in #ff6060 and bolded.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.