0

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?! :(

3
  • 1
    Are there supposed to be two different variables with similar names, $news and $newss? Also, you are using the $key variable as an argument for get_post_meta() before you set it. Commented Dec 5, 2012 at 23:48
  • You have used get_post($single->ID), where $single came from ? Commented Dec 5, 2012 at 23:53
  • thanks chris, you steared me in the right direction! and yes $news and $newss are supposed to be different :) Commented Dec 5, 2012 at 23:55

1 Answer 1

1

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. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.