自定义字段在Wordpress中不起作用

时间:2012-12-05 23:40:44

标签: php wordpress wordpress-plugin

我使用wordpress插件实现此代码以检索自定义字段值,然后在自定义字段值为true时在帖子URL的末尾添加值。

因此,对于下面的示例,如果自定义字段“testme”值为“news”,则应在URL的末尾添加$ news值,即“fromwhere = news”。 这个概念/代码在我使用的插件中运行良好,然后我尝试在主Wordpress循环中应用它并且它不起作用。这是我在主要wordpress循环中使用的代码:

/* entry_title */
if ( !function_exists( 'wpstart_entry_title' ) ) {
    function wpstart_entry_title() {
    $post = get_post($single->ID);
    $newss = get_post_meta($post->ID, $key, TRUE);
                $key = 'testme';
                $news = '?fromwhere=news"';
             if($newss == 'news') {

        if ( is_single() || is_page() ) { ?>
            <h1 class="entry-title"><?php the_title(); ?></h1>
        <?php } elseif (is_404()) { ?>
            <h1 class="entry-title"><?php _e( 'Page not found', 'wpstart' ); ?> - 404</h1>
        <?php } else { ?>
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>'.$news.'" 
title="<?php the_title_attribute( array('before' => esc_attr__( 'Permalink: ',   'wpstart' ), 
'after' => '')); ?>" rel="bookmark">
<?php the_title(); ?></a></h2>
        <?php }

        }
        else { echo '<h2>DID NOT WORK</h2>';
        }
    }
}

所有帖子标题都返回“DID NOT WORK”,即使是那些我将“testme”自定义字段设置为“news”的帖子。为什么这不起作用?! :(

1 个答案:

答案 0 :(得分:0)

我只是移动了$ newss值,因此它低于其他关键函数..就像这样......

/* entry_title */
if ( !function_exists( 'wpstart_entry_title' ) ) {
function wpstart_entry_title() {
$post = get_post($single->ID);
            $key = 'testme';
            $news = '?fromwhere=news"';
            $newss = get_post_meta($post->ID, $key, TRUE);
            if($newss == 'news') {
                      more code...

这就是诀窍。 :)

相关问题