• This not working out for me. Any tips or example to make it work? post args = array($tags) didnt get value correctly.

    <h6>seal</h6>
    <h6>mark</h6>
    <?php
        $tags = "<script>document.write(h6);</script>"; 
     
    $args = array( 
        'post_type' => 'Rune', 
        'posts_per_page' => -1,
        'tag' => $tags,
    
    );
    $lastposts = get_posts( $args );
        
    foreach ( $lastposts as $post ) :
      setup_postdata( $post ); ?>
    	<?php the_content(); ?>
    <?php endforeach; 
    wp_reset_postdata(); 
    ?>
         
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    Using javascript to generate a tag argument makes no sense. Javascript only executes in a browser. PHP executes server side long before it gets to a browser. Is the intent in you example that “seal” and “mark” are to become tag arguments? Or is it seal OR mark? Then the tag argument has to be either “seal+mark” or “seal,mark” respectively. These values can be assigned to $tags before the HTML content is output. Once HTML is output it’s gone, you can’t do anything with it.

    Anytime you use setup_postdata( $post ), you need to ensure global $post; is declared somewhere within scope of your code.

    Post type arguments take slugs as arguments not presentation names, so it’s probably ‘rune’ and not ‘Rune’. Sometimes letter case does not matter, other times it does. It’s best to assume it always matters.

    You don’t need <?php ?> tags for every line if there is no intervening HTML. It doesn’t hurt, but it’s not necessary and makes for messy, inefficient code.

    Thread Starter apollo789

    (@apollo789)

    I want transfer value from created meta box, selected element go loop as a $tags
    Link Code

    Moderator bcworkz

    (@bcworkz)

    You need to get the selected value from where ever it’s stored in the DB. The metabox outputs the form field, some other code needs to save the selected field somewhere.

    In a similar vein, $options[“foo”] will not affect which field is selected because it is never assigned the saved value. The saved value is typically whatever the selected option value is, but you are determining selection on an integer while the option values are strings.

    Thread Starter apollo789

    (@apollo789)

    I was looking, but i couldnt find. I have 4options in metabox that I wanted. But i cant save, getting error. I want fix this and add that saved options go to page-templates! Could u help with this?
    Code Metabox

    • This reply was modified 8 years, 1 month ago by apollo789.
    Moderator bcworkz

    (@bcworkz)

    You should save the selected values through the rune_save() function. Where should the selection be saved? It can be anywhere in the DB. If the selection is related to the post being saved, post meta makes sense, so update_post_meta(() would be correct, but the parameters currently used in your code are undefined. Don’t rely on the global $post, it may not have the values you think it does.

    I don’t know what meta key you want to use, I’ll use “apo_runes”. It’s a good idea to prefix meta names with something unique in order to not conflict with other plugins. You can save the selected tag with this:
    update_post_meta( $post_id, 'apo_runes', $_POST['runeselect'] );

    Note that this is a terrible example because we should never put user input directly into the DB. The value in $_POST actually needs validation and sanitation. What I’ve presented will suffice temporarily, but you need to implement proper security before going live.

    When you output the field’s options, you can get the currently saved value if it exists with get_post_meta() and use it in the selected() function so that the previously saved option is preselected for the user. This part is optional. Without this, the first option is always selected.

    You can also use get_post_meta() anywhere on any template to get the saved value. You only need the associated post ID. If you are in the loop, get_the_ID() will return the current post ID.

    Thread Starter apollo789

    (@apollo789)

    almost working. How to put $meta to <options> now? How to put $meta in to page-template without using get_post_meta($post->ID, ‘apo_runes’, true);?
    Code

    Moderator bcworkz

    (@bcworkz)

    When you use the selected() function, you want to compare the current option value with a previously saved value. If there’s a match, selected() will output the “selected” option attribute so that option is the one displayed by default on the page. The dropdown is displayed when you call skillbox_callback( get_the_ID()). The option represented by $meta should be displayed in the dropdown.

    You cannot use $meta in the template without calling get_post_meta() again because it is local to your function. That’s how PHP works. You’d have to declare it as global within your function and anywhere it is to be used in order to use it outside the function. As a global, $meta is a poor name, it could easily clash with other variables using the same name. You should prefix it to use it globally, for example global $apo_meta;.

    Thread Starter apollo789

    (@apollo789)

    thanks a lot

    Moderator bcworkz

    (@bcworkz)

    Sure, no problem ??

    Hey, I just had a thought. A little unconventional, and it may not work for you, depending where you want to use $meta on the template. The skillbox_callback() is referred to as a “template tag” because it outputs content and is intended for use on templates. Template tags normally do not return values, but they could. You could return the $meta value for subsequent use on the template even though the main purpose is to output a dropdown field.

    Make the last line of the function this: return $meta;

    Then call your function on the template, where you want the dropdown to occur, like so:
    <?php $meta = skillbox_callback( get_the_ID()); ?>

    After this point in the template, you can now use $meta as desired without declaring it global. If you needed it before this point, then this scheme will not work.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘text value to php’ is closed to new replies.