【问题标题】:WordPress shortcodes for terms术语的 WordPress 简码
【发布时间】:2016-07-24 12:55:15
【问题描述】:

我试图为自定义分类术语动态创建简码。但未能如愿。 假设,如果有一个名为“wordpress”的术语,那么我应该能够通过简码查询与该术语相关的所有帖子。 更准确地说,假设是否有一个称为“事件”的分类法,并且在该分类法下有多个术语。因此,我试图通过每个术语的简码查询每个术语下的帖子。

这是我尝试过的:

function wordpress_recent_post( $atts, $content ) {
  $a = shortcode_atts( array(
    'cat' => '',
  ), $atts );
  $args = array(
    'posts_per_page' => 1,
    'offset' => 0,
    'category_name' => $a['cat'],
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
  );
  $recent_posts = new WP_Query( $args );
  ob_start();
  if ( ! $recent_posts-> have_posts() ) {
    return 'No Posts Found for ' . $a['cat'];
  }
  while ( $recent_posts->have_posts() ) {
    $recent_posts->the_post();
    the_title( '<h2>', '</h2>' );
    if ( '' != $a['cat'] ) {
      $href = '/category/' . $a['cat'];
    } else {
      $href = '/blog';
    }
    echo "<p><a href='$href'>Read More" . ucwords( $a['cat'] ) . '</a></p>';
  }
  wp_reset_query();
  return ob_get_clean();
}
add_shortcode( 'wordpress_recent_post', array($this, 'wordpress_recent_post') );

然后我用它从一个名为“特征”的术语中调用帖子,其 id 为“183”(假设) [wordpress_recent_post cat="183"]

任何帮助都会非常感激。

谢谢!

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    添加术语 slug 做到了。应该有 slug 而不是 id,像这样: [wordpress_recent_post cat="features"]

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2011-06-13
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多