Display metabox values in Wordpress - php

I'm trying to add metabox with date in Wordpress. I used code from this page. It works well, but I have a problem displaying the metabox values on page.
My loop:
<?php query_posts(array('post_type' => 'event' , 'posts_per_page' => 9999, 'orderby' => 'menu_order', 'order' => 'ASC' ));
if (have_posts()) : while (have_posts()) : the_post();
$custom = get_post_custom($post->ID); ?>
<div class="event">
<h2><?php the_title(); ?></h2>
<p>Date: <?php echo get_post_meta($post->ID, '_day', true);?>.<?php echo get_post_meta($post->ID, '_month', true);?>.<?php echo get_post_meta($post->ID, '_year', true);?></p>
<p>Hour: <?php echo get_post_meta($post->ID, '_hour', true);?>:<?php echo get_post_meta($post->ID, '_minute', true);?></p>
<?php the_content(''); ?>
</div>
<?php endwhile; endif; ?>
I added custom post type 'event'. Values in metabox(date/location) have been saved, but it haven't displayed on page.
Why it doesn't work?

Problem solved. I have written incorrect meta_key.
For example:
Instead '_day' must be '_start_day' (for Start day) or '_end_day' (for End day)
Correct code for display metabox with Start Day:
<?php echo get_post_meta($post->ID, '_start_day', true);?>

Related

Show recent blog posts, but exclude current post

I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>

Woocommerce how to show custom attributes other than default in home page?

