【问题标题】:Common php function called by other functions其他函数调用的常用php函数
【发布时间】:2017-01-15 10:00:50
【问题描述】:

您好,我想创建两个具有不同参数但具有相同公共功能的函数。这是我的例子...

常用功能:

function my_responsive_pictures($post_id){
// Get alt text or set the $alt_text variable to the post title if no alt text exists
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ( !$alt_text ) { $alt_text = esc_html( get_the_title($post_id) ); }

// Get the info for each image size including the original (full)
$thumb_original = wp_get_attachment_image_src($attachment_id, 'slideshow');
$thumb_large    = wp_get_attachment_image_src($attachment_id, 'slideshow-lg');
$thumb_medium   = wp_get_attachment_image_src($attachment_id, 'slideshow-md');
$thumb_small    = wp_get_attachment_image_src($attachment_id, 'slideshow-xs');

// Create array containing each image size + the alt tag
$thumb_data = array(
'thumb_original' => $thumb_original[0],
'thumb_large'    => $thumb_large[0],
'thumb_medium'   => $thumb_medium[0],
'thumb_small'    => $thumb_small[0],
'thumb_alt'      => $alt_text
);

// Echo out <picture> element based on code from above
echo '<picture>';
echo '<!--[if IE 9]><video style="display: none;"><![endif]-->'; // Fallback to <video> element for IE9
echo '<source srcset="' . $thumb_data['thumb_large'] . ', ' . $thumb_data['thumb_original'] . ' x2" media="(min-width: 800px)">';
echo '<source srcset="' . $thumb_data['thumb_medium'] . ', ' . $thumb_data['thumb_large'] . ' x2" media="(min-width: 400px)">'; 
echo '<source srcset="' . $thumb_data['thumb_small'] . ', ' . $thumb_data['thumb_medium'] . ' x2">'; 
echo '<!--[if IE 9]></video><![endif]-->'; // Fallback to <video> element for IE9
echo '<img srcset="' . $thumb_data['thumb_small'] . ', ' . $thumb_data['thumb_medium'] . ' x2" alt="' . $thumb_data['thumb_alt'] . '">';
echo '</picture>';
}

另一个调用通用函数的:

function my_responsive_thumbnail($post_id){
// Get the featured image ID
$attachment_id = get_post_thumbnail_id($post_id);
my_responsive_pictures();
}

第二个带有其他参数 $attachment_ID :

function my_responsive_acfthumbnail($post_id){
// Get the featured image ID
$attachment_id = get_field('image_bandeau');
my_responsive_pictures();
}

什么也没发生 :(。我做错了什么?感谢您的帮助...

【问题讨论】:

  • 函数在同一个文件里?你也错过了必需的参数。

标签: php wordpress function call


【解决方案1】:

你的函数需要一个参数,当你在这里调用它时

my_responsive_pictures();

你什么都没通过。

您还必须先调用 my_responsive_thumbnail() 函数,然后它才会对您的“通用”函数进行后续调用。

【讨论】:

    【解决方案2】:

    此代码存在一些问题。我们首先要看的是main函数。

    function my_responsive_pictures($post_id){
    

    您的函数定义没有为$post_id 提供默认值,因此在您调用该函数时需要它。尝试在不传递参数的情况下调用函数会触发错误。

    $alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    if ( !$alt_text ) { $alt_text = esc_html( get_the_title($post_id) ); }
    

    这里您指的是尚未设置的$attachment_id。如果没有找到,您将获得帖子的标题。

    为此,您需要为函数设置两个参数。

    function my_responsive_pictures( $attachment_id, $post_id ) {
    

    任何时候我们调用这个函数,我们都需要传入$attachment_id(图片的ID)和$post_id(帖子的ID)。

    接下来我们需要修改最终调用主函数的函数。

    function my_responsive_thumbnail( $post_id ) {
        // Get the featured image ID
        $attachment_id = get_post_thumbnail_id( $post_id );
    
        // Now that we have the featured image ID, we really ought
        // to do some error checking. Let's assume that all went well.
        my_responsive_pictures( $attachment_id, $post_id );
    }
    

    下一个功能需要更多关注。请记住,您正在使用帖子 ID 调用这些函数。您需要让get_field() 知道它应该为其检索图像的帖子的 ID。

    function my_responsive_acfthumbnail( $post_id ) {
        // Get the featured image ID
        $attachment_id = get_field( 'image_bandeau', $post_id );
        my_responsive_pictures( $attachment_id, $post_id );
    }
    

    示例用法:

    my_responsive_acfthumbnail( get_the_ID() );
    

    您可能还需要考虑为帖子 ID 设置默认值,这样在检索您正在查看的当前帖子的图像时就不需要传递它。

    最后,考虑调用my_responsive_pictures 的函数之间的重复级别。您需要检查附件 ID 是否有效,因此函数可能会变得更大,只有 1 行不同。

    关于get_field()的更多信息:https://www.advancedcustomfields.com/resources/get_field/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2018-06-09
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多