le code pour obtenir le metabox dans l'image est:
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
}}
Actuellement, je suis arrivé à cette page d'édition!
le code de archive-offers.php est: et je veux l’imprimer sur la page d’archive
<?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 -->
** Je veux imprimer la 1ère offre ici! ** ** Je veux imprimer la 2e offre ici! **
<?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(); ?>
Mécanisme : quand je vérifie et que je les publie, ils doivent rester vérifiés ici! Après cela, les messages personnalisés sélectionnés (offre) doivent être imprimés sur archive-offer.php
que dois-je faire pour y parvenir? Aide appréciée!
La fonctionnalité est réalisée de cette façon!
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
La fonction de rappel!
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
}}
Enregistrez la fonction Metabox!
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' );
}
}
Enfin la procédure à écho vérifié!
<?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 ?>
Un grand merci à @sallycj