自定义快捷码+使用多个高级自定义复选框查询和订购帖子

时间:2017-04-21 作者:Anth

我正在处理函数内部的自定义短代码函数。php将显示“speakers”帖子类型,带有“featured speaker”ACF复选框,并按ACF select元素设置的特定顺序显示。Bt自定义短代码不显示任何内容。我已经检查了很多次代码,但我无法发现我做错了什么。

add_shortcode( \'featured_speakers_clzd\', \'custom_speakers_clzd\' );function custom_speakers_clzd() {

$args = array(
    \'post_type\' => \'speaker\',
    \'posts_per_page\' => -1,
    \'post_status\' => \'publish\',
      \'meta_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'key\'       => \'featured_speaker\',
                    \'value\'     => \'1\',
                    \'compare\'   => \'=\',
                ),
                array(
                    \'key\' => \'speaker_order\',
                    \'orderby\' => \'meta_value\',
                    \'order\' => DESC,
                ),

            ),
    );

$query = new WP_Query( $args );

$out = \'\';

$out .= \'<div class="full_section_inner">\';

if ( $query->have_posts() ) {
    // The Loop
    while ( $query->have_posts() ) {
          $query->the_post();
          $out .= \'<div class="vc_col-sm-3">\';
                $out .= \'<div class="wpb_single_image">\';
                     $out .= \'<a href="\'.get_field(\'speaker_link\').\'" target="_blank">\';
                          $out .= \'<div class="vc_single_image-wrapper vc_box_border_circle  vc_box_border_grey">\';
                                //$out .= \'<img width="150" height="150" src="http://growcommerce.co/wp-content/uploads/2016/06/0c02f46-150x150.jpg" class="vc_single_image-img attachment-thumbnail" alt="Miki Beradelli, CEO, Kidbox" srcset="http://growcommerce.co/wp-content/uploads/2016/06/0c02f46-150x150.jpg 150w, http://growcommerce.co/wp-content/uploads/2016/06/0c02f46.jpg 296w" sizes="(max-width: 150px) 100vw, 150px">\';
                          $out .= get_the_post_thumbnail( get_the_ID(), \'thumbnail\' );
                          $out .= \'</div>\';
                     $out .= \'</a>\';
                $out .= \'</div>\';
                $out .= \'<div class="separator transparent"></div>\';
                $out .= \'<div class="wpb_text_column wpb_content_element ">\';
                     $out .= \'<div class="wpb_wrapper">\';
                          $out .= \'<p style="text-align: center;">\';
                                $out .= \'<a href="https://www.linkedin.com/in/miki-racine-berardelli-b0022b3/" target="_blank">\';
                                     $out .= \'<span style="color: #ffffff;">\'.get_the_title().\'</span>\';
                                $out .= \'</a>\';
                          $out .= \'</p>\';
                          $out .= \'<p style="text-align: center;">\';
                                $out .= \'<a href="\'.get_field(\'speaker_link\').\'" target="_blank">\';
                                     $out .= \'<span style="color: #ffffff;">\'.get_field(\'company\').\'</span>\';
                                $out .= \'</a>\';
                          $out .= \'</p>\';
                     $out .= \'</div>\';
                $out .= \'</div>\';
                $out .= \'<div class="separator transparent"></div>\';
          $out .= \'</div>\';
    }

    wp_reset_postdata();
}

$out .= \'</div>\';

return $out;
}

1 个回复
SO网友:rudtek

try this:

$args = array(
    \'post_type\' => \'speaker\',
    \'posts_per_page\' => -1,
    \'post_status\' => \'publish\',
      \'meta_query\' => array(
                array(
                    \'key\'       => \'featured_speaker\',
                    \'value\'     => \'1\',
                    \'compare\'   => \'=\',
                ),
        ),
     \'orderby\' => \'meta_value\',
     \'meta_key\' => \'speaker_order\',
     \'order\' => DESC,   
    );

相关推荐

Namespaced shortcode?

我正在改造一个旧的WP站点,该站点有许多自定义的短代码,显然由于代码当前的组织方式,这些短代码在性能方面付出了代价。当然,我可以修复优化不好的代码,使用十几个短代码,并且一天就可以完成,但我想知道如何更好地组织它们。根据WordPress\'documentation, 建议将它们放在插件中并在上初始化init. 我们可以通过这样“命名”它们来减少这个钩子中的负载吗?[com.company shortcode attr=\"attr\" prop=\"prop\"] 有人尝试过这样的解决方案吗