user1658560
user1658560

Reputation: 540

Custom Fields Not Working in Wordpress

I implemented this code with a wordpress plugin to retrieve a custom field value and then add a value at the end of a post URL if the custom field value is true.

So for the below example, if the custom field "testme" value is "news", it should add the $news value at the end of the URL, which is ?fromwhere=news". This concept / code worked fine in the plugin I was using, then I tried to apply it inside the main Wordpress loop and it does not work. Here is the code I'm using inside the main wordpress loop:

/* 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>';
        }
    }
}

All the post titles return "DID NOT WORK", even those that I set the "testme" custom field to "news". Why isn't this working?! :(

Upvotes: 0

Views: 257

Answers (1)

user1658560
user1658560

Reputation: 540

I simply moved the $newss value so it was below the other key functions.. like so..

/* 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...

This did the trick. :)

Upvotes: 1

Related Questions