Support » Plugins » Hacks » Call Custom Field inside shortcode or php within the template

  • Resolved liiam

    (@liiam)


    Hi

    I am using the Simple WP Shopping Cart

    They offer short code and php options for inserting a button which is:

    <?php echo print_wp_cart_button_for_product(’PRODUCT-NAME’, PRODUCT-PRICE); ?>

    or

    [wp_cart:PRODUCT-NAME:price:PRODUCT-PRICE:end]

    As you can see for each button you need to set the name and price.

    I have made a custom post type with custom fields using ACF plugin (advanced custom fields plugin)

    I have successfully called some of those custom fields into my template but I would like to insert the “price” custom field and the “post title” into the shortcode (or php) of the shop button.

    for example

    echo '<h2>';the_title();echo '</h2>';
    	echo the_field('price');
    	 echo '<img src="';the_field('image');echo '">';
            echo print_wp_cart_button_for_product(‘echo the_title();’, echo the_field('price'); );
             the_excerpt();

    I have tried several variations but it either doesnt do anything or it breaks the page.

    I am calling the custom post type in groups of 3 as well, in line with the layout I want to achieve so the full code to pull the posts is

    <?php
    
    // Show a selected number of posts per row
    $posts_per_row = 3;
    $posts_per_page = 9;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'posts_per_page' => $posts_per_page,
       'paged' => $paged,
       'post_type' => product,
    );
    query_posts($args);
    if (have_posts()) {
       while (have_posts()) {
          the_post();
          if ((++$post_counter % $posts_per_row) == 1  || $posts_per_row == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of post_row -->\n";  // End previous row
             }
             echo "<div class='post_row'>\n";  // Start a new row
          }
          echo "<div class='grid_4'>\n";  // Start one post
             // Output post data here
             echo '<h2>';the_title();echo '</h2>';
    	echo the_field('price');
    	 echo '<img src="';the_field('image');echo '">';
            echo print_wp_cart_button_for_product(‘wordpress’, 7.50);
             the_excerpt();
          echo "</div><!-- End of post_class -->\n";  // End of post
       } ?>
       </div><!-- End of post_row -->
       <div class='clear'></div>
       <div>
          <div><?php next_posts_link('« Older Entries') ?></div>
          <div><?php previous_posts_link('Newer Entries »') ?></div>
       </div>
    <?php } else {
       // Code for no posts found
    }
    ?>

    If that helps any further.

    Hopefully I have explained clearly enough that someone might know what I want to do, even if you dont know how to solve it but you know what terms I should be googling for etc please let me know.

    Thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • lets see if this can help.

    First of all, you do not use “echo” like that.
    I’ll give you an example:

    function output_pork() {
    echo ‘<h2>pork</h2>
    another pork’;
    }

    this function outputs pork on one line then “another pork” on the next.

    Hi
    Thanks for your response. I have been playing around today and I discovered how to turn my custom field into a variable and then echo that variable on the page.

    That works ok.

    What I am still struggling with is echoing / outputting / filling in the button specific php with the variable.

    <?php echo print_wp_cart_button_for_product('I NEED VARIABLE HERE’, DIFFERENT ONE HERE); ?>

    if I do

    $priceinbutton = get_field('price');
     $iteminbutton = get_field('title');

    Then

    echo $priceinbutton;
     echo $iteminbutton;

    Outputs the value i.e. 50

    What is the working version of this..

    <?php echo print_wp_cart_button_for_product('echo $iteminbutton;’, echo $priceinbutton;); ?>

    please?

    Thanks Guys

    <?php
    function somthing() {
    $priceinbutton = get_field(‘price’);
    $iteminbutton = get_field(‘title’);

    echo print_wp_cart_button_for_product(”.$iteminbutton.”, ”.$priceinbutton.” );
    }
    ?>

    it is not double quotes, but two single quotes then a dot

    or try this

    <?php
    function somthing() {
    $priceinbutton = get_field(‘price’);
    $iteminbutton = get_field(‘title’);

    echo print_wp_cart_button_for_product(”.$iteminbutton.”, $priceinbutton );
    }
    ?>

    or this

    <?php
    function somthing() {
    $priceinbutton = get_field(‘price’);
    $iteminbutton = get_field(‘title’);

    echo print_wp_cart_button_for_product($iteminbutton, $priceinbutton );
    }
    ?>

    THANK YOU!

    So I went with `function somthing() {
    $priceinbutton = get_field(‘price’);
    $iteminbutton = get_field(‘title’);

    echo print_wp_cart_button_for_product($iteminbutton, $priceinbutton );
    }`

    it didnt work straight away but I realised that was my fault since I said the title was a custom field but it wasnt it was just the wordpress post title.

    so I amended it to

    $priceinbutton = get_field('price');
    $iteminbutton = get_the_title();

    and it worked like a dream!

    Thanks so much

    awesome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Call Custom Field inside shortcode or php within the template’ is closed to new replies.