Forum FAQForum FAQSearchSearch MemberlistMemberlist Forum ignore listForum ignore list RegisterRegister ProfileProfile Log in to check your private messagesLog in to check your private messages Log inLog in
WP custom fields

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side
View previous topic :: View next topic  
Author Message
blackshtef
mi3.crew


Joined: 13 Sep 2003
Posts: 661

PostPosted: 15.04.2009 08:49    Post subject: WP custom fields Add user to your forum ignore list Reply with quote

O hai!

Ovako, imam neka pitanja vezana za custom fieldove u Wordpressu.
Pri pisanju posta, imam slijedeće fieldove:
- thumbnail (u njega stavljam link do full-size slike koju onda plugin sreže na određene dimenzije)
- Source (u njega stavljam URL do sitea gdje sam naišao na vijest)
- Submitted_by (u njega stavljam URL do čovjeka koji je poslao vijest)

E sad, thumbnail ide u svakom postu, to je oke. Ono što mi nije oke, ondnosno ne razumijem - kako source i submitted_by prikazati samo ako je unešena neka vrijednost za taj post?

Recimo, kad koristim upute sa Wordpress Codexa, onda u slučaju kad nije unesena vrijednost sourcea ili submitted_by, ispiše mi link do tog posta Confused

Dakle, jel moguće kako srediti da:
ako ima vrijednost u polju source, ispiši koja je, inače nemoj Smile

(moram više naučit bar osnove PHP-a, pobogu Smile )
Back to top
View user's profile Send private message Send e-mail MSN Messenger
zytzagoo
mi3.crew


Joined: 25 Aug 2003
Posts: 1842
Location: Zagreb, Hrvatska

PostPosted: 15.04.2009 14:15    Post subject: Add user to your forum ignore list Reply with quote

Nesto ala ovog bi trebalo raditi prema onome sto pise na codexu:
Code:

<?php

$post_meta_source = get_post_meta($post->ID, 'source', true);
$post_meta_submitted_by = get_post_meta($post->ID, 'submitted_by', true);

if (!empty($post_meta_source)) {
    echo 'Source: ' . $post_meta_source;
}

if (!empty($post_meta_submitted_by)) {
    echo 'Submitted by: ' . $post_meta_submitted_by;
}

U ovoj varijanti je lakse za odrzavati, ovisi doduse kako i gdje zelis ispisivati vrijednosti
tih custom polja:
Code:

$custom_fields = array(
    // key => label
    'source' => 'Source: ',
    'submitted_by' => 'Submitted by: '
);
foreach ($custom_fields as $field => $label) {
    $custom_field_value = get_post_meta($post->ID, $field, true);
    if (!empty($custom_field_value)) {
        echo $label . $custom_field_value;
    }
}


Medjutim, dohvat svakog pojedinog custom fielda na taj nacin bi mogao rezultirati extra
upitima na bazu (svako polje po jedan upit, ali nisam isao gledati source get_post_meta()
funkcije), a ako je tako, onda ti je bolje koristiti ovo:

http://codex.wordpress.org/Function_Reference/get_post_custom

Tj, nesto ala ovo, sto rezultira jednim queryem za sva custom polja odjednom, a samo
odabires koja ces ispisati (i samo ako nisu prazna):
Code:

<?php
// grabs all custom fields for the post
$post_custom_fields = get_post_custom($post->ID);
// display only these fields
$displayed_custom_fields = array(
    // key => label
    'source' => 'Source: ',
    'submitted_by' => 'Submitted by: '
);
// display a field only if it has a value
foreach ($wanted_custom_fields as $field => $label) {
    $custom_field_value = isset($custom_fields[$field]) && !empty($custom_fields[$field]) ? $custom_fields[$field] : false;
    if ($custom_field_value) {
        echo $label . $custom_field_value;
    }
}


Have fun!

_________________
[+]I[+]am[+]my[+]own[+]religion[+]
Back to top
View user's profile Send private message Visit poster's website Twitter profile
maratz
mi3.crew


Joined: 24 Nov 2003
Posts: 1207
Location: ZAG

PostPosted: 15.04.2009 14:16    Post subject: Add user to your forum ignore list Reply with quote

Code:
$blah = get_post_meta($post->ID, 'blah', true);      
if ($blah != '') { }

blah je ime "polja"

Edit what zyt said :p

_________________
STOP HITTING YOURSELF! | NETIQUETTE | TYPETESTER | Hypertext rulez™ | CREATIVE NIGHTS | ACCOMMODATIONS
Back to top
View user's profile Send private message Visit poster's website Twitter profile
blackshtef
mi3.crew


Joined: 13 Sep 2003
Posts: 661

PostPosted: 15.04.2009 15:59    Post subject: Add user to your forum ignore list Reply with quote

Super tajming Very Happy

Hvala dečki, imate pivo ili dva Smile
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group