After a long and recursive search on the web, I didn't find a solution for my needs.
I need to display, in home page, not only product name and price, but also some custom attribute I already have set in all my products. Let's say 'year' and 'author'.
All snippets found are single product or shop page related only, nothing for home page (or any other page anyway).
Tried also with woocommerce shortcodes, but they are not applicable for that purpose.
I already use:
[recent_products per_page="6" columns="6" orderby="year" order="desc"]
to display recent products ordered by "year" att (I found a snippet to add 'orderby' custom att).
Any help will be really appreciated.
Here's the code that you can use for displaying products from a specific category with meta data on the home page:
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'categoryNAme', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3>Title: <?php the_title(); ?></h3>
<h3>Content: <?php the_content(); ?></h3>
<h3>Date: <?php the_date(); ?></h3>
<h3>Author: <?php the_author(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

What is the file to display a list of "post type" in Wordpress?

I am looking for what file I can display my list post type. I've tried to display in archive-posttype.php but I haven't success.
<?php
$newsArgs = array( 'post_type' => 'agenda', 'posts_per_page' => 4);
$newsLoop = new WP_Query( $newsArgs );
while ( $newsLoop->have_posts() ) : $newsLoop->the_post();
?>
<?php the_post_thumbnail(); ?>
<h1><?php the_title(); ?></h1>
<p><strong><?php the_time('d.m.Y') ?></strong></p>
<p><?php the_content(); ?></p>
<p><?php //echo get_the_term_list( $post->ID, 'categorias', 'Categorias: ', ' '); ?></p>
<p>Local: <?php echo get_post_meta($post->ID, 'valor_meta', true); ?></p>
<?php endwhile; ?>
In place of $posttype you must place the actual post type name, so if you are registered CPT with name agenda the file that you can display it is archive-agenda.php.
You can find more information here and here.
By the way, if you display your CPT (agenda for example) in (archive-agenda.php) and you use 'pretty permalinks' - your url for displaying agenda is your-site-name.com/agenda/, then no need to use custom query to display them, you can use default loop, i.e. in file archive-agenda.php you can display your custom posts as usual:
if ( have_posts() ) {
while( have_posts() ) : the_post();
the_title();
endwhile;
endif;

Struggling to get custom meta data to show in wordpress post

I'm following a guide at the moment and have managed to create a custom post type and meta boxes. However, getting the meta data to display in a post just isn't happening for me at the moment.
All i want is the meta box to have 3 custom text fields that I can then output in a post.
This is my functions file (the part that defines the post type and meta boxes):
add_action('init', 'product_manager_register'); // Calls function to set up post-type
function product_manager_register() {
$args = array(
'label' => __('Product Manager'),
'singular_label' => __('Skin'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => array('slug' => 'skins', 'with_front' => false),
);
register_post_type('products' , $args ); // runs the function
}
//Add featured image support to the theme
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
add_action("admin_init", "product_manager_add_meta");
function product_manager_add_meta(){
add_meta_box("product-meta", "Product Options", "product_manager_meta_options", "products", "normal", "high");
}
function product_manager_meta_options(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
$custom = get_post_custom($post->ID);
$buylink = $custom['buylink'][0];
$price = $custom['price'][0];
$previewlink = $custom['previewlink'][0];
?>
<style type="text/css">
<?php include('productmeta.css'); ?>
</style>
<div class="product_extras">
<div> <label> Buy Link: </label> <input name="buylink" value="<?php echo $buylink; ?>" /></div>
<div> <label> Price: </label> <input name="price" value="<?php echo $price; ?>" /></div>
<div> <label> Preview Link: <input name="previewlink" value="<?php echo $previewlink; ?>" /></div>
</div>
<?php
}
add_action('save_post', 'save_product_meta');
function save_product_meta(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return $post_id;
}else{
update_post_meta($post->ID, "buylink", $_POST["buylink"]);
update_post_meta($post->ID, "price", $_POST["price"]);
update_post_meta($post->ID, "previewlink", $_POST["previewlink"]);
}
}
?>
And I display the information in single-products like this:
<?php get_header() ?>
<div class="container content"><!-- Begin maincontent Container -->
this is the page i'm editing
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$buynowlink = $custom ["buynowlink"][0];
$price = $custom ["price"][0];
$previewlink = $custom ["previewlink"][0];
?>
<div class="post group">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
<?php print "<p>$buynowlink</p>"; ?>
<?php print "<p>$price/p>"; ?>
<?php print "<p>$buynowlink</p>"; ?>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
I know I'm probably doing something stupid but any help would be really appreciated. I know I could do this with a plugin but I'd rather learn to do it the proper way.
You just have the wrong ID's in place
so to output correctly its should be:
<?php
$custom = get_post_custom($post->ID);
$buynowlink = $custom ["buylink"][0];
$price = $custom ["price"][0];
$previewlink = $custom ["previewlink"][0];
?>
and to print it - you have the buy link in the previews rule as well as an open p tag so it should be:
<?php print "<p>$buynowlink</p>"; ?>
<?php print "<p>$price</p>"; ?>
<?php print "<p>$previewlink</p>"; ?>
Hope this helps
I think I see a couple of syntax errors in your code, such as the way you want to print the custom field's values and so on.
I would recommend this approach as opposed to the approach you are using, and I'll explain why in a bit, after the code.
PHP WordPress Loop with get_post_meta();
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!--This is a conditional statement to see if the value of the custom field has something, and if it does then it shows it, otherwise it doesn't render anything-->
<?php if ( get_post_meta($post->ID, 'cf-size', true)):?>
<h1>Custom Field Value: <?php echo get_post_meta($post->ID, 'cf-size', true);?></h1>
<?php endif;?>
<?php endwhile; endif; ?>
You can notice that cf-size is the name of the custom field and that I'm checking for it's value on the current post. The code above will surely work as I've used it many times in my own creations.
Here is an example of how to pull 3 fields with the same if statement... just remember that if the condition doesn't validate as "true" (meaning that all 3 fields have a value) then none will show even if 2 or 1 does have a value.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!--This is a conditional statement to see if the value of the custom field has something, and if it does then it shows it, otherwise it doesn't render anything-->
<?php if ( get_post_meta($post->ID, 'cf-size', 'cf-color', 'cf-brand', true)):?>
<h1>Custom Field Value for Size: <?php echo get_post_meta($post->ID, 'cf-size', true);?></h1>
<h1>Custom Field Value for Color: <?php echo get_post_meta($post->ID, 'cf-color', true);?></h1>
<h1>Custom Field Value for Brand: <?php echo get_post_meta($post->ID, 'cf-brand', true);?></h1>
<?php endif;?>
<?php endwhile; endif; ?>

Wordpress: How can I display the custom fields from a custom post type on a page?

I have created a custom post type called "box-img."
I have to custom fields in it, one called "img-url" and "img."
The "img" fields contains an image.
I want to be able to display those 2 fields on my page. Right now I have:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'box-img',
'showposts' => 4
) );
?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php $key = get_post_meta($post->ID, 'img-url'); ?>
<p><?php if($key) { echo $key[0]; } else { the_title(); }; ?></p>
<?php $key1 = get_post_meta($post->ID, 'img-url'); ?>
<p> <?php echo $key1; ?> </p>
<?php endwhile;
wp_reset_query(); ?>
I have tried those 3 different ways to see what I get, but all I can get is the title to show!
Any help would be very helpful!
Thanks!
Set the third optional parameter to true to return a single result (string), e.g.:
$key1 = get_post_meta( $post->ID, 'img-url', true );
http://codex.wordpress.org/Function_Reference/get_post_meta

Categories