I'm trying to add a custom field to my Wordpress page but it's not working. When I set its value nothing happens on the HTML.
I read the Wordpress documentation and I tried to follow the steps there but something went wrong.
Everything else it's working, like the_title(), the_post_thumbnail()...it's just this custom field that I it's not Working :(
https://wordpress.org/support/article/custom-fields/
https://codex.wordpress.org/Function_Reference/register_post_type
Custom Field:
Functions.php
$supports = array (
'title',
'editor',
'thumbnail',
'custom-fields'
);
HTML
<?php
$args = array ('post_type' => 'produtos');
$loop = new WP_Query( $args );
if ($loop->have_posts() ) {
while ($loop->have_posts() ) {
$loop->the_post();
?>
<div class="col-xl-2 col-lg-3 col-md-3 col-sm-4 col-md-3 col-12">
<div class="produtos-head">
<div class="img-fluid produtos-img">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="produtos-titulo d-flex align-items-center justify-content-center">
<?php the_title(); ?>
</div>
<div class="preco-original"> Price:R$
<?php $original_price = get_post_meta($post->ID, 'original_price', true);
if($original_price){ ?>
<p>
<? echo $original_price; ?>
</p>
<?php
}else{
}
?>
</div>
<div class="preco-promocional"> <span> Sale: </span>
<span class="preco-promocional-number"> $ </span>
</div>
<button class="btn-vendedor"> Contact </button>
</div>
<?php
}
}
?>
</div>
</div>
use get_the_ID() instead of $post->ID and need to change <? echo $original_price; ?> to <?php echo $original_price; ?>
Related
I have a problem with woocommerce. I'm using the woocommerce_before_main_content hook to add a .jumbotron page cover. I was able to display the content like page title and categories that needs to appear on the background image, but the image that is displayed is the worng one, I don't know why but wordpress will show the first product image thumbnail and the image I've selected as featured inside the shop page of woocommerce is different. Is there a solution?
I'm using this code:
function btheme_woocommerce_before_main_content()
{
if( has_post_thumbnail() ):
?>
<div class="jumbotron jumbotron-fluid shop-page-cover" style="background-image:url('<?php echo the_post_thumbnail_url(); ?>');">
<!-- <div class="parallax" data-parallax-image></div> -->
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h1 class=""><?php woocommerce_page_title(); ?></h1>
<?php $categories = get_terms( array('taxonomy' => 'product_cat') ); ?>
<?php foreach( $categories as $category ): ?>
<h4 class="category-name d-inline"><?php echo $category->name; ?></h4>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<?php endif; ?>
<div class="container-fluid content-wrapper" id="uptheme-woocommece-wrapper">
<?php
}
add_action( 'woocommerce_before_main_content', 'btheme_woocommerce_before_main_content', 10 );
first you need to get post thumbnail by id
<?php
$shop_page_id = wc_get_page_id('shop');
function btheme_woocommerce_before_main_content()
{
if( has_post_thumbnail($shop_page_id) ):
?>
<div class="jumbotron jumbotron-fluid shop-page-cover" style="background-image:url('<?php echo get_the_post_thumbnail_url($shop_page_id,'full'); ?>');">
<!-- <div class="parallax" data-parallax-image></div> -->
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h1 class=""><?php woocommerce_page_title(); ?></h1>
<?php $categories = get_terms( array('taxonomy' => 'product_cat') ); ?>
<?php foreach( $categories as $category ): ?>
<h4 class="category-name d-inline"><?php echo $category->name; ?></h4>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<?php endif; ?>
<div class="container-fluid content-wrapper" id="uptheme-woocommece-wrapper">
<?php
}
add_action( 'woocommerce_before_main_content', 'btheme_woocommerce_before_main_content', 99 );
The PHP code seems to be blocking or interfering with my advanced custom fields as it doesn't display unless I remove the PHP code then it does. I can't figure out where the issue is.
Any help is much appreciated.
PHP
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) {
$newsCat = 'cat=5,7&showposts=3';
} else {
$newsCat = 'cat=6,8&showposts=3';
}; ?>
Advanced Custom Fields
<div class="carousel-item active">
<div class="row py-5">
<?php if( have_rows('block') ): ?>
<?php while( have_rows('block') ): the_row();
// vars
$content = get_sub_field('content');
?>
<div class="col-lg-4 col-md-4">
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) { ?> <!--Check if url contains the word "items" -->
<h2 class="fw-b c-blue mt-0">Ár bhFís</h2>
<?php } else { ?>
<h2 class="fw-b c-blue mt-0">Our Vision</h2>
<?php } ?>
</div>
<div class="col-lg-8 col-md-8">
<p class="c-blue mb-0"><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
Fixed it by just changing how News articles where added.
<div class="row pt-4 pb-3">
<?php
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'post'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6 mb-5">
<div class="w-100 mb-2 px-2">
<img class="w-100" src="<?php $featimage = the_post_thumbnail_url('news-image'); ?>" alt="">
<p class="text-muted mt-4 mb-2"><?php echo get_the_date('dS M, Y'); ?></p>
<h3 class="c-blue"><?php the_title(); ?></h3>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
So this is driving me kind of crazy, I just can't seem to get the page navigation to work. I installed a plugin wp-pagenavi but it still doesn't work. Is there anyone that could help me getting it to work. Here is my code (the loop):
<?php $args = array(
'post_type' => 'portfolio',
'posts_per_page'=> 10,
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'foto_video_type',
'field' => 'slug',
'terms' => 'foto'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<?php $naam = get_field('naambedrijf');
$soort = get_field('soort_uitje');
$foto = get_field('flickr_fotoset'); ?>
<div class="col s12">
<div class="title">
<h2 class="truncate" style="line-height:20px;"><?php echo $naam; ?> | <?php echo $soort; ?></h2>
<a class="fotos" href="https://www.flickr.com/photos/../sets/<?php echo $foto; ?>" target="_blank"><small>Bekijk alle foto's</small></a>
</div>
<div class="flickrphotoset">
<?php echo do_shortcode('[slickr-flickr id="" search="sets" set="' . $foto . '" size="large" items="9" bottom="10" responsive="on" type="thumbnail" galleria_options="lightbox:true;thumbnail:lazy"]'); ?>
</div>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
<?php wp_pagenavi(); ?>
And this is my archive code:
<section id="collaps">
<div class="row">
<div class="col s12 offset-s0 m12 l7 offset-l5">
<ul class="collapsible z-depth-1" data-collapsible="accordion">
<li>
<div class="collapsible-header"><i class="fa fa-camera" aria-hidden="true"></i><p>Foto</p></div>
<div id="portfolio" class="collapsible-body">
<div class="row">
<?php get_template_part('loop-foto'); ?>
<div class="col s12">
</div>
</div>
</div>
</li>
<li>
<div class="collapsible-header"><i class="fa fa-video-camera" aria-hidden="true"></i><p>Video</p></div>
<div id="portfolio" class="collapsible-body">
<div class="row">
<?php get_template_part('loop-video'); ?>
</div>
</div>
</li>
</ul>
</div>
</div>
</section>
When i add the "older" Wordpress code:
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries', '1000') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »', '1000') ?></div>
</div>
It kind of works. But page/2/ give a 404 error... What am I doing wrong?
I'm not sure if it is your case, but as I faced a similar case, I'm going to post my solution here:
In my case the problem was that I had a page with a permalink equal to the post type, so just changing the page slug solved the problem.
I have a custom post type set up called TESTIMONIALS and two CPT categories set up which are CLIENT TESTIMONALS & CLINIC TESTIMONIALS
I am trying to display only the posts from the CLIENT TESTIMONALS CPT category.
What would I need to add to the below to achieve this?
<div role="tabpanel" class="tab-pane fade" id="profile">
<?php query_posts('post_type=testimonials'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php endwhile; // end of the loop. ?>
</div>
You can use something like this.
<?php
$type = 'testimonials';
$args=array(
'post_type' => $type,
'category'=>'CPT',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
}
?>
I am using crowdfunding by astoundify to ceate custom post type download(which is campaign actually). I have created a signle-download.php page as a template but post pages do not seem to use this template. Is there anything else I have to do?
code for single-download.php
<?php
global $wp_embed;
get_header();
?>
<div class="bigcontainer4">
<div class="container">
<div id="content">
<div class="clearfix">
<?php while ( have_posts() ) : the_post(); $campaign = new ATCF_Campaign( $post->ID ); ?>
<div class="project_content_line">
<div class="blog_post">
<div class="project_image_bg">
<div class="project_image">
<?php if ( $campaign->video() ) : ?>
<div class="project_video">
<div class="project_videobox">
<?php echo $wp_embed->run_shortcode( '[embed]' . $campaign->video() . '[/embed]' ); ?>
</div>
</div>
<?php else :
the_post_thumbnail('blog-single-image');
endif; ?>
</div>
<h1>
<?php the_title() ;?>
</h1>
</div>
</div>
<div class="blog_sidebar">
<div class="project_sb_date">
<?php printf( __( '%s'), get_the_date('') ); ?>
-
<?php printf( __( '%s'), $campaign->end_date('') ); ?></div>
<div class="project_sb_date_rem">
<?php echo $campaign->
days_remaining(); ?>
<?php echo _n( $campaign->days_remaining(), 'day left', 'days left' ); ?></div>
<div class="project_small_excerpt">
<?php //echo excerpt(26); ?>
<div class="project_list_box_loader">
<div class="project_list_box_loaderbar">
<span style="width: <?php echo $campaign->percent_completed(); ?>"></span>
</div>
</div>
</div>
<div class="project_money_data">
<div class="project_money_data1">
<p>
<?php echo $campaign->current_amount(); ?></p>
<span>Raised</span>
</div>
<div class="project_money_data2">
<p></p>
<span>
of
<?php printf( __( '%s'), $campaign->goal() ); ?></span>
</div>
<?php if ( $campaign->is_active() )
echo edd_get_purchase_link( array(
'download_id' => $post->ID,
'class' => '',
'price' => false,
'text' => __( 'Contribute Now', 'fundler' )
) );
?>
</div>
</div>
<div class="project_content_line">
<div class="blog_post">
<div class="project_content">
<?php the_content(); ?>
<div class="project_content_updates">
<div class="project_sb_title">
<p>Latest</p>
<span>Updates</span>
</div>
<?php echo $campaign->updates() ?>
</div>
<?php comments_template(); ?>
</div>
</div>
<div class="blog_sidebar">
<div class="project_author_box">
<div class="project_sb_title">
<p>About</p>
<span>The Author</span>
</div>
<div class="project_author_image">
<?php
$author_email = get_the_author_meta('email');
echo get_avatar($author_email, '512');
?>
<div class="project_author_image_data">
<p>
<?php if ( '' != $campaign->author() ) :
printf( __( '%s', 'fundler' ), esc_attr( $campaign->author() ) );
endif; ?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php get_footer(); ?>
According to Wordpress documentation:
WordPress looks for template files with specific names in the current
Theme's directory and uses the first matching template file listed
under the appropriate query section below.
So, I guess, you have another template matching condition. And wordpress uses it to display your page.
Added from comment: May be you are using just another theme, not the one, your file resides in?