Wordpress主题设置幻灯片功能设计?
开发一个主题,首页有一个幻灯片展示,#1、#2、#3 三张。
发布文章的时候,需要有一个勾选选项,勾选1、2或者3,会在首页的相应幻灯片显示;勾选4(默认)则不会显示。
尝试过wordpress的自定义字段(Custom Fields)但是好像不能实现,求提供思路和解决办法。
回复内容:
开发一个主题,首页有一个幻灯片展示,#1、#2、#3 三张。
发布文章的时候,需要有一个勾选选项,勾选1、2或者3,会在首页的相应幻灯片显示;勾选4(默认)则不会显示。
尝试过wordpress的自定义字段(Custom Fields)但是好像不能实现,求提供思路和解决办法。
<code> <?php /* Fire our meta box setup function on the post editor screen. */ add_action( 'load-post.php', 'sola_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'sola_post_meta_boxes_setup' ); /* 这是需要修改的两处之一,本功能只需要一个checkbox,将checkbox的title、id等属性填充到 $fields数组中, 后面的代码会自动根据数组填充的内容创建Post Meta Box */ $fields = array( array( 'name' => __('Use as slide'), 'desc' => 'Check this box and make the post a slider', 'id' => 'sola-post-slider', 'type' => 'checkbox', 'default' => '' ) ); /* Meta box setup function. */ function sola_post_meta_boxes_setup() { /* Add meta boxes on the 'add_meta_boxes' hook. */ add_action( 'add_meta_boxes', 'sola_add_post_meta_boxes' ); add_action( 'save_post', 'sola_save_post_meta_boxes', 10, 2 ); } /* Create one or more meta boxes to be displayed on the post editor screen. */ /* 这里也需要改一下,设置需要创建的Post Meta Box叫什么名字,显示在什么位置 */ function sola_add_post_meta_boxes() { add_meta_box( 'sola-post-slider-class', // Unique ID __('Slideshow'), // Title 'sola_seo_box_format', // Callback function 'post', // Admin page (or post type) 'side', // Context 'default' // Priority ); } function sola_seo_box_format(){ global $fields,$post; // Use nonce for verification echo '<input type="hidden" name="sola_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '">'; echo '<table class="form-table">'; foreach ($fields as $field) { // get current post meta data $meta = get_post_meta($post->ID, $field['id'], true); echo '<tr>'. '<th><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'. '<td>'; switch ($field['type']) { case 'text': echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%">'. ' '. $field['desc']; break; case 'textarea': echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . ''. ' '. $field['desc']; break; case 'select': echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">'; foreach ($field['options'] as $option) { echo '<option selected : .>'. $option . '</option>'; } echo '</select>'; break; case 'radio': foreach ($field['options'] as $option) { echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '" . checked :>' . $option['name']; } break; case 'checkbox': echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '" . checked :>'; break; } echo '<td>'.'</td></textarea> </td> </tr>'; } echo '</table>'; } function sola_save_post_meta_boxes($post_id) { global $fields, $post; //Verify nonce if (!wp_verify_nonce($_POST['sola_meta_box_nonce'], basename(__FILE__))) { return $post_id; } //Check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } //Get the post type object. $post_type = get_post_type_object( $post->post_type ); //Check permissions if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; foreach ($fields as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } } ?> </code>
这段代码会在文章创建和编辑页面创建如下所示的Post Meta Box,如下图
读取幻灯片文章
接下来修改slider.php,过去只需要查询custom post type,现在使用post meta box实现,就需要根据post的meta信息搜索幻灯片,代码如下
<code>$args = array( 'meta_query' => array( array( 'key' => 'sola-post-slider', 'value' => 'on', ) ) ); $slides = get_posts($args); </code>
用get_posts()和meta_query参数结合,就可以达到目的,有了数据,直接循环输出就行了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to
