Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här visar du customize-fält utanför loopen i WordPress

Vill du visa customze-fält utanför loopen i WordPress? Normalt sett visas customize-fält inuti WordPress-loopen tillsammans med annat content och metadata i posten. I den här artikeln visar vi hur du kan displayed customize-fält utanför loopen i WordPress.

How to display custom fields outside the WordPress loop

Vad är customize-fält i WordPress?

Custom fields allow you to add additional meta data into your WordPress posts and then display them along with your post content.

Du kan add to custom-fält genom att aktivera metaboxen custom fields under Screen Options. Du kan också skapa egna metaboxar i WordPress för att ge dina custom-fält ett bättre användargränssnitt.

Adding custom field to a WordPress post or page

Mer detaljer finns i vår guide för nybörjare om hur du använder WordPress custom fields.

Eftersom custom fields addar metadata till posts kan de enkelt visas inuti WordPress-loopen tillsammans med annat content i posten. Men ibland kanske du vill displayed dem utanför loopen. Till exempel i en widget i sidebar. Det är då det blir lite knepigt.

Med detta sagt, låt oss se hur du enkelt kan displayed custom fields utanför loopen i WordPress.

Display Custom Fields Data Utanför Loopen i WordPress

Istället för att visa custom fields meta data utanför loopen, ska vi faktiskt visa you hur man använder flera loopar i your WordPress themes utan att påverka huvudslingan.

Den här artikeln är obligatorisk för att du ska kunna add to kod till dina WordPress theme-filer. Om du inte har gjort det tidigare kanske du vill läsa vår guide om hur du copy and paste kod i WordPress.

Du måste add to följande kod till dina theme-filer där du vill visa data för custom fields i WordPress.

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>

Den här koden hämtar helt enkelt den globala variabeln $wp_query för att få ID:n för posten. Därefter använder den funktionen get_post_meta() för att hämta och mata ut dina custom field-data.

Glöm inte att ändra Your-Custom-Field med ditt faktiska custom-fält.

Du kan customize koden för att matcha dina behov. Du kan också använda andra argument för sökning för att hämta och visa data för custom fields för olika posts och pages.

Låt oss ta en titt på ett annat exempel. Den här använder WP_Query-klassen som är ett mycket bättre och mer flexibelt sätt att använda flera loopar i dina WordPress temafiler.

Lägg helt enkelt till den här koden till ditt tema eller barntema där du gillar att visa det anpassade fältet.

$args = array ( 
// Post or Page ID
'p' => 231,
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo get_post_meta( get_the_ID(), 'Mood', true);
		}

	
	/* Restore original Post Data */
	wp_reset_postdata();

} else {

echo 'Nothing found';
	
}

Glöm inte att ersätta Mood med ditt eget custom fältnamn och post ID:n med ditt eget post eller page id.

Custom field in WordPress sidebar widget

Det var allt för nu.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du visar customize-fält utanför loopen i WordPress. Du kanske också vill se vår WordPress Theme Cheat Sheet för Beginnare.

Om du gillade den här artikeln, vänligen prenumerera på vår YouTube-kanal för WordPress video tutorials. Du kan också hitta oss på Twitter och Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

9 kommentarerLämna ett svar

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Jiří Vaněk says

    Can the author’s field be added this way? I have a website with multiple authors, and under the author’s name in the article, I would like to always display their bio (author information). Can this be done with a snippet, or would something more complex like the Advanced Custom Fields plugin be better? I’m a novice with these fields and don’t understand them very well yet.

    • WPBeginner Support says

      As long as the coauthors are in your custom fields you can use this method or advanced custom fields to display the author information.

      Administratör

      • Jiří Vaněk says

        Thank you for the confirmation. I downloaded the Advanced Custom Fields plugin and am trying to create custom fields for article authors. I also went through this article again to better understand how these fields work. Hopefully, everything will go well. In any case, I now have at least a slightly better understanding of this issue. Thank you very much.

  3. Daniel R says

    Hi there,

    Great article, I’ve used WPB a few times to help me work some things out!

    I’m currently trying to get the custom field information from the most recent post in a specific category, and to display this as inline text within a paragraph.

    Do you know if there is any plugin for this or if we can achieve this with PHP/JavaScript?

    Basically what I want to ask WordPress is ”Go and get the most recent post in the todays-tip category then find the value of the custom field ’odds’ and display ’odds’ inside this span.’

    Really I’d like to stay away from hard coding this into the page and would prefer a shortcode/JS solution as the paragraph is editable. Basically one of the webmasters might go in and change the text in the paragraph but still want to show the ’odds’ in a certain place. The page is built on a drag-n-drop editor on xPro.

    • Daniel R says

      I’m currently using a recent posts plugin shortcode to display the title in another paragraph. I then strip back all the styling to make the text inline with the paragraph. It’s a bit of a dodgy route!

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.