spot_img
HomeWordPress教程wordpress建站公司 如何分配状态/标记以发布?

wordpress建站公司 如何分配状态/标记以发布?

spot_img

JasperAI 10000字免费额度试用

我想标记一些帖子以将它们包含在特色内容滑块中。 为此,我想在新的帖子页面上包含一个复选框,这样如果选中该复选框,帖子就会被标记为精选。 然后我可以在滑块中显示所有标记的帖子。 如何做到这一点? 谢谢。

您可以使用复选框创建自己的元框,并且只选择选中的帖子:

WordPress divi主题

添加元数据代码

/* Define the custom box */
add_action( 'add_meta_boxes', 'my_slider_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'my_slider_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function my_slider_add_custom_box() {
    add_meta_box( 
        'my_slider_sectionid',
        __( 'my slider', 'my_slider_textdomain' ),
        'my_slider_inner_custom_box',
        'post' 
    );
}

/* Prints the box content */
function my_slider_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'my_slider_noncename' );
    $saved = get_post_meta($post->ID,'my_slider_field',true);
    // The actual fields for data entry
    echo '<label for="my_slider_field">';
    _e("Check the box if you want this post to show in the slider", 'myplugin_textdomain' );
    echo '</label> ';
    echo '<input type="checkbox" id="my_slider_field" name="my_slider_field" value="yes"';
    echo  ($saved) ? 'checked': '';
    echo '/>';
}

/* When the post is saved, saves our custom data */
function my_slider_save_postdata( $post_id ) {
      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
          return;

      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times

      if ( !wp_verify_nonce( $_POST['my_slider_noncename'], plugin_basename( __FILE__ ) ) )
          return;

      // OK, we're authenticated: we need to find and save the data
        $old = get_post_meta($post_id,'my_slider_field',true);
      if (isset($_POST['my_slider_field']) && $_POST['my_slider_field'] == "yes"){
            update_post_meta($post_id,'my_slider_field',true);
      }else{
        if (!empty($old)){
            delete_post_meta($post_id,'my_slider_field');
        }
      }
}

查询已检查的帖子

$args = array(
    'posts_per_page' => -1
    'meta_query' => array(
        array(
            'key' => 'my_slider_field',
            'value' => true
        )
    );
$slider_query = new WP_Query($args);

5分钟生成10篇英文软文article forge软件试用
siteground guangda
WordPress花园官方账号
WordPress花园隶致力于为广大跨境电商和独立站爱好者提供优质的WordPress教程、Woocommerce教程、Facebook、Twitter、tiktok、Instagram教程和谷歌SEO教程等资料和对应的建站推广服务。关注‘哟派出海’公众号了解最新资讯。粉丝福利:Shopline免费独立站建设14天优惠 商务合作: [email protected]
RELATED ARTICLES