How to add feed ads in WooCommerce - php

I try to add feed ads in product thumbnails of my website
There is no error and website is running fine. But No ads has been comeup in my website. How to achieve this in feed ads in WooCommerce product thumbnail.
I have edited my woocommerce-teplate.php as
if ( ! function_exists( 'woocommerce_content' ) ) {
/**
* Output WooCommerce content.
*
* This function is only used in the optional 'woocommerce.php' template.
* which people can add to their themes to add basic woocommerce support.
* without hooks or modifying core templates.
*/
function woocommerce_content() {
$i=1;
if ( is_singular( 'product' ) ) {
while ( have_posts() ) :
the_post();
if($i==5||$i==7||$i==10){?>
#my adsense code
<?php }
wc_get_template_part( 'content', 'single-product' );
$i++;
endwhile;
} else {
?>
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
<?php do_action( 'woocommerce_archive_description' ); ?>
<?php if ( woocommerce_product_loop() ) : ?>
<?php do_action( 'woocommerce_before_shop_loop' ); ?>
<?php woocommerce_product_loop_start(); ?>
<?php if ( wc_get_loop_prop( 'total' ) ) : ?>
<?php $i++; while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if($i==5||$i==7||$i==10){?>
#my adsense code
<?php }?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php $i++;?>
<?php endwhile; ?>

Related

Insert highlighted products div in middle of Woocommerce loop

I want to insert a featured product area in the middle of the normal woocommerce loop.
The code for the woocommerce loop is
<?php
if ( woocommerce_product_loop() ) {
do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
} else {
do_action( 'woocommerce_no_products_found' );
}
do_action( 'woocommerce_after_main_content' );
?>
The shortcode I want to insert after 8 products is
<div class="cart-upsell hide-on-desktop clear">
<h3 class="centered">You May Also Like...</h3>
<?php echo do_shortcode('[products limit="2" columns="2" orderby="rand" order="DESC"]')?>
</div>
Currently this is shown at the end of the loop but think it will work better in the middle - only displaying on mobile devices.
So how can I get this into the middle of the loop?
This should work:
<?php
if ( woocommerce_product_loop() ) {
do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
$i = 1;
while ( have_posts() ) {
the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
if(8 === $i){
?>
<div class="cart-upsell hide-on-desktop clear">
<h3 class="centered">You May Also Like...</h3>
<?php echo do_shortcode('[products limit="2" columns="2" orderby="rand" order="DESC"]')?>
</div>
<?php
}
$i++;
}
}
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
} else {
do_action( 'woocommerce_no_products_found' );
}
do_action( 'woocommerce_after_main_content' );
?>

wp-paginate() showing blank page while navigating to other pages

I am using wp-paginate plugin for pagination on my wordpress site. The pagination links are showing fine. The problem is every page shows a blank page.
I am using following code to call pagination. I am new to wordpress so sorry for any mistakes..
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php //twentytwelve_content_nav( 'nav-below' ); ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentytwelve_content_nav( 'nav-below' );
}
?>
You have stared if block but not close it. Enable errors you will get error in php
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php //twentytwelve_content_nav( 'nav-below' ); ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentytwelve_content_nav( 'nav-below' );
}
endif; //<--------------add end if
?>
Finally I found the solution. There was a page named page.php on my server's root folder. I deleted it and problem solved.

I just need to break a loop on a specific page (wordpress/woocommerce)

Would anyone care to help me disable the loop on a WooCommerce (Wordpress plugin) shop page?
I've used shortcodes to fill the page with the product categories that I need, so now I need to stop WooCommerce from displaying anything underneath my content (WooCommerce is loading the products from the loop but I don't want it to).
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
^ Somewhere in there I need to implement if(is_shop()) to display nothing on the /shop page, and then elseif to display the above code so that the subcategory pages and product archives don't break.
I would be very grateful for any help...
You need to check that you're not on a shop page using is_shop(). See the WooCommerce Docs on Conditional Tags for more info.
<?php while ( have_posts() && !is_shop() ) : the_post(); ?>
I didn't completely understand whether or not you're okay with completely getting rid of the posted code on the shop page...but if you are, you can wrap the entire thing in a conditional:
<?php if ( !is_shop() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>
How to remove the sort order, product/category loop, and pagination from the shop page, so as to implement your own content using only shortcodes via the page editor.
product-archive.php:
<?php if ( !is_shop() ) : ?>
<?php
/**
* woocommerce_before_shop_loop hook
*
* #hooked woocommerce_result_count - 20
* #hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php
/**
* woocommerce_after_shop_loop hook
*
* #hooked woocommerce_pagination - 10
*/
do_action( 'woocommerce_after_shop_loop' );
?>
<?php endif; ?>

Query post for posts from the beginning

I don't know why but when I want to display the posts with comment opened, it shows posts which are among the last 10 posts (depend how much is in READING menu), so posts which are in the first page.
My code :
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php
$PostID = get_the_ID();
if (comments_open( $post_id )) { ?>
<?php p2_load_entry(); ?>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
This is my file :
<?php
/**
* Static page template.
Template Name: test
*
* #package P2
*/
?>
<?php get_header(); ?>
<ul id="postlist">
<?php
query_posts( $args );
while ( have_posts() ) : the_post(); ?>
<?php
$PostID = get_the_ID();
if (comments_open( $post_id )) { ?><?php p2_load_entry(); ?>
<?php } ?>
<?php endwhile;
wp_reset_query();
?>
</ul>
<?php get_footer(); ?>

How to import the Wordpress publications into the static page?

I am using in my Wordpress-based site a specific template that has no options display publications, but only static pages. How can I import it to my latest publication?
You can get the latest posts by entering "the Loop" enviroment, the code below is from the Twenty Eleven: Main Index Template by Wordpress
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
you may not want to use twentyeleven_content_nav( 'nav-below' ); nor twentyeleven_content_nav( 'nav-below' ); while using other template (say not based on Twenty Eleven)

Categories