duan246558 2017-03-22 01:15
浏览 43

如何将类别参数添加到短代码?

I am trying to create a shortcode that displays my post type: advanced_topic but want to add the category parameter such as

[advanced-topics category="parole"]

to display only topics under that category.

This is the code I have so far but I feel like I'm missing something as it's not working... Any help is appreciated. Thank you.

function display_advanced_topics(){

    extract( shortcode_atts( array( 'category_name' => 'criminal-records' ), $args ) ); // $categoryName variable will be initialized to 'criminal-records' if the shortcode is missing the 'category' parameter

    $string = '';
    $args = array(
    'post_type' => 'advanced_topic',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'category_name' => $categoryName
    );

$query = new WP_Query( $args );
if( $query->have_posts() ){
    $string .= '<div class="advanced-topics">';
    while( $query->have_posts() ){
        $query->the_post();

        $string .= 
        '<div class="row advanced-topic">'
                    .'<div class="content">
                            <div class="title"><?php get_the_title(); ?></div>
                        </div>'
                .'</div>';
    }
            $string .= '</div>';

}
wp_reset_postdata();
return $string;
}
add_shortcode( 'advanced-topics', 'display_advanced_topics' );
  • 写回答

1条回答 默认 最新

  • dongzhi5386 2017-03-22 01:27
    关注
    1. Always is good to use

          error_reporting(E_ALL);
          ini_set('display_error', 1);
      
    2. wrong quotes

          ob_start();
          get_the_title()
          $the_title = ob_get_clean();
          $string .= 
          '<div class="row advanced-topic">'
                      .'<div class="content">
                              <div class="title">'. $the_title .'</div>
                          </div>'
                  .'</div>';
      

    if get_the_title() prints results you must use ob_get_clean

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