Use custom field as tag slug

时间:2013-09-09 作者:vyperlook

我正在尝试用自定义字段值替换“tag slug”,因此它将自动提取与自定义字段“my tag slug”匹配的标记

下面的代码符合我的要求

 <?php if (get_post_meta(get_the_ID(), \'my-tag-slug\', true)) { ?><?php echo get_post_meta(get_the_ID(), \'my-tag-slug\', true); ?><?php } else { ?><?php } ?>
但我怎么能把它放在这里:

<?php

    $args=array(
      \'tag\' => \'tag-slug\',
      \'showposts\'=>5,
      \'caller_get_posts\'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo \'5 recent Posts with tag1\';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
       <?php
      endwhile;
    } //if ($my_query)
  wp_reset_query();  // Restore global post data stomped by the_post().
?>
谢谢你的帮助

1 个回复
SO网友:Anjum

首先从保存的元键my tag slug中获取变量中的标记slug

$tag_slug = (isset(get_post_meta(get_the_ID(), \'my-tag-slug\', true))) ? get_post_meta(get_the_ID(), \'my-tag-slug\', true) : null;
现在按$tag\\u slug查询帖子,将showposts替换为posts\\u per\\u page

$args = array(
  \'tag\' => $tag_slug,
  \'posts_per_page\' => 5,
  \'caller_get_posts\' => 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo \'5 recent Posts with tag1\';
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
   <?php
  endwhile;
} //if ($my_query)
wp_reset_query();

结束

相关推荐