WordPress.org

Ready to get started?Download WordPress

Forums

Independent line of code in the posts (15 posts)

  1. Xaplus
    Member
    Posted 1 year ago #

    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!

  2. Krishna
    Volunteer Moderator
    Posted 1 year ago #

    If you can link to a post we can see how you can (or cannot) do that.

  3. Xaplus
    Member
    Posted 1 year ago #

    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".

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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)

  5. Xaplus
    Member
    Posted 1 year ago #

    Thank you!! That's exactly what I wanted! :)

  6. Xaplus
    Member
    Posted 1 year ago #

    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.]

  7. Xaplus
    Member
    Posted 1 year ago #

    I also tried "this_custom_meta_input" instead of "custom_meta_input_value"

  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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";
    }
    ?>
  9. Xaplus
    Member
    Posted 1 year ago #

    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!

  10. Xaplus
    Member
    Posted 1 year ago #

    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>
    }
    ?>
  11. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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";
    }
    ?>
  12. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

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

  13. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    And, I can say that.

  14. Xaplus
    Member
    Posted 1 year ago #

    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"?

  15. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    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'

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.