2017-03-22 31 views
0

我想创建一个显示我的职位类型简码:advanced_topic但要添加的类别参数如如何将类别参数添加到短代码?

[高级课题类别=“假释”]

显示下唯一主题该类别。

这是我到目前为止的代码,但我觉得我失去了一些东西,因为它不工作......任何帮助表示赞赏。谢谢。

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'); 
+0

您应该阅读Codex关于如何使用属性制作简码的方法。你的失踪。 https://codex.wordpress.org/Function_Reference/shortcode_atts – Christina

+0

我做到了,那是我如何开始这个简码,但我似乎无法找到我失踪的东西? –

+0

我想通了!谢谢。 –

回答

0
  1. 始终是很好用

    error_reporting(E_ALL); 
        ini_set('display_error', 1); 
    
  2. 错误报价

    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>'; 
    

如果get_the_title()打印结果,你必须使用ob_get_clean

+0

嗨,所以它是打印结果,但它只打印所有与初始默认类别“犯罪记录”的结果。我尝试了[高级主题类别=“假释”]来显示“假释”类别,但它似乎没有采取参数。 –

+0

我改进了代码。您无法将打印结果分配给变量。 (简化)通过'ob_start()' - 'ob_get_clean()'可以将变量分配给每个打印。 – bato3