Critical error on single product page woo commerce - php

My website started showing this strange error recently which I have been unable to resolve. When I try to view a product, it returns a critical error.
I’ve done some troubleshooting and I realized the theme is the culprit. I have also checked the error log and I saw that this specific line of code is causing the error:
Uncaught Error: Call to a member function is_in_stock() on string in /home/u306409103/domains/apdbrestore.com/public_html/wp-content/themes/custom-theme/woocommerce/single-product.php:45 Stack trace: #0
When you check that line, it says:
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
To be specific the error is pointing towards the first line:
<?php if( !$product->is_in_stock() ) : ?>
I know the problem is coming from here but don’t know what next to do. What statement can I use here to do away with this error?
Thanks
Full code
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' ); ?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */

Finally got the solution to this problem and in case anyone stumbles across this, all I had to do was to update how to get the reference post ID because the existing one is deprecated already.
<?php global $product;
$product = wc_get_product( $post->ID );
$stock_quantity = get_post_meta($post->ID, '_stock', true);
//var_dump($product->get_stock_status());
?>
<?php if ($product->get_stock_status() != 'instock') : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>

Make sure that the $product is not a string or a boolean before checking for stock.
<?php if( is_object($product) && !$product->is_in_stock() ) : ?>
Or, you completly stop processing the template by bailing out at the top if no product was returned like this:
if (!is_object($product)) {
return;
}

Try to put this code, I updated your code a bit.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' );
global $product;
?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$new_product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$new_product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $new_product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );

Related

Buddyboss Group Filter

In the Buddyboss Group Loop are all groups displayed.
I want to add a filter that only groups with a specific categrorie are displayed in the first grid.
I added the comment to the first grid and found out that i can display another grid when
i duplicate the code a second time, but i have the filter problem.
Then i want to add another grid with also a filter to another group categorie.
How can i achieve this?
I dont get it how to add a filter.
<?php
/**
* BuddyBoss - Groups Loop
*
* #since BuddyPress 3.0.0
* #version 3.1.0
*/
bp_nouveau_before_loop(); ?>
<?php if ( bp_get_current_group_directory_type() ) : ?>
<div class="bp-feedback info">
<span class="bp-icon" aria-hidden="true"></span>
<p class="current-group-type"><?php bp_current_group_directory_type_message(); ?></p>
</div>
<?php endif; ?>
<?php $cover_class = bp_disable_group_cover_image_uploads() ? 'bb-cover-disabled' : 'bb-cover-enabled'; ?>
<?php if ( bp_has_groups( bp_ajax_querystring( 'groups' ) ) ) : ?>
<?php bp_nouveau_pagination( 'top' ); ?>
//FIRST GRID ONLY FOR "KOMMISIONEN"
<ul id="groups-list-kommissionen" class="<?php bp_nouveau_loop_classes(); ?> <?php echo $cover_class; ?>">
<?php
while ( bp_groups() ) :
bp_the_group();
?>
<li <?php bp_group_class( array( 'item-entry' ) ); ?> data-bp-item-id="<?php bp_group_id(); ?>" data-bp-item-component="groups">
<div class="list-wrap">
<?php if( !bp_disable_group_cover_image_uploads() ) { ?>
<?php
$group_cover_image_url = bp_attachments_get_attachment( 'url', array(
'object_dir' => 'groups',
'item_id' => bp_get_group_id(),
) );
$default_group_cover = buddyboss_theme_get_option( 'buddyboss_group_cover_default', 'url' );
$group_cover_image_url = $group_cover_image_url ?: $default_group_cover;
?>
<div class="bs-group-cover only-grid-view"><img src="<?php echo $group_cover_image_url; ?>"></div>
<?php } ?>
<?php if ( ! bp_disable_group_avatar_uploads() ) : ?>
<div class="item-avatar">
<?php bp_group_avatar( bp_nouveau_avatar_args() ); ?>
<div class="groups-loop-buttons only-grid-view">
<?php bp_nouveau_groups_loop_buttons(); ?>
</div>
</div>
<?php endif; ?>
<div class="item">
<div class="item-block">
<h2 class="list-title groups-title"><?php bp_group_link(); ?></h2>
<?php if ( bp_nouveau_group_has_meta() ) : ?>
<p class="item-meta group-details only-list-view"><?php bp_nouveau_group_meta(); ?></p>
<p class="item-meta group-details only-grid-view"><?php
$meta = bp_nouveau_get_group_meta();
echo $meta['status']; ?>
</p>
<?php endif; ?>
<p class="last-activity item-meta">
<?php
printf(
/* translators: %s = last activity timestamp (e.g. "active 1 hour ago") */
__( 'active %s', 'buddyboss-theme' ),
bp_get_group_last_active()
);
?>
</p>
</div>
<div class="item-desc group-item-desc only-list-view"><?php bp_group_description_excerpt( false , 150 ) ?></div>
<?php bp_nouveau_groups_loop_item(); ?>
<div class="groups-loop-buttons footer-button-wrap"><?php bp_nouveau_groups_loop_buttons(); ?></div>
<div class="group-members-wrap only-grid-view">
<?php echo buddyboss_theme()->buddypress_helper()->group_members( bp_get_group_id(), array( 'member', 'mod', 'admin' ) ); ?>
</div>
</div>
</div>
</li>
<?php endwhile; ?>
<?php bp_nouveau_pagination( 'bottom' ); ?>
<?php else : ?>
<?php bp_nouveau_user_feedback( 'groups-loop-none' ); ?>
<?php endif; ?>
<?php
bp_nouveau_after_loop();
Okay i found the answer by trying all possible ways.
Here is the solution code.
<?php
/**
* BuddyBoss - Groups Loop
*
* #since BuddyPress 3.0.0
* #version 3.1.0
*/
bp_nouveau_before_loop(); ?>
<?php if ( bp_get_current_group_directory_type() ) : ?>
<div class="bp-feedback info">
<span class="bp-icon" aria-hidden="true"></span>
<p class="current-group-type"><?php bp_current_group_directory_type_message(); ?></p>
</div>
<?php endif; ?>
<?php $cover_class = bp_disable_group_cover_image_uploads() ? 'bb-cover-disabled' : 'bb-cover-enabled'; ?>
<?php if ( bp_has_groups( 'group_type=kommission' ) ) : ?>
<?php bp_nouveau_pagination( 'top' ); ?>
<ul id="groups-list-kommissionen" class="<?php bp_nouveau_loop_classes(); ?> <?php echo $cover_class; ?>">
<?php
while ( bp_groups() ) :
bp_the_group();
?>
<li <?php bp_group_class( array( 'item-entry' ) ); ?> data-bp-item-id="<?php bp_group_id(); ?>" data-bp-item-component="groups">
<div class="list-wrap">
<?php if( !bp_disable_group_cover_image_uploads() ) { ?>
<?php
$group_cover_image_url = bp_attachments_get_attachment( 'url', array(
'object_dir' => 'groups',
'item_id' => bp_get_group_id(),
) );
$default_group_cover = buddyboss_theme_get_option( 'buddyboss_group_cover_default', 'url' );
$group_cover_image_url = $group_cover_image_url ?: $default_group_cover;
?>
<div class="bs-group-cover only-grid-view"><img src="<?php echo $group_cover_image_url; ?>"></div>
<?php } ?>
<?php if ( ! bp_disable_group_avatar_uploads() ) : ?>
<div class="item-avatar">
<?php bp_group_avatar( bp_nouveau_avatar_args() ); ?>
<div class="groups-loop-buttons only-grid-view">
<?php bp_nouveau_groups_loop_buttons(); ?>
</div>
</div>
<?php endif; ?>
<div class="item">
<div class="item-block">
<h2 class="list-title groups-title"><?php bp_group_link(); ?></h2>
<?php if ( bp_nouveau_group_has_meta() ) : ?>
<p class="item-meta group-details only-list-view"><?php bp_nouveau_group_meta(); ?></p>
<p class="item-meta group-details only-grid-view"><?php
$meta = bp_nouveau_get_group_meta();
echo $meta['status']; ?>
</p>
<?php endif; ?>
<p class="last-activity item-meta">
<?php
printf(
/* translators: %s = last activity timestamp (e.g. "active 1 hour ago") */
__( 'active %s', 'buddyboss-theme' ),
bp_get_group_last_active()
);
?>
</p>
</div>
<div class="item-desc group-item-desc only-list-view"><?php bp_group_description_excerpt( false , 150 ) ?></div>
<?php bp_nouveau_groups_loop_item(); ?>
<div class="groups-loop-buttons footer-button-wrap"><?php bp_nouveau_groups_loop_buttons(); ?></div>
<div class="group-members-wrap only-grid-view">
<?php echo buddyboss_theme()->buddypress_helper()->group_members( bp_get_group_id(), array( 'member', 'mod', 'admin' ) ); ?>
</div>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php bp_nouveau_pagination( 'bottom' ); ?>
<?php else : ?>
<?php bp_nouveau_user_feedback( 'groups-loop-none' ); ?>
<?php endif; ?>
<?php if ( bp_has_groups( 'group_type=task-force' ) ) : ?>
<?php bp_nouveau_pagination( 'top' ); ?>
<ul id="groups-list-taskForces" class="<?php bp_nouveau_loop_classes(); ?> <?php echo $cover_class; ?>">
<?php
while ( bp_groups() ) :
bp_the_group();
?>
<li <?php bp_group_class( array( 'item-entry' ) ); ?> data-bp-item-id="<?php bp_group_id(); ?>" data-bp-item-component="groups">
<div class="list-wrap">
<?php if( !bp_disable_group_cover_image_uploads() ) { ?>
<?php
$group_cover_image_url = bp_attachments_get_attachment( 'url', array(
'object_dir' => 'groups',
'item_id' => bp_get_group_id(),
) );
$default_group_cover = buddyboss_theme_get_option( 'buddyboss_group_cover_default', 'url' );
$group_cover_image_url = $group_cover_image_url ?: $default_group_cover;
?>
<div class="bs-group-cover only-grid-view"><img src="<?php echo $group_cover_image_url; ?>"></div>
<?php } ?>
<?php if ( ! bp_disable_group_avatar_uploads() ) : ?>
<div class="item-avatar">
<?php bp_group_avatar( bp_nouveau_avatar_args() ); ?>
<div class="groups-loop-buttons only-grid-view">
<?php bp_nouveau_groups_loop_buttons(); ?>
</div>
</div>
<?php endif; ?>
<div class="item">
<div class="item-block">
<h2 class="list-title groups-title"><?php bp_group_link(); ?></h2>
<?php if ( bp_nouveau_group_has_meta() ) : ?>
<p class="item-meta group-details only-list-view"><?php bp_nouveau_group_meta(); ?></p>
<p class="item-meta group-details only-grid-view"><?php
$meta = bp_nouveau_get_group_meta();
echo $meta['status']; ?>
</p>
<?php endif; ?>
<p class="last-activity item-meta">
<?php
printf(
/* translators: %s = last activity timestamp (e.g. "active 1 hour ago") */
__( 'active %s', 'buddyboss-theme' ),
bp_get_group_last_active()
);
?>
</p>
</div>
<div class="item-desc group-item-desc only-list-view"><?php bp_group_description_excerpt( false , 150 ) ?></div>
<?php bp_nouveau_groups_loop_item(); ?>
<div class="groups-loop-buttons footer-button-wrap"><?php bp_nouveau_groups_loop_buttons(); ?></div>
<div class="group-members-wrap only-grid-view">
<?php echo buddyboss_theme()->buddypress_helper()->group_members( bp_get_group_id(), array( 'member', 'mod', 'admin' ) ); ?>
</div>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php bp_nouveau_pagination( 'bottom' ); ?>
<?php else : ?>
<?php bp_nouveau_user_feedback( 'groups-loop-none' ); ?>
<?php endif; ?>
<?php
bp_nouveau_after_loop();

