从一个分类的唯一术语中显示上一篇文章的自定义字段

时间:2013-06-30 作者:Kian Abrishami

我的第一个主题设计有问题,我想你可以很容易地帮助我!(我在Google和WordPress codex中搜索了好几次我的问题,但我找不到我想要的确切答案!!)

我想显示我的“yesterday\'上一篇文章的自定义字段,来自表单元格中“一个分类法的唯一术语”!

在另一个单元格中,我想显示“上一篇文章的自定义字段,来自一个分类法的唯一术语”

我使用此代码:

<?php
/**
* @package WordPress
* @subpackage Default_Theme
*/
    $yesterday = strtotime("-1 day");
    $yesterday = date("Y-m-d", $yesterday);
    $today = date("Y-m-d");
$querystr = "SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wposts.post_date >= \'$yesterday\'
    AND wposts.post_date <= \'$today\'
    AND wposts.post_type = \'MY-CUSTOM-POST-NAME\'
    ORDER BY wposts.post_date ASC;";
$posts = $wpdb->get_results($querystr, OBJECT);
get_header();
?>
<div  id="content" role="main">
<?php 

if($posts) {
    foreach($posts AS $post) setup_postdata($post);
    {
        ?>
<div id="post-<?php  the_ID(); ?>">
<div> <?php  echo get_post_meta($post->ID,\'MY-CUSTOM-FIELD\',TRUE) ?></div>
</div>
<?php  } ?>
<?php  } else { ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn\'t here.</p>
<?php  get_search_form(); ?>
<?php  } ?>
</div>
<?php  get_sidebar(); ?>
<?php  get_footer(); ?>
但此代码显示我的“上一个自定义文件”ALL terms 一种分类法!“”

请帮我解决这个问题。

2 个回复
最合适的回答,由SO网友:rfrq 整理而成

我希望我的理解都是正确的:

显示自定义post类型中的特定自定义字段,post分配给自定义分类法中的特定术语,用于今天和昨天的最新条目,我无法解决此任务,但您可以使用标准WordPress工具尝试以下功能:

function show_latest48h_post_cf_in_term( $cpt, $tax, $term_slug, $cf ) {        
                    /* vars */
        /******************************************************/
        $output = \'\';
        $today_latest = \'\';
        $yesterday_latest = \'\';
        $timestamp = current_time( \'timestamp\' );
        $today = strftime(\'%Y-%m-%d\', $timestamp);
        $yesterday = strftime(\'%Y-%m-%d\', strtotime(\'-1 day\', $timestamp));
        $today_ar = date_parse($today);
        $yesterday_ar = date_parse($yesterday);

        /* today */
        /******************************************************/                 
        // args
        $args_today = array(
            \'post_type\' => $cpt,
            \'tax_query\' => array(
                array(
                        \'taxonomy\' => $tax,
                        \'field\' => \'slug\',
                        \'terms\' => $term_slug
                )
            ),
            \'year\' => $today_ar["year"],
            \'monthnum\' => $today_ar["month"],
            \'day\' => $today_ar["day"],
            \'meta_key\' => $cf,
            \'posts_per_page\'  => 1
        );
        // get today post
        $today_query = get_posts($args_today);        
        if ($today_query) {                
                $today_latest .= \'<p>\' . $today . \': \' . get_post_meta( $today_query[0]->ID, $cf, true) . \'</p>\';                    
        }

        /* yesterday */
        /******************************************************/                 
        // args
        $args_yesterday = array(
            \'post_type\' => $cpt,
            \'tax_query\' => array(
                array(
                        \'taxonomy\' => $tax,
                        \'field\' => \'slug\',
                        \'terms\' => $term_slug
                )
            ),
            \'year\' => $yesterday_ar["year"],
            \'monthnum\' => $yesterday_ar["month"],
            \'day\' => $yesterday_ar["day"],
            \'meta_key\' => $cf,
            \'posts_per_page\'  => 1
        );
        // get yesterday post
        $yesterday_query = get_posts($args_yesterday);        
        if ($yesterday_query) {
            $yesterday_latest .= \'<p>\' . $yesterday . \': \' . get_post_meta( $yesterday_query[0]->ID, $cf, true) . \'</p>\';                    
        }

        /* output */
        /******************************************************/            
        if ($today_query || $yesterday_query) {
            $output = $today_latest . $yesterday_latest;
        } else {
            $output = \'Nothing found\';
        }
        echo $output;
}

SO网友:s_ha_dum

首先,使用WP_Query 而不是您自己的SQL。

我想这就是你想要的:

$yesterday = strtotime("-1 day");
$yesterday = date("Y-m-d", $yesterday);
$yesterday = explode(\'-\',$yesterday);
$args = array(
  \'post_type\' => \'MY-CUSTOM-POST-NAME\',
  \'year\' => $yesterday[0],
  \'monthnum\' => $yesterday[1],
  \'day\' => $yesterday[2],
  \'tax_query\' => array(
    array(
      \'taxonomy\' => \'people\',
      \'field\' => \'slug\',
      \'terms\' => \'bob\'
    )
  )
);
$query = new WP_Query($args);
一些解释:如果你想要的是“昨天”的帖子,你真的不需要检查从今天到昨天的帖子,你只需要问昨天的帖子就可以了。

当你说你想要“来自一个分类法的独特术语”时,我真的不知道你是什么意思,但我认为你的意思是你想要来自某个特定术语或某个特定分类法的术语的帖子,因此tax_query.

结束

相关推荐

Taxonomy as checkboxes

因此,我试图让Wordpress将类别显示为自定义元框中的一系列选择框,并使用以下示例tutorial, 但当我去保存选择时,它会以预期选择ID的形式生成其他类别。它也无法保存多个选择。 <?php // Add the Meta Box function add_custom_meta_box() { add_meta_box( \'custom_meta_box\', // $id&