i need to put my $product->get_image in ajax but i don t know how to do
i want to put my $product->get_image(...) to $details = Array("..., "img"=>"$imgL");
here s my code
<?php
define('WP_USE_THEMES', false);
require_once('wp-load.php');
global $qode_options_theme16;
global $wp_query;
$sku=$_POST['id'];
$args = array('post_type' => 'product','meta_value' => $sku);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
$i=0;
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
$imgL=$product->get_image( $size = 'shop_large', $attr = array(), $placeholder = true );
$prix = $product->get_price();
$nom = $product->get_formatted_name();
endwhile;
}
$details = Array("prix"=>"$prix", "nom"=>"$nom", "sku"=>"$sku", "img"=>"$imgL");
echo json_encode($details);
?>
thanks in advance
Please call the code like this:
$imgL=$product->get_image( 'shop_large', array(), true );
Related
I have a fully working AJAX load more button on my Wordpress posts. On click, it loads the next page of posts and appends them directly to the end of the first set.
I now need to create category filters. So when a user clicks a category name, it updates the results below via AJAX to only show posts in that category - BUT I need the load more ajax to continue to work correctly. I've tried hashing together two different AJAX calls, and also tried combining both AJAX into one, but I can't seem to get them talking...
Here's the code:
functions.php
function we_title_filters() {
if(is_home()): ?>
<div class="title-filters">
<h3 class="widget-title" style="display:inline-block;">Latest News</h3>
<div class="filters">
<?php
$include = array(3651, 7, 2828, 2829, 2172);
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'include' => $include
) );
foreach ( $categories as $category ) {
printf( '%2$s',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
?>
<div class="dropdown" style="float:right;">
<span class="dropbtn">More</span>
<ul class="more-list">
<?php
$include = array(3651, 7, 2828, 2829, 2172);
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'include' => $include
) );
foreach ( $categories as $category ) {
printf( '<li>%2$s</li>',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
?>
</ul>
</div>
</div>
</div>
<?php endif;
}
This outputs the filters in a list. On click of one of them, it needs to AJAX change the results, but also keep the load more button working, within that category
/* AJAX load more stuff */
/**
*
* Infinite Scroll
*
* #since 1.0.0
*
* #author Lauren Gray
* #author Bill Erickson
* #link http://www.billerickson.net/infinite-scroll-in-wordpress
*
* Enqueue necessary JS file and localize.
*
*/
add_action( 'wp_enqueue_scripts', 'sn_infinite_scroll_enqueue' );
function sn_infinite_scroll_enqueue() {
if ( ! is_singular() ) {
global $wp_query;
$args = array(
'nonce' => wp_create_nonce( 'be-load-more-nonce' ),
'url' => admin_url( 'admin-ajax.php' ),
'query' => $wp_query->query,
'maxpage' => $wp_query->max_num_pages,
);
wp_enqueue_script( 'sn-load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array( 'jquery' ), '1.0', true );
wp_localize_script( 'sn-load-more', 'beloadmore', $args );
}
}
/**
*
* Infinite Scroll
*
* #since 1.0.0
*
* #author Lauren Gray
* #author Bill Erickson
* #link http://www.billerickson.net/infinite-scroll-in-wordpress
*
* Parse information
*
*/
add_action( 'wp_ajax_sn_infinite_scroll_ajax', 'sn_infinite_scroll_ajax' );
add_action( 'wp_ajax_nopriv_sn_infinite_scroll_ajax', 'sn_infinite_scroll_ajax' );
function sn_infinite_scroll_ajax() {
if ( ! is_singular() ) {
check_ajax_referer( 'be-load-more-nonce', 'nonce' );
$excluded_ids = get_field('top_stories', option);
$args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
$args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
$args['paged'] = esc_attr( $_POST['page'] );
$args['post_status'] = 'publish';
$args['post__not_in'] = $excluded_ids;
$pageType = esc_attr( $_POST['pageType'] );
if ( $pageType == "is-home" ) {
$initial = 30;
$ppp = 30;
$columns = 3;
}
else {
$initial = 30;
$ppp = 30;
$columns = 3;
}
$args['posts_per_page'] = $ppp;
$args['offset'] = $initial + ( $ppp * ( $_POST['page'] ) );
ob_start();
$loop = new WP_Query( $args );
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();
sn_post_summary( $loop->current_post, $columns );
endwhile;
endif;
wp_reset_postdata();
$page = esc_attr( $_POST['page'] );
}
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
}
/**
*
* Infinite Scroll
*
* #since 1.0.0
*
* #author Lauren Gray
* #author Bill Erickson
* #link http://www.billerickson.net/infinite-scroll-in-wordpress
*
* Output articles
*
*/
function sn_post_summary( $count, $columns ) {
// Be able to convert the number of columns to the class name in Genesis
$fractions = array( '', 'half', 'third', 'fourth', 'fifth', 'sixth' );
// Make a note of which column we're in
$column_number = ( $count % $columns ) + 1;
// Add one-* class to make it correct width
$countClasses = sprintf( 'one-' . $fractions[$columns - 1] . ' ', $columns );
// Add a class to the first column, so we're sure of starting a new row with no padding-left
if ( 1 == $column_number )
$countClasses .= 'first ';
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 );
add_action( 'genesis_entry_header', 'genesis_do_post_image', 8 );
echo '<article class="' . $countClasses . implode( ' ', get_post_class() ) . '">'; // add column class
do_action( 'genesis_entry_header' );
echo '</article>';
}
This is a modified version of the infinite scroll loader found here: https://gist.github.com/graylaurenm/86daa4f23aa8749c0933f72133ac7106
I removed the infinite scroll options so it only loads on click of the button.
db_connection.php
<?php
define('_HOST_NAME', 'localhost');
define('_DATABASE_USER_NAME', 'root');
define('_DATABASE_PASSWORD', '');
define('_DATABASE_NAME', 'test_database');
$dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
if ($dbConnection->connect_error) {
trigger_error('Connection Failed: ' . $dbConnection->connect_error, E_USER_ERROR);
}
$_GLOBAL['dbConnection'] = $dbConnection;
?>
function.php
<?php
function categoryParentChildTree($parent = 0, $spacing = '', $category_tree_array = '') {
global $dbConnection;
$parent = $dbConnection->real_escape_string($parent);
if (!is_array($category_tree_array))
$category_tree_array = array();
$sqlCategory = "SELECT id,name,parent_id FROM tbl_categories WHERE parent_id = $parent ORDER BY id ASC";
$resCategory=$dbConnection->query($sqlCategory);
if ($resCategory->num_rows > 0) {
while($rowCategories = $resCategory->fetch_assoc()) {
$category_tree_array[] = array("id" => $rowCategories['id'], "name" => $spacing . $rowCategories['name']);
$category_tree_array = categoryParentChildTree($rowCategories['id'], ' '.$spacing . '- ', $category_tree_array);
}
}
return $category_tree_array;
}
?>
index.php
<?php
require_once 'db_connection.php';
require_once 'functions.php';
$categoryList = categoryParentChildTree();
foreach($categoryList as $key => $value){
echo $value['name'].'<br>';
}
?>
can anyone help me to translating this so i can use on codeigniter? recursive function
sorry im new on codeigniter
please help
I've no idea to pass $data variable from Model to Controller.
In Model:
function active(){
$query = $this->db->query(" SELECT * FROM `event` WHERE DATE_FORMAT( NOW( ) , '%m-%d-%Y' ) BETWEEN START AND END ");
foreach($query->result_array() as $row){
$ev_name = $row['ev_name'];
$image = $row['ev_image'];
$start = $row['start'];
$end = $row['end'];
$desc = $row['ev_dec'];
$ev_id = $row['ev_id'];
}
$data = array('ev_name' => $ev_name,
'ev_image' => $image,
'start' => $start,
'end' => $end,
'ev_desc' =>$desc,
'ev_id'=> $ev_id
);
echo $data;
}
In Controller:
function active_event()
{
$this->load->view('active_event');
$this->load->model('Usermodel', Sdata);
}
You should use return keyword which is used for passing data from one function to another. You also need to load and call model method before loading view so that you can use data from modal in view. I think you need to check MVC pattern and Codeigniter userguide.
In your model,
function active(){
$query = $this->db->query(" SELECT * FROM `event` WHERE DATE_FORMAT( NOW( ) , '%m-%d-%Y' ) BETWEEN START AND END ");
foreach($query->result_array() as $row){
$ev_name = $row['ev_name'];
$image = $row['ev_image'];
$start = $row['start'];
$end = $row['end'];
$desc = $row['ev_dec'];
$ev_id = $row['ev_id'];
}
$data = array('ev_name' => $ev_name,
'ev_image' => $image,
'start' => $start,
'end' => $end,
'ev_desc' =>$desc,
'ev_id'=> $ev_id
);
return $data;
}
In your controller,
function active_event()
{
$this->load->model('YOUR MODEL NAME');
$data = $this->YOURMODELNAME->active();
$this->load->view('active_event', $data);
}
Page 2 displays the same posts as page 1. What could be the problem?
Below is code from index.php It is used to sort posts by simply clicking on a link such as sort by: "price" or "random".
Pagination
<?php
previous_posts_link();
next_posts_link();
?>
Loop
<?php
$sort= $_GET['sort'];
if($sort == "A")
{
$order= "orderby=rand&posts_per_page =2";
}
if($sort == "B")
{
$order= array (
'meta_key'=>'price',
'orderby'=>'meta_value_num',
'order'=>'DESC',
'posts_per_page' => 2
);
}
?>
random
price
<?php $loop = new WP_Query($order); ?><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); the_meta ();?>
<?php endwhile; wp_reset_query();?>
Yourt problem is using a custom query. Why aren't you using the main query. It is totally unnecessary and a waste of resources to run another query here.
As this code is on index.php, which will be your homepage, you can simply make use of the default loop, and use pre_get_posts to alter your main query before it is run. Doing it this way doesn't require extra unnecessary database queries, and it is much cleaner
So, delete the custom query in your index.php, and replace it with the following code. This is all that you are going to need (and don't abuse the php tags :-))
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); the_meta ();
endwhile;
previous_posts_link();
next_posts_link();
?>
Now, in functions.php, add the following
function my_custom_query($query){
if ( $query->is_home() && $query->is_main_query() ) {
$sort= $_GET['sort'];
if($sort == "A"){
$query->set( 'orderby', 'rand' );
$query->set( 'posts_per_page', '2' );
}
if($sort == "B"){
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
$query->set( 'posts_per_page', '2' );
}
}
}
add_action( 'pre_get_posts', 'my_custom_query' );
If you, for some reason, need to run a custom query, remember to add the paged variable to your arguments. You can have a look at these parameters in WP_Query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 'paged' => $paged ) );
This isn't enough though. You need to set the $max_pages parameter as well in next_posts_link( $label , $max_pages ); for pagination to work correctly. You'll need to something like this
next_posts_link( 'Older Entries', $loop->max_num_pages );
If you want to use a custom WP_Query and still want pagination to work, you must pass in the paged argument.
// what page is this? default to 1.
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
// "A" args
$order = "orderby=rand&posts_per_page=2&paged={$paged}";
// "B" args
$order = array (
'meta_key'=>'price',
'orderby'=>'meta_value_num',
'order'=>'DESC',
'posts_per_page' => 2,
'paged' => $paged,
);
Trying to use this code to render a menu module on a custom template
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'menu' );
$attribs = array('style' => 'mainnav');
$module->params = "menutype=" .$mainmenu ."\nshowAllChildren=1";
echo JModuleHelper::renderModule($module, $attribs);
The menu only works if I have another menu module published, so I am sure this only needs a line of code to make it work without having to publish a menu module.
The menu exists, the module for this menu does not exist, I am trying to create it with this code.
Please help.
The code works fine I just had a small correction to make:
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'mod_menu' );
$attribs = array('style' => 'mainnav');
$module->params = "menutype=" .$mainmenu ."\nshowAllChildren=1";
echo JModuleHelper::renderModule($module, $attribs);
on the second line, the call should be to "mod_menu" and not just "menu", and this makes the code to work perfect :)
Why don't just use the include module?
<jdoc:include type="modules" name="mainnav" style="mainnav" />
This will allow you to publish whatever module you wan't in that position.
Otherwise the getModule function works like this:
JModuleHelper::getModule( 'position', 'title' );
According to the Joomla! API so you need to pass both parameters.
i use this code to render other module by id
$mod_id = $params->get('mod_id');
if ($type == 'logout' && $mod_id != ''){
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$db =& JFactory::getDBO();
if ($jVersion=='1.5') {
$query = 'SELECT id, title, module, position, params'
. ' FROM #__modules AS m'
. ' WHERE id='.intval($mod_id);
} else {
$query = 'SELECT id, title, module, position, content, showtitle, params'
. ' FROM #__modules AS m'
. ' WHERE m.id = '.intval($mod_id);
}
$db->setQuery( $query );
if ($mod = $db->loadObject()){
$file = $mod->module;
$custom = substr( $file, 0, 4 ) == 'mod_' ? 0 : 1;
$modu->user = $custom;
// CHECK: custom module name is given by the title field, otherwise it's just 'om' ??
$mod->name = $custom ? $mod->title : substr( $file, 4 );
$mod->style = null;
$mod->position = strtolower($mod->position);
echo $renderer->render($mod, array());
}
}
use this, 100%, render modules at this position.
<?php
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer('modules');
$options = array('style' => 'xhtml');
$position = 'article-banners';
echo $renderer->render($position, $options, null);
?>
$position refer to module position, may be more than one...
$style - none, rounded, xhtml...