Support » Fixing WordPress » Independent line of code in the posts

  • Hello,
    When creating a new post I would like to add another option. It would be very similar to the option “tags” already existing. As you can enter tags and they will be display under the article, I would like an option “Source” where you can write a line of text (to give the source of the image used on my post). It would display as tags, under the article in a simple and discreet line. The easy way would be to add it directly in the post. I’ve already tried it but it is not optimal as it conflicts with others changes I’d like to make. I would like to treat it in the code as an independent part (not together with the post) like the title, the category or the tags.
    Thank you!

Viewing 14 replies - 1 through 14 (of 14 total)
  • If you can link to a post we can see how you can (or cannot) do that.

    Here’s the link:
    http://lemoncoco.fr/la-peau-parfaite/

    It’s the line “Source: Getty Images / Ilovedoodle”
    I would like to look like this but right know it is inside the post and it’s not the “cleanest” way to do so. Moreover, I have problems with it when trying to add new functionalities like a “like button”.

    Michael

    (@alchymyth)

    Forum Moderator

    using a custom field might be a possibility;

    http://codex.wordpress.org/Custom_Fields

    you can create a new input section below the post editor instead of using the ‘normal’ custom fields.

    add something like this into functions.php of your theme: http://pastebin.com/GMDSwFXY

    you can then either use a filter to append it automtically at the end of the post content:

    //output the custom field after the post content via filter function//
    add_filter( 'the_content', 'source_post_meta_output', 30 );
    
    function source_post_meta_output( $text ) {
    	global $post;
    	if( $source = get_post_meta($post->ID, '_source_custom_meta_input_value', true ) ) $text .= '<span class="source"><span>Source:</span> ' . $source . '</span>';
    return $text;
    }

    or use this line somewhere in the template (single.php ?):
    <span class="source"><span>Source:</span> <?php echo get_post_meta($post->ID, '_source_custom_meta_input_value', true ); ?></span>

    (adapt to your css classes etc)

    Thank you!! That’s exactly what I wanted! 🙂

    And how can I make it appear only if the input is given?
    I want to use the second option u told me (line somewhere in the template (single.php ?))
    I’ve tried this, but i may not be recalling the good key ($source) because it shows even if there is no input text :

    <?php
    		$source=custom_meta_input_value;
    		if (!empty($source)) {
    		?>
    		    <div class="source"><span class="source-photos">Source Photo(s) :</span> <?php echo get_post_meta($post->ID, '_source_custom_meta_input_value', true ); ?></div>
    		<?php
    		} else { echo "Nothing";
      		}
    		?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    I also tried “this_custom_meta_input” instead of “custom_meta_input_value”

    Michael

    (@alchymyth)

    Forum Moderator

    try:

    <?php
    $source = get_post_meta( $post->ID, '_source_custom_meta_input_value', true );
    if ( isset($source) ) {
    ?>
    <div class="source"><span class="source-photos">Source Photo(s) :</span> <?php echo $source; ?></div>
    <?php
    } else { echo "Nothing";
    }
    ?>

    I’ve just tried but I’m still having the same problem.
    It shows either way, even if there’s nothing writen
    Any other ideas?
    Thanks!

    Although changing “isset” to “empty” seems to work… but the other way around of course. Is there a way to inverse the conditions then? Something like:

    <?php
    $source = get_post_meta( $post->ID, '_source_custom_meta_input_value', true );
    if ( empty($source) ) {
    ?>
    echo "Nothing";
    <?php
    } else {
    div class="source"><span class="source-photos">Source Photo(s) :</span> <?php echo $source; ?></div>
    }
    ?>
    Michael

    (@alchymyth)

    Forum Moderator

    simply if( $source ) works for me:

    <?php
    $source = get_post_meta( $post->ID, '_source_custom_meta_input_value', true );
    if ( $source ) {
    ?>
    <div class="source"><span class="source-photos">Source Photo(s) :</span> <?php echo $source; ?></div>
    <?php
    } else { echo "Nothing";
    }
    ?>

    I, personally, would never give advice to anyone to ever use Getty Images without consent…

    And, I can say that.

    Thank you! Now it’s working!

    Why if give the source it shouldn’t be a problem, right?
    What do u mean by “I can say that”?

    Why if give the source it shouldn’t be a problem, right?

    No. Getty Images is a business that charges fees and royalties. You may want to Google ‘sued by Getty images’

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Independent line of code in the posts’ is closed to new replies.