如何在没有添加主题_支持的情况下拉取图像(‘post_thhumbnages’)

时间:2017-04-26 作者:Daniel

我正在为我的网站开发一个公文包页面,我尽可能多地使用了这样的功能,当我将鼠标悬停在每个项目的列表名称上时,它会在网站的左侧显示相同的标题,如下所示:

enter image description here

我想做的是,当您将鼠标悬停在右侧的项目标题上时,媒体库中的图像会显示在左侧,这样我就可以开始取得一些进展。不幸的是,这个WordPress构建是不同的,我不是从头开始构建的,我在函数中注意到了这一点。php页面,没有add\\u theme\\u支持(“post\\u缩略图”),而是我在函数中看到的全部内容。php:

<?php
/**
 * Theme functions and definitions.
 *
 * Sets up the theme and provides some helper functions
 *
 * When using a child theme (see http://codex.wordpress.org/Theme_Development
 * and http://codex.wordpress.org/Child_Themes), you can override certain
 * functions (those wrapped in a function_exists() call) by defining them first
 * in your child theme\'s functions.php file. The child theme\'s functions.php
 * file is included before the parent theme\'s file, so the child theme
 * functions would be used.
 *
 *
 * For more information on hooks, actions, and filters,
 * see http://codex.wordpress.org/Plugin_API
 *
 * @package   CMMS
 * @author    Creative MMS
 * 
 * 
 * @since     1.1.0
 */

/*---------------------------------------------------------------------------*/
/*  Load the parent style.css file
/*---------------------------------------------------------------------------*/

add_action(\'wp_enqueue_scripts\', \'enqueue_parent_styles\');
function enqueue_parent_styles() {
    wp_enqueue_style(\'parent-style\', get_template_directory_uri().\'/style.css\');
}

/*------------------------------------------------------------------
[Load Files]
-------------------------------------------------------------------*/
function cmms_child_load_files() {

    $dir = get_stylesheet_directory();

    // Include ACF Theme Functions
    require_once ( $dir.\'/inc/acf/acf_groups.php\' );
    require_once ( $dir.\'/inc/acf/acf.php\' );
}

add_action(\'acf/init\', \'cmms_child_load_files\');

/*------------------------------------------------------------------
[Add Child scripts]
-------------------------------------------------------------------*/
function cmms_child_assets() {

    // Directory to pull assets from
    $dir = get_stylesheet_directory_uri();

    //Use for DEV
    //wp_enqueue_script(\'child-scripts\', $dir.\'/assets/js/child-scripts.js\', array(\'jquery\'), \'1.0\', true);
  wp_enqueue_script( \'cmms-tween-max\', $dir . \'/assets/js/plugins/ScrollMagic/TweenMax.min.js\', array( \'jquery\' ), \'1.0\', true );
  wp_enqueue_script( \'cmms-scroll-magic\', $dir . \'/assets/js/plugins/ScrollMagic/ScrollMagic.min.js\', array( \'jquery\' ), \'1.0\', true );
  wp_enqueue_script( \'cmms-animation\', $dir . \'/assets/js/plugins/ScrollMagic/animation.gsap.min.js\', array( \'jquery\' ), \'1.0\', true );
  wp_enqueue_script( \'cmms-add-indicators\', $dir . \'/assets/js/plugins/ScrollMagic/debug.addIndicators.min.js\', array( \'jquery\' ), \'1.0\', true );

    //Use for PRODUCTION
     wp_enqueue_script( \'cmms-child-app\', $dir . \'/assets/js/cmms-child-app.min.js\', array( \'jquery\' ), \'1.0\', true );

}
add_action(\'wp_enqueue_scripts\', \'cmms_child_assets\');
如何从媒体库中加载一些图像,而不在函数中添加主题支持(“post\\u缩略图”)。php?这是我知道怎么做的唯一方法。我的想法是,然后添加以下代码:

<?php the_post_thumbnail(); ?>
此处:

<li class="item <?php if ($count == 1) : ?>active<?php endif; ?>" id="<?= $count; ?>"><?php the_post_thumbnail(); ?></li>
以上代码来自存档工作。php:

<?php
/**
 * Work template file.
 *
 * This is the most generic template file in a WordPress theme and one of the
 * two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * For example, it puts together the home page when no home.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package   CMMS
 * @author    Creative MMS
* 
 * 
 * @since     1.0.0
 */

get_header(); ?>


    <div class="cmms-house">

        <div class="content-area cmms-clr">

            <main class="site-main cmms-clr">

                <?php
                // Display page header
                //get_template_part( \'partials/archives/header\' ); ?>

                <?php
                // Check if posts exist
                if ( have_posts() ) : ?>

                    <div class="work-entry cmms-clr">   
                        <div class="work-col work-left">
                            <ul>
                            <?php
                                // Get query
                                global $wp_query;

                                // Count
                                $count = 1;

                                // Loop through posts
                                while ( have_posts() ) : the_post(); ?>

                                    <li class="item <?php if ($count == 1) : ?>active<?php endif; ?>" id="<?= $count; ?>"><?php the_post_thumbnail(); ?><?= get_the_title(); ?></li>

                            <?php
                                // End loop
                                $count++; endwhile;
                            ?>
                            <?php wp_reset_postdata(); ?>
                            </ul>
                        </div><!-- .left -->

                        <div class="work-col work-right">
                            <ul>
                            <?php
                                // Get query
                                global $wp_query;

                                //Counter
                                $counter = 1;

                                // Loop through posts
                                while ( have_posts() ) : the_post(); ?>

                                    <li class="list-right" data-trigger="#<?= $counter; ?>"><?= get_the_title(); ?></li>

                            <?php
                                // End loop
                                $counter++; endwhile;
                            ?>
                            <?php wp_reset_postdata(); ?>
                            </ul>
                        </div><!-- .right -->

                    </div><!-- .work-entry -->  

                    <?php
                    // Include pagination template part
                    cmms_include_template( \'partials/global/pagination.php\' ); ?>

                <?php
                // Display no posts found message
                else : ?>

                    <?php get_template_part( \'partials/entry/none\' ); ?>

                <?php endif; ?>

            </main><!-- .main -->

        </div><!-- .content-area -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

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

如果不想修改主题或添加子主题来处理帖子缩略图支持,可以通过插件将回调附加到after_setup_theme 挂钩:

<?php
/*
Plugin Name: WPSE Post Thumbnail Support
Plugin URI: 
Description: Adds post thumbnail support to theme.
Version: 0.0.1
Author:
Author URI:
License: GPL2/Creative Commons
*/


add_action( \'after_setup_theme\', \'wpse_theme_setup\' );
function wpse_theme_setup() {
    /*
     * Enable support for Post Thumbnails on posts and pages.
     */
    add_theme_support( \'post-thumbnails\' );
}
或者,您可以创建自己的附件图像上载功能(我个人使用CMB2 为此),然后通过元ID获取图像并将其传递给wp_get_attachment_image().

相关推荐