WooCommerce Add to cart not working properly

I am currently customizing my content-single-product.php template, and I need to add the simple_add_to_cart. It shows up fine and the quantity box seems to work, but when I click the add to cart button nothing happens.
Here is my full template code:
<?php
/**
* woocommerce_before_single_product hook.
*
* #hooked wc_print_notices - 10
*/
do_action( 'woocommerce_before_single_product' );
if ( post_password_required() ) {
echo get_the_password_form();
return;
}
?>
<div class="product_wrap">
<div class="product_image_wrap">
<div class="thumbnails navigator-2">
<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
//echo Image instead of URL
echo '<div class="slider_thumb">' . wp_get_attachment_image($attachment_id, 'thumbnail') . '</div>';
}
?>
</div>
<div class="main_image">
<div class="product-slider prod-nav">
<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
//echo Image instead of URL
echo '<div class="prod_img">' . wp_get_attachment_image($attachment_id, 'full') . '</div>';
}
?>
</div>
</div>
</div>
<div class="product_content_wrap">
<div itemscope itemtype="<?php echo woocommerce_get_product_schema(); ?>" id="product-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="summary entry-summary">
<h3><?php echo get_post_meta( $post->ID, 'comic_series', true ); ?></h3>
<h1><?php woocommerce_template_single_title(); ?></h1>
<h2><?php echo get_post_meta( $post->ID, 'author_info', true ); ?></h2>
<div class="product_description">
<?php woocommerce_product_description_tab(); ?>
</div>
<div class="add_to_cart">
<?php woocommerce_simple_add_to_cart(); ?>
</div>
</div><!-- .summary -->
</div>
</div>
</div>
<div class="product_details_wrap">
<div class="product_details">
<div class="details_content">
<h4>Details</h4>
<ul>
<li><p><span>Published:</span> <?php echo get_post_meta( $post->ID, 'published', true ); ?></p></li>
<li><p><span>Writer:</span> <?php echo get_post_meta( $post->ID, 'writer', true ); ?></p></li>
<li><p><span>Penciller:</span> <?php echo get_post_meta( $post->ID, 'penciller', true ); ?></p></li>
<li><p><span>Cover Artist:</span> <?php echo get_post_meta( $post->ID, 'cover_artist', true ); ?></p></li>
</ul>
<ul>
<li><p><span>Format:</span> <?php echo get_post_meta( $post->ID, 'format', true ); ?></p></li>
<li><p><span>Price:</span> <?php echo $product->get_price(); ?></p></li>
<li><p><span>UPC:</span> <?php echo $product->get_sku(); ?></p></li>
<li><p><span>FOC Date:</span> <?php echo get_post_meta( $post->ID, 'foc_date', true ); ?></p></li>
</ul>
</div>
</div>
</div>
Obviously I am loading a few custom fields for the product, but it is a simple product. I am owndering what I may be missing here...
As it turns out its a conflict with Advanced Custom Fields! I am going to research this a little more, and possibly post a new question. Pretty crazy!

Loop the index plug-in jeckpack post view

I'm trying to get my index of the loop in order list for viewing. I'm using Jetpack Post Views for this
Ex: the most viewed post is first and so on.
I use a custom post with namo movies , only queensize my loop is not bringing this custom post.
Could anyone help me ?
<div id="content">
<?php if( function_exists( 'stats_get_csv' ) ) :
$top_posts = stats_get_csv( 'postviews', array( 'days' => 21, 'limit' => -1 ) );
$top_ids = array();
foreach ( $top_posts as $top_post ) {
$top_ids[] = $top_post['post_id'];
}
// Check for transient, hat tip Greg Rickaby
if ( false === ( $tops = get_transient( 'popular_posts' ) ) ) :
$top_args = new WP_Query( array(
'posts_per_page' => '5',
'post__in' => $top_ids,
'posts_per_page' => 14,
'post_type' => array('movies')
)
);
$tops = new WP_Query( $top_args );
// Store transient and expire after 24 hours
set_transient( 'popular_posts', $tops, 24 * HOUR_IN_SECONDS );
endif;
while ($tops->have_posts()) : $tops->the_post(); ?>
<div class="box " id="post-<?php the_ID(); ?>">
<div class="boxim">
<?php
if ( has_post_thumbnail() ) { ?>
<img class="boximg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=270&w=180&zc=1" alt=""/>
<?php } else { ?>
<img class="boximg" src="<?php bloginfo('template_directory'); ?>/images/dummy.png" alt="" />
<?php } ?>
<div class="scorebox">
<span class="sholder"> <span class="scorebar score-<?php $rscore=get_post_meta($post->ID, 'wtf_rscore', true); echo $rscore; ?>"> </span> </span>
</div>
</div>
<div class="boxentry">
<div class="btitle">
<h2><?php the_title(); ?></h2>
</div>
<div class="bmeta">
<span class="mgenre"><?php echo get_the_term_list( $post->ID, 'movie-genre', '', ', ', '' ); ?></span> <span class="mcom"><?php comments_popup_link('0', '1', '%'); ?></span>
</div>
<div class="clear"></div>
</div>
</div>
<?php if(++$counter % 3 == 0) : ?>
<div class="clear"></div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php endif; ?>
</div>

