admin管理员组

文章数量:1287961

If i create php dynamic website i use the code something like this

<input type="text" name="fname">
$name = $_POST['fname'];
echo $name;

In WordPress i have created a plugin. Inside is a metabox with one input text field “name”.

I try to create dynamic content with variables so i can “echo” them anywhere in front pages. More specific in each “directory” post pages it must display dynamically at the page 1 the name “John” and the second page the name “Jane”.

Here is my code:

$args = array(
                'post_type' => array( 'directory' ),
                'order'     => 'ASC',
            );

            // The Query
            $query = new WP_Query( $args );

            // The Loop
          if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
                }
            } else {
                 no posts found
            }

    $name = get_post_meta( get_the_ID(), 'name', true );
        
    if( !empty( $name )  ) {
        echo $name;
    }
    else {
        echo "Empty meta";
    }

Can anyone show me an example on wordpress code about how to create dynamic content with get_post_meta or show me what i am doing wrong ?

Update for question of Antti Koskinen 10-Sep-2021

function prfx_custom_meta() {
    add_meta_box( 'prfx_meta',
                 __( 'Meta Box Title', 'prfx-textdomain' ),
                  'prfx_meta_callback',
                  'directory'
                );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );

/**
 * Outputs the content of the meta box
 */
function prfx_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
    $prfx_stored_meta = get_post_meta( $post->ID );
    ?>
 
    <p>
        <label for="name" class="prfx-row-title"><?php _e( 'Name', 'prfx-textdomain' )?></label>
        <input type="text" name="name" id="name" value="<?php if ( isset ( $prfx_stored_meta['name'] ) ) echo $prfx_stored_meta['name'][0]; ?>" />
    </p>

    
    <?php
}

function prfx_meta_save( $post_id ) {
 
    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
 
    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }
 
    // Checks for input and sanitizes/saves if needed
    if( isset( $_POST[ 'name' ] ) ) {
        update_post_meta( $post_id, 'name', sanitize_text_field( $_POST[ 'name' ] ) );
    }
    
         
}
add_action( 'save_post', 'prfx_meta_save' );

For showing in frontend i use:

$args = array(
                'post_type' => array( 'directory' ),
                'order'     => 'ASC',
            );

            // The Query
            $query = new WP_Query( $args );

            // The Loop
          if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
                }
            } else {
                 no posts found
            }

    $name = get_post_meta( get_the_ID(), 'name', true );

    if( !empty( $name )  ) {
        echo $name;
    }
    else {
        echo "Empty meta";
    }

本文标签: plugin developmentHow to create dynamic content with getpostmeta