打印在帖子的Metabox内选中的定制帖子(优惠)是一个好策略吗?

时间:2018-03-22 作者:Nimesh

the code for getting the metabox in image is:

function op_register_menu_meta_box() {
add_meta_box(
\'op-menu-meta-box-id\',
esc_html__( \'Custom offers Checklist (select any 2)\', \'text-domain\' ),
\'op_render_menu_meta_box\',
\'listing\'
);
}
add_action( \'add_meta_boxes_listing\', \'op_register_menu_meta_box\' );
function op_render_menu_meta_box() {
// Metabox content
$getPostsToSelect = get_posts(\'post_type=offers&numberposts=-1\');
foreach ($getPostsToSelect as $aPostsToSelect) {
?>
<label>
<input 
  type=\'checkbox\' 
  name=\'yourcustom_meta[]\' 
  class=\'postsToSelect\'
  value=\'<?php echo $aPostsToSelect->ID ?>\'
 /> 
<?php echo $aPostsToSelect->post_title;
?>
</label><br />
<?php
}}
目前我已经到了这个编辑贴子页面!

enter image description here

the code of archive-offers.php is: 我想把它打印在存档页上

<?php
/**
* The main template file***/
get_header(); ?>
<?php
        /**
        * The main template file
        * It puts together the home page if no home.php file exists.
        *
        * @package Simple Blog Theme
        */
        get_header(); ?>
            <div class="row mb-5">
                <!-- Blog Entries Column -->
                <div class="col"></div>
                <div class="col-sm-9">
                    <article itemscope itemtype="http://schema.org/Article">
                        <span itemscope itemprop="mainEntityOfPage" itemType="https://schema.org/WebPage" itemid="https://google.com/article" /></span>
                        <div class="my-5">
                        </div>
                        <!-- Blog Post -->
**我想在此处打印第一份报价!****我想在这里打印第二份报价!**
                        <?php
                    // Note: this loop is a simplified version of a loop published in a post at Elegant Themes
                    // Source: https://www.elegantthemes.com/blog/tips-tricks/converting-html-sites-to-wordpress-sites
                    if ( have_posts() ) : while ( have_posts() ) : the_post();  ?>
                            <div>
                                <h1 class="card-title mb-4" itemprop="headline">
                                    <span itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
        <meta itemprop="name" content="Rookie Bloggers">
        <meta itemprop="url" content="http://sample1.nepromedia.com/wp-content/uploads/2018/02/logo.jpg">
        <span itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
        <meta itemprop="url" content="http://sample1.nepromedia.com/wp-content/uploads/2018/02/logo.jpg"></span>


                                    <?php the_title(); ?>
                                </h1>
                                <div class=" ml-1 mr-1 row alert alert-info text-info mb-5">
                                    <span class="col-sm"><i class="fas fa-edit "></i> Last Modified On</span> <span class="col-sm" itemprop="datePublished">
                                <meta itemprop="dateModified" content="<?php the_modified_date(); ?>"><strong><i class="fas fa-calendar-alt "></i>
                        <?php the_time( \'F j, Y\' ); ?></strong></span>
                                    <span class="col-sm" itemprop="author" itemscope itemtype="http://schema.org/Person">
                            <meta itemprop="name" content="<?php the_author(); ?>">
        <meta itemprop="publisher"><?php $args = array(\'class\' => \'nimesh12\' , ); ?> <?php echo get_avatar( get_the_author_meta( \'ID\' ), 25, $default, $alt, $args); ?>
        <?php the_author_posts_link(); ?></span>
                                </div>

                                <span itemprop=\'image\' itemscope itemtype=\'http://schema.org/ImageObject\'><meta itemprop="url"content="<?php echo get_the_post_thumbnail_url($post_id); ?>"><?php the_post_thumbnail( \'single-post\', array(\'class\' => \'img-fluid\') ); ?></span>
                                <div>
                                    <p class="card-text mb-5" itemprop="description">
                                        <?php the_content(); ?>
                                    </p>
                                    <?php
                        $prev_post = get_previous_post();
                        if (!empty( $prev_post )): ?>
                                        <strong><a href="<?php echo $prev_post->guid ?>"><?php echo \'Older posts\' ?></a></strong>
                                        <?php endif ?>
                                </div>

                            </div>
                            <?php endwhile; /* rewind or continue if all posts have been fetched */ ?>

                            <?php else : ?>
                            <?php endif; ?>
                    </article>
                </div>
                <div class="col"></div>
            </div>
            <!-- /.row -->
            </div>
            <!-- /.container -->
            <?php get_footer(); ?>
Mechanism:当我检查其中两个并发布它们时,它们应该在这里保持检查状态!之后,应在archive-offer.php

我应该怎么做才能做到这一点?感谢您的帮助!

1 个回复
最合适的回答,由SO网友:Nimesh 整理而成

该功能是通过这种方式实现的!

function op_register_menu_meta_box() {
add_meta_box(
    \'op-menu-meta-box-id\', // Metabox ID
     esc_html__( \'Custom offers Checklist (select any 2)\', \'text-domain\' ), //Metabox Title
    \'op_render_menu_meta_box\', // Metabox Callback Function
    \'listing\' // Place Where this Metbax Should Appear
    );
}
add_action( \'add_meta_boxes_listing\', \'op_register_menu_meta_box\' ); //Metabox Hook

The callback function!

function op_render_menu_meta_box( $post ) { // Metabox Callback Function Called
// Get the selected \'offers\' (array of post IDs).
$offers_ids = (array) get_post_meta( $post->ID, \'_offers_ids\', true );

// Displays a nonce field, without referrer field.
wp_nonce_field( \'save-offers-meta\', \'offers_meta_nonce\', false );

// Metabox content
$getPostsToSelect = get_posts(
                    array(
                        \'post_type\' => \'offers\',
                        \'posts_per_page\' => 3,
                    ));
foreach ($getPostsToSelect as $aPostsToSelect) {
?>
<label>
    <input 
      type=\'checkbox\' 
      name=\'offers_meta[]\' 
      class=\'postsToSelect\'
      value=\'<?php echo $aPostsToSelect->ID ?>\'
      <?php checked( true, in_array( $aPostsToSelect->ID, $offers_ids ) ); ?>
     /> 
    <?php echo $aPostsToSelect->post_title;
?>
</label><br/>
<?php
}}

Save Metabox function!

add_action( \'save_post\', \'op_save_offers_meta\', 10, 2 );
function op_save_offers_meta( $post_id, $post ) {
    // Make sure that we can save the meta data.
    if ( ( empty( $post_id ) || empty( $post ) || ( \'listing\' !== get_post_type( $post ) ) )
    || ( empty( $_POST[\'post_ID\'] ) || $_POST[\'post_ID\'] != $post_id || ! isset( $_POST[\'offers_meta\'] ) )
    || ( empty( $_POST[\'offers_meta_nonce\'] ) || ! wp_verify_nonce( $_POST[\'offers_meta_nonce\'], \'save-offers-meta\' ) )
    || ( ! current_user_can( \'edit_post\', $post_id ) ) ) {
        return;
    }

    $offers_ids = wp_parse_id_list( $_POST[\'offers_meta\'] );
    if ( ! empty( $offers_ids ) ) {
        update_post_meta( $post_id, \'_offers_ids\', $offers_ids );
    } else {
        delete_post_meta( $post_id, \'_offers_ids\' );
    }
}

At last the procedure to echo checked !

<?php $post_id = get_the_ID();

// Get the selected \'offers\' (array of post IDs).
$offers_ids = (array) get_post_meta( $post_id, \'_offers_ids\', true );

$q = new WP_Query( [
    \'post_type\' => \'offers\',
    \'post__in\'  => $offers_ids,
] );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
    ?>
        <h3><?php the_title(); ?></h3>
        <p><?php the_content(); ?></p>
    <?php
    }
}

wp_reset_query ?>

Big thanks to @sallycj

结束

相关推荐

为所有用户显示链接到POST设置的Metabox

目前,我有一个元框,我的网站的所有编辑都可以看到。我希望所有投稿人都能看到这个元框。我使用了一个用户角色插件,看起来元框链接到了帖子设置。当我允许任何用户发布帖子时,他们都可以看到这个元框。有没有办法让所有用户都能看到这个元框,但仍然可以让贡献者不能发布帖子。我正在使用Tagdiv报纸主题,并试图让post settings元框显示出来。我相信元框id是:td\\u post\\u theme\\u settings\\u metabox谢谢