基于_缩略图_id的DO_SHORT代码

时间:2017-03-22 作者:W. White

Situation
我创建了一个名为“横幅”的自定义帖子类型。在每一篇帖子上,我都能添加一幅特色图片。这将保存在我的数据库中,并将获得_thumbnail_id. 此外,在每个帖子编辑页面上,还有一个用于banner\\u url和target属性的自定义元框。在那里,我给我的横幅的链接,它应该去后,点击它的前端。目标属性将以正确的方式打开图像。这将正确发送到我的wpdb。

插件代码:

<?php
// function to show a banner using query of banner post type
function show_banner() {

// start by setting up the query
$get_banner = new WP_Query( array(
  \'post_type\' => \'banners\',
  \'meta_query\' => array(
    \'relation\' => \'AND\',
    array(
      \'key\' => \'banner_link\',
      \'value\' => \'https://www.mypage.com\'
    )
  )
));


// now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) : while ( $get_banner->have_posts() ) : $get_banner->the_post();

$output = \'<div class="container" align="center"><a href="\'.get_post_meta( get_the_ID(), \'banner_link\', true ).\'"
target="\'.get_post_meta( get_the_ID(), \'target\', true).\'">\'.get_the_post_thumbnail().\'</a></div>\';

 endwhile;
endif;
wp_reset_postdata();
return $output;
}

add_shortcode( \'banner\', \'show_banner\' );
//END
?>
第页。php

<?php  echo do_shortcode("[banner]");?>
Question
我使用meta\\u查询来选择url。它工作正常,显示正确的图像。链接和目标也可以正常工作。但我想要的是,我可以向do\\u快捷码回显添加一个缩略图id,然后在前端显示正确的图像,并在其周围显示正确的url和目标。

我希望有人能在正确的方向上帮助我。提前谢谢。

EDIT SOLUTION<特别感谢bynicolas帮我找到了正确的方向!

插件代码:

<?php
// function to show a banner using query of banner post type
function show_banner( $att ) {

  // Set default $att values if none is provided
  // $att is an array so access the shortcode value via     $att[\'thumbnail_id\']
  // in the [banner thumbnail_id="12"] shortcode

$atts = shortcode_atts(
    array(
        \'_thumbnail_id\' => \'\',
    ), $atts, \'banner\' );

//print_r(\'Thumbnail_id: \' . $att[\'_thumbnail_id\'] . \'<br>\');

//start by setting up the query
$get_banner = new WP_Query( array(
  \'post_type\' => \'banners\',
)
);

//now check if the query has posts and if so, output their content in a banner-box div
if ( $get_banner->have_posts() ) :
  while ( $get_banner->have_posts() ) : $get_banner->the_post();

    $thumbnail_id = get_post_meta(get_the_ID(), \'_thumbnail_id\', true);
    $image_url = get_post_meta(get_the_ID(), \'banner_link\', true);
    $target = get_post_meta(get_the_ID(), \'target\', true);

    if ($thumbnail_id === $att[\'_thumbnail_id\']) {
      // print_r($thumbnail_id . \' \');
      // print_r($image_url . \' \');
      // print_r($target . \'<br>\');
      $output = \'<div class="container" align="center"><a href="\'.get_post_meta( get_the_ID(), \'banner_link\', true ).\'"
      target="\'.get_post_meta( get_the_ID(), \'target\', true).\'">\'.get_the_post_thumbnail().
      \'</a></div>\';
    }

  endwhile;
endif;
wp_reset_postdata();
return $output;

}
add_shortcode( \'banner\', \'show_banner\' );
?>
第页。php

<?php echo do_shortcode(\'[banner _thumbnail_id="584"]\');?>
//584 is the thumbnail_id, so change that to your own thumbnail_id.

1 个回复
SO网友:bynicolas
// function to show a banner using query of banner post type
function show_banner( $att ) {

  // Set default $att values if none is provided
  // $att is an array so access the shortcode value via $att[\'thumbnail_id\']
  // in the [banner thumbnail_id="12"] shortcode
  $atts = shortcode_atts( array(
    \'thumbnail_id\' => \'\',
  ), $atts, \'banner\' );

  // start by setting up the query
  $get_banner = new WP_Query( array(
    \'post_type\' => \'banners\',
    \'meta_query\' => array(
      \'relation\' => \'AND\',
      array(
        \'key\' => \'banner_link\',
        \'value\' => \'https://www.mypage.com\'
      )
    )
  ));


  $img_markup = ! empty( $att[ \'thumbnail_id\' ] ) ? sprintf( \'<img src="%s" />\', wp_get_attachment_image_src( $att[ \'thumbnail_id\' ] ) ) : \'\';


  // now check if the query has posts and if so, output their content in a banner-box div
  if ( $get_banner->have_posts() ) : 
    while ( $get_banner->have_posts() ) : $get_banner->the_post();

      $output = \'<div class="container" align="center"><a href="\'.get_post_meta( get_the_ID(), \'banner_link\', true ).\'"
      target="\'.get_post_meta( get_the_ID(), \'target\', true).\'">\'.$img_markup.\'</a></div>\';

    endwhile;
  endif;
  wp_reset_postdata();
  return $output;
}
add_shortcode( \'banner\', \'show_banner\' );

相关推荐

Do not parse shortcode in CPT

我有一个CPT,我不想在它的内容中解析shortcode(使用\\u content()函数)。我可以使用remove\\u filter删除短代码的默认过滤器。但我如何确定我只是为了我想要的CPT而删除过滤器?我有一个在页面中使用的快捷码[我的自定义快捷码]。此短代码使用WP\\U查询和输出CPT帖子。我不想在这篇CPT文章中分析短代码。我是否应该在短代码解析挂钩之前用虚拟内容更改短代码,并在之后替换回来?或者我应该在我的CPT输出之前删除短代码的默认过滤器,然后在我的CPT输出完成后再次添加短代码的默