繁体   English   中英

Wordpress函数获取先前由管理员在“媒体设置”中设置的post_thumbnail的尺寸

[英]Wordpress function to get dimensions of post_thumbnail previously set by admin in Media Settings

我在functions.php中使用以下代码,以便加载jQuery并设置需要在我的jQuery脚本中使用的变量(帖子缩略图的宽度和高度)。

<?php
if( !is_admin()){
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"), false, '');
   wp_enqueue_script('jquery');
   wp_localize_script( 'jquery', 'MyThumbSize', array( 'width' => '240px','height' => '160px' ) );
}?>

这些变量是硬编码的(240px和160px),但是我想知道是否有可能从Wordpress函数中自动获取这些值以及语法是什么(因为我不是一个好的编码器)。

我知道这些设置是由管理员在“媒体设置”下的信息中心设置的。

您可以使用以下任何一种介质尺寸:

   <?php 
    $thumbnail_width = get_option( 'thumbnail_size_w' );
        $thumbnail_height = get_option( 'thumbnail_size_h' );
        $medium_width = get_option( 'medium_size_w' );
        $medium_height = get_option( 'medium_size_h' );
        $large_height = get_option( 'large_size_w' );
        $large_width = get_option( 'large_size_w' );
   ?>

如果在函数中使用php标签,请摆脱它。

因此,您的代码应如下所示:

<?php
$thumbnail_width = get_option( 'thumbnail_size_w' );
$thumbnail_height = get_option( 'thumbnail_size_h' );

if( !is_admin()){
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"), false, '');
   wp_enqueue_script('jquery');
   wp_localize_script( 'jquery', 'MyThumbSize', array( 'width' => $thumbnail_width,'height' => $thumbnail_height ) );
}?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM