Display custom field in product (wordpress) - php

I would like to display custom field on product page.
I made field (Advanced Custom Fields) and add a rule to display in selected category on Posts. It works, I placed there a simple text and would display in product page. In editor I edit template and pasted the code:
<?php the_field( 'my_info' ); ?>
Unfortunately nothing appears.
I try also something like this:
<?php
query_posts('cat=195&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<?php if( get_field('my_info') ): ?>
<?php the_field('my_info'); ?>
<?php endif; ?>
<?php endwhile;
?>
After this code field display, but there is a problem with loading other page sections.
What I make wrong?

Since query_posts() is used, you have to put wp_reset_query(); after the endwhile(). Otherwise use WP_Query .
<?php
$query=WP_Query('cat=195&posts_per_page=1');
while ($query->have_posts()) : $query->the_post(); ?>
<?php if( get_field('my_info') ): ?>
<?php the_field('my_info'); ?>
<?php endif; ?>
<?php endwhile; ?>

Related

Wordpress Custom Theme: Plugins Won't Load or Display Only Code

This is my first attempt at creating a theme purely from scratch. Before this I just used underscores_me and made most of my changes to style.css and left the majority of PHP alone, because I'm a novice with it.
My issue is that plugins are not working. None of the ones I have installed work. At this point I have been trying plugins that create an event calendar, but I'm assuming that any and all plugins will have issues. In the areas that are meant to display the calendar, there are two things I'm seeing. Plugin-generated pages show nothing at all (aside from the theme visuals) and plugins that are inserted into an admin-created page display plugin-generated code.
I am using WampServer. I have wp_footer(); and wp_head(); in the correct places. My functions.php file was created from the example found at https://scanwp.net/blog/create-a-wordpress-starter-theme-from-scratch/ and the only adjustment I have made to it so far is to remove the fontawesome line of code.
My index.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
<?php get_footer(); ?>
And my page.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
<?= get_post_field('post_content', $post->ID) ?>
<?php get_footer(); ?>
First of all, you have to understand that in your displayed files, there are no functions that recall objects that will later show the page content.
Then, in your index.php file there is an error, besides what was said above, because you're calling the_title () (function) that extrapolates the title of a post or page via the post object, which in this case should be extracted within the while loop contained within the if condition.
Then try editing the files as follows.
index.php
<?php
get_header(); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if( is_singular() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts to display</h1>
<?php endif; ?>
<?php get_footer(); ?>
and page.php
<?php
get_header(); ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
However, within the wordpress codex you will find all the guides well written on any type of function, look no further.
source: https://codex.wordpress.org/

Wordpress posts displaying the same content

I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11.
However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.
Here is the loop on the custom page template
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else:
// no posts.
endif;
?>
And here is what I have on single.php
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
You should call the_post() in single.php before doing anything else.
Try this:
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Leave your other code alone. It looks like it should be working as expected.
Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.
In single.php use the following code to get the content and title.
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
use this in your custom page, i used wp_reset_postdata();
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else:
// no posts.
endif;
?>
And on single.php use this
<?php
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
?>

Wordpress ACF repeater field loop

I am trying to loop out a repeater field in my wordpress front-page template
But for some reason the div is empty and doesn't seem to work.
I am 100% sure my code is correct so there must be an issue.
Anyone has any idea what it could be?
This is how my loop looks like, the field keys are correct! :)
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
This is what my front-page.php looks like. The funny thing I've used this in an other project before and everything worked fine. Now my screen is just blank, I have no idea what's going on.
if have_rows('field_56e7d8bebb545') is really true, debug the_row();
var_dump(the_row());
You can view the_result, if is empty, maybe you must set a second parameter for have_row($field_name, $post_id)
field_56e7d8bebb545 is the field key, not the field name. The field name must be used in the have_rows() function. This can be found on the Advanced Custom Fields screen next to the label -
Try this one .. since you have not printed any such thing that can be displayed in browser .. you only provided the data attribute to div.
Write something inside inner HTML might see you repeater values
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"><?php the_sub_field('image'); ?></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>

Manually show specific posts on homepage

To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});

Wordpress woocommerce empty my account + car page

I am stuck when trying to install woocommerce. I add managed to do it, but suddenly, I don't know why, the "my account" page and "cart" page are empty, if I look into the source code, the content is here but between and therefore invisible. I have no idea and have been searching for hours on internet and stackoverflow with no luck!
Has anyone an idea of why this is doing this?
I have re-installed three times the plug-in, deleted everything and reinstalled it, it sill does the same error....
I haven't touched anything in the code and the pages are chosen in the backend
EDIT :
My woocommercePage is
<?php get_header(); ?>
<?php woocommerce_content(); ?>
<?php get_footer(); ?>
And my page is :
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php get_footer(); ?>
The WooCommerce "My Account" and "Cart" pages should just be regular WordPress pages. The WooCommerce functionality is added by using shortcodes in the page content. You may also include other content and shortcodes if you would like, but the following shortcodes are required.
For the Cart use:
[woocommerce_cart]
And for My Account use:
[woocommerce_my_account]
Your page template will need to make a call to the_content() so that the $post->post_content value is passed through the the_content filter which will call do_shortcode() to process the shortcodes.
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- output the page content here to process shortcodes -->
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php get_footer(); ?>

Categories