Add pagination to this Wordpress Home

I Guys! I'm working on a pagination for this theme homepage. The original code is:
<?php
/**
Blog
**/
$display_count = get_theme_mod('wpex_home_blog_count', '30');
$wpex_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => $display_count,
'no_found_rows' => true,
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<?php
// Display post meta details
wpex_post_meta() ;?>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
What I did is, I changed it to
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('showposts=4' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<ul class="post-meta clr">
<li class="meta-date">
<?php _e('Posted on','wpex'); ?>
<span class="meta-date-text"><?php echo get_the_date(); ?></span>
</li>
</ul>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endwhile; ?>
</div>
<?php wp_pagenavi();?>
<?php wp_reset_postdata(); ?>
</article>
This seems to work on the first page, but when i go to page 2, it gives an copy after copy after copy of the whole page, like an infinate page one. That's not how it should work.
Any ideas what goes wrong here? Does this make any sense to you?
EDIT:
I tried the following. Right now the first page looks good, but when i press the second page on the pagination, it outputs the same as the first.
<?php
$args = array(
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1),
'posts_per_page' => 4
);
query_posts($args);
?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<ul class="post-meta clr">
<li class="meta-date"><?php _e('Posted on','wpex'); ?> <span class="meta-date-text"><?php echo get_the_date(); ?></span> </li>
</ul>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endwhile; ?>
<?php wp_pagenavi();?>
</div><!-- #homepage-portfolio -->
<?php endif; ?>
<?php wp_reset_query(); ?>
</article><!-- #post -->
<?php endwhile; ?>
Whole original code:
<div id="primary" class="content-area clr">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article class="homepage-wrap clr">
<?php
/**
Post Content
**/ ?>
<?php if ( get_the_content() !== '' ) { ?>
<div id="homepage-content" class="entry clr">
<?php the_content(); ?>
</div><!-- .entry-content -->
<?php } ?>
<?php
/**
Features
**/
$wpex_query = new WP_Query(
array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'features',
'posts_per_page' => '-1',
'no_found_rows' => true,
)
);
if ( $wpex_query->posts ) { ?>
<div id="homepage-features" class="clr">
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-features', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-features -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
/**
Portfolio
**/
$display_count = get_theme_mod('wpex_home_portfolio_count', '8');
$wpex_query = new WP_Query(
array(
'post_type' => 'portfolio',
'posts_per_page' => $display_count,
'no_found_rows' => true,
'tax_query' => wpex_home_portfolio_taxonomy(),
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-portfolio" class="clr">
<h2 class="heading"><span><?php _e( 'Recent Work', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<?php get_template_part( 'content-portfolio', get_post_format() ); ?>
<?php if ( $wpex_count == '4' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php
/**
Blog
**/
$display_count = get_theme_mod('wpex_home_blog_count', '30');
$wpex_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => $display_count,
'no_found_rows' => true,
)
);
if ( $wpex_query->posts && '0' != $display_count ) { ?>
<div id="homepage-blog" class="clr">
<h2 class="heading"><span><?php _e( 'From The Blog', 'wpex' ); ?></span></h2>
<?php $wpex_count=0; ?>
<?php foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<?php $wpex_count++; ?>
<article class="recent-blog-entry clr col span_1_of_3 col-<?php echo $wpex_count; ?>">
<?php
// Display post thumbnail
if ( has_post_thumbnail() ) { ?>
<div class="recent-blog-entry-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>">
<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
</a>
</div><!-- .recent-blog-entry-thumbnail -->
<?php } ?>
<header>
<h3 class="recent-blog-entry-title"><?php the_title(); ?></h3>
<?php
// Display post meta details
wpex_post_meta() ;?>
</header>
<div class="recent-blog-entry-content entry clr">
<?php wpex_excerpt( 18, false ); ?>
</div><!-- .recent-blog-entry-content -->
</article><!-- .recent-blog -->
<?php if ( $wpex_count == '3' ) { ?>
<?php $wpex_count=0; ?>
<?php } ?>
<?php endforeach; ?>
</div><!-- #homepage-portfolio -->
<?php } ?>
<?php wp_reset_postdata(); ?>
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
Try declaring $paged outside, and call this in the query_posts
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 4
);
query_posts($args . '&paged=' . $paged);
I might have found an answer for this myself, apparently it's different when you use pagination on the frontpage. I used this and it seems to be working!:
if( is_front_page() ){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
} else {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

the_content(); not showing in images.php

I am having problem getting the_content(); on image.php or attachment.php, the_content(); it's showing in single.php but no in imaage.php, I tried to edit the code but I got nothing,
<div class="content section-inner">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="posts">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="content-inner">
<div class="featured-media">
<?php $imageArray = wp_get_attachment_image_src($post->ID, 'full', false); ?>
<a href="<?php echo esc_url( $imageArray[0] ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment">
<?php echo wp_get_attachment_image( $post->ID, 'post-image' ); ?></a>
</div> <!-- /featured-media -->
<div class="post-header">
<h2 class="post-title"><?php echo basename(get_attached_file( $post->ID )); ?></h2>
<div class="post-meta">
<span><?php _e('Uploaded', 'lingonberry'); echo ' '; the_time(get_option('date_format')); ?></span>
<span class="date-sep">/</span>
<span><?php _e('Width:', 'lingonberry'); echo ' ' . $imageArray[1] . ' px'; // 1 is the width ?></span>
<span class="date-sep">/</span>
<span><?php _e('Height:', 'lingonberry'); echo ' ' . $imageArray[2] . ' px'; // 2 is the height ?></span>
</div>
</div> <!-- /post-header -->
<?php if ( ! empty( $post->post_excerpt ) ) : ?>
<div class="post-content">
<?php the_excerpt(); ?>
</div> <!-- /post-content -->
<?php endif; ?>
</div> <!-- /content-inner -->
<div class="post-nav">
<?php
/**
* Grab the IDs of all the image attachments in a gallery so we can get the URL of the next adjacent image in a gallery,
* or the first image (if we're looking at the last image in a gallery), or, in a gallery of one, just the link to that image file
*/
$attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
foreach ( $attachments as $k => $attachment ) :
if ( $attachment->ID == $post->ID )
break;
endforeach;
$l = $k - 1;
$k++;
if ( isset( $attachments[ $k ] ) ) :
// get the URL of the next image attachment
$next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );
$prev_attachment_url = get_attachment_link( $attachments[ $l ]->ID );
else :
// or get the URL of the first image attachment
$next_attachment_url = get_attachment_link( $attachments[ 0 ]->ID );
endif;
?>
<?php _e('« Previous<span> attachment</span>', 'lingonberry'); ?>
<?php _e('Next<span> attachment</span> »', 'lingonberry'); ?>
<div class="clear"></div>
</div> <!-- /post-nav -->
<?php comments_template( '', true ); ?>
<?php endwhile; else: ?>
<p><?php _e("We couldn't find any posts that matched your query. Please try again.", "lingonberry"); ?></p>
<?php endif; ?>
</div> <!-- /post -->
</div> <!-- /posts -->
I need help on this
Your code is bit of a mystery but if it looks like this
<?php if ( ! empty( $post->post_excerpt ) ) : ?>
<div class="post-content">
<?php the_excerpt(); ?>
<?php the_content(); ?>
</div> <!-- /post-content -->
<?php endif; ?>
the commands the_excerpt() and the_content() will only execute when there is an excerpt present. The if-statement says "if excerpt is not empty then execute the following code.
Try this:
<div class="post-content">
<?php if ( ! empty( $post->post_excerpt ) ) : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php the_content(); ?>
</div> <!-- /post-content -->
There is a shorter way of writing this but this way it shows the change better.

Categories