Wie kann ich Custom Fields in der index.php ausgeben?

  • Hai.

    Wie kann ich Custom Fields in der index.php ausgeben?

    Meine Schleife in der index.php sieht derzeit so aus:

    PHP
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <div id="posts_index">    
                <a href="<?php the_permalink() ?>"><?php
                    $data = $wpdb->prefix . "posts";
                    $query = "SELECT guid FROM $data WHERE post_type = 'attachment' AND post_parent = " . $post->ID;     
                    $bild= $wpdb->get_var( $query );
                    echo('<img src=' . $bild . ' height="125px" width="125px" alt="" style="" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8;this.filters.alpha.opacity=80"/>');
                ?><?php the_title(); ?></a>
            </div>
            <?php endwhile; ?>

    Es wird also nur das Bild des Posts und der Posttitel ausgeben. Ich möchte allerdings, dass unter dem Titel noch ein Customfield ausgegebn wird. Ich habe es mit folgendem Code versucht:

    PHP
    <?php $key="customfield"; echo get_post_meta($post->ID, $key, true);  ?>

    Da zeigt es allerdings nichts an... Kommt allerdings auch kein Fehler oder iwas anderes... Weiß jemand Rat?:|

    Gruß.
    STVN!

    • Anzeige

    Hallo!

    Wenn du gerade an deiner Website arbeitest oder dein aktuelles Hosting überdenkst: Wir betreiben mit NetzLiving eine Hosting-Plattform, die speziell auf Performance, Sicherheit und einfache Verwaltung ausgelegt ist.

    • ✔️ Schnelle Ladezeiten (optimiert für WordPress & Co.)
    • ✔️ Deutsche Server & DSGVO-konform
    • ✔️ Persönlicher Support (kein 0815-Ticket-System)

    Mehr erfahren

  • war zu fauel etwa eigenes zu entwickeln und habe das gefunden ...

    [COLOR=#000000]<?php[/COLOR] [COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR]have_posts[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]:[/COLOR] [COLOR=#000000]?>[/COLOR]
    [COLOR=#000000]<?php[/COLOR] [COLOR=#b1b100]while[/COLOR] [COLOR=#009900]([/COLOR]have_posts[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]:[/COLOR] the_post[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR];

    <!-- Hier kommt der Inhalt hin -->
    <!-- Den Inhalt der Felder speichern wir in Variablen -->

    [COLOR=#000000]<?php[/COLOR] [COLOR=#000088]$ordner[/COLOR] [COLOR=#339933]=[/COLOR] get_post_meta[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]post[COLOR=#339933]->[/COLOR][COLOR=#004000]ID[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000ff]'ordner'[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#009900]true[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR]
    [COLOR=#000000]<?php[/COLOR] [COLOR=#000088]$img01[/COLOR] [COLOR=#339933]=[/COLOR] get_post_meta[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]post[COLOR=#339933]->[/COLOR][COLOR=#004000]ID[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000ff]'img01'[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#009900]true[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR]

    <!-- $ordner und $img01 sind die Variablennamen in diesen der Inhalt
    gespeichert wird. das 'ordner' und 'img01' sind die Custom Field Namen
    die wir später im Beitrag mit Inhalt füllen werden im nächsten Schritt
    Fragen wir ab ob der Inhalt des NICHT Leer ist und wenn das der Fall ist
    dann soll er etwas ausgeben -->;

    [COLOR=#000000]<?php[/COLOR] [COLOR=#b1b100]if[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$img01[/COLOR] [COLOR=#339933]!==[/COLOR] [COLOR=#0000ff]''[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR] [COLOR=#000000]?>[/COLOR]
    <p class="bild"><img src)"[COLOR=#000000]<?php[/COLOR] bloginfo[COLOR=#009900]([/COLOR][COLOR=#0000ff]'home'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR]/
    wp-content/uploads/portfolio/[COLOR=#000000]<?php[/COLOR] [COLOR=#000088]$ordner[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR]/[COLOR=#000000]<?php[/COLOR] [COLOR=#b1b100]echo[/COLOR] [COLOR=#000088]$img01[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR].jpg"
    alt="[COLOR=#000000]<?php[/COLOR] the_title[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR]" /></p>
    [COLOR=#000000]<?php[/COLOR] [COLOR=#009900]}[/COLOR] [COLOR=#000000]?>[/COLOR]

    <!-- Jetzt wird, falls das Custom Field "img01" ausgefüllt ist,
    ein Bild angezeigt. Das Custom Field "ordner" sollte natürlich
    auch richtig ausgefüllt sein damit der Pfad zum Bild später stimmt.
    Jetzt muss nur noch der Loop geschlossen werden und das ganze ist fertig.
    Natürlich kann man das ganze noch mit anderen Sachen ergänzen
    aber da soll hier nicht näher drauf eingegangen werden. -->;

    [COLOR=#000000]<?php[/COLOR] [COLOR=#b1b100]endwhile[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#b1b100]else[/COLOR][COLOR=#339933]:[/COLOR] [COLOR=#000000]?>[/COLOR]
    <p>Falls keine Artikel vorhanden...</p>
    [COLOR=#000000]<?php[/COLOR] [COLOR=#b1b100]endif[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#000000]?>[/COLOR]

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!