【问题标题】:Wordpress: Include custom_fields (Book author) in search resultsWordpress:在搜索结果中包含 custom_fields(书籍作者)
【发布时间】:2012-11-20 05:10:26
【问题描述】:

我使用 TwentyEleven 作为我的基本主题,我已经皮肤了。 在我的 Content.php 文件中,搜索结果看起来像这样

<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    <div class="entry-summary">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
        <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
        <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

我正在尝试将“作者”元键/值对与帖子标题和帖子内容一起返回到我的搜索结果中,如下所示:

<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    <div class="entry-summary">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
        <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
        <?php get_post_meta(get_the_ID(), _wpsc_author, true); ?>
        <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

注意这一行:

<?php get_post_meta(get_the_ID(), _wpsc_author, true); ?>

然而,这什么也没返回?

我已经阅读了大量关于 the_meta()、get_post_custom() 等主题的内容,但我尝试的任何方法似乎都没有奏效。

希望了解一些见解吗?

编辑: 抱歉,我对“作者”部分不太清楚。 作者是 post_meta 表中的自定义字段,以及“书籍类型”、书籍条件“出版年份”等。它不是文章作者。

谢谢

【问题讨论】:

  • 也可以试试 print_r(get_post_custom());我们想看看 _wpsc_author 是否真的在您的元数据中。另外..这段代码看起来像是在循环中,我们在循环中对吗?
  • 您似乎只是在非搜索页面上拉取作者元数据。这是否正是您想要的有点令人困惑,因为您说您想在搜索结果中显示该信息。考虑将&lt;?php echo (get_post_meta(get_the_ID(), _wpsc_author, true)); ?&gt; 部分移到&lt;?php the_excerpt(); ?&gt; 之后

标签: wordpress


【解决方案1】:

做一件事,只是在函数之前添加 echo 它将显示结果,如果函数的最后一个参数设置为 false,则为您提供一个数组,如果设置为 true,则为您提供一个字符串,因此请根据您的要求进行更改

【讨论】:

  • 你的意思是这样 Abhimanue?如果我这样做,它仍然没有返回任何内容。
  • &lt;?php $post_id = $post-&gt;ID; $key = $prefix.'City_Railway_Station'; $meta_values = get_post_meta($post_id, $key, true); echo $meta_values; ?&gt; 我就是这样做的。
【解决方案2】:

你试过the_author_meta();吗?

你可以像这样使用它:the_author_meta('user_email')

请注意,此函数仅在循环内有效......但有一些方法可以绕过它,但它们很糟糕。

可用键:

user_login
user_pass
user_nicename
user_email
user_url
user_registered
user_activation_key
user_status
display_name
nickname
first_name
last_name
description
jabber
aim
yim
user_level
user_firstname
user_lastname
user_description
rich_editing
comment_shortcuts
admin_color
plugins_per_page
plugins_last_view
ID

【讨论】:

  • 嗨,肖恩,我添加了一个编辑并稍微清理了我的问题。我在最初的帖子中对“作者”不太清楚。不过感谢您的提示,毫无疑问,我会在某个时候使用它
  • 糟糕,你误会了。看起来应该可以,但是请为我尝试两件事;检查它是否与引号'_wpsc_author'中的_wpsc_author一起使用,并且get_the_ID()返回正确的ID
猜你喜欢
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
相关资源
最近更新 更多