how to know when the user is in specific part of the website and when he leaves that part and display the data in the admin dashboard

I want to display for the admin when a user visits a page and when he leaves that page

the page is for example test.test/my-dashboard. and it’s only available for the logged in users. the admin knows to know the time when the user is in that page and the time the user leaves that page.

thanks in advance and excuse my language.

How do I make my terms for each product display via foreach loop? (woocommerce)

This is my current loop to display my products via woocommerce. When I do print_r($ category_array); It returns the array, but when I try to use a function to call it so I can do what I want to with the data, it makes and my entire screen doesn’t display after the loop. Maybe it’s a mistake in my function? Still very new to woocommerce and wp_loops. Thank you

            <?php             // WP_Query arguments             $  args = array(                 'p'                      => 'product',                 'post_type'              => array( 'product' ),                 'order'                  => 'ASC',                 'post_per_page' => 20,             );              // The Query             $  query = new WP_Query( $  args );              // The Loop             if ( $  query->have_posts() ) {                 while ( $  query->have_posts() ) {                     $  query->the_post();                     function filter_categories($  categories) {                             foreach ($  categories as $  category) {                                 echo $  category->name;                             }                     }                       ?>                     <div class="row">                         <div class="col-2">                             <?php echo the_post_thumbnail(get_the_ID(), 'thumbnail'); ?>                         </div>                         <div class="col-7">                             <a href="<?= get_permalink(); ?>"><?= the_title()?></a>                             <br/>                             <?php                             $  category_array = get_the_terms(get_the_ID(), 'product_cat');                             filter_categories($  category_array);                             ?>                         </div>                         <div class="col-3 text-right ">Price</div>                     </div>                     <?php                 }             } else {                 // no posts found             }              // Restore original Post Data             wp_reset_postdata();             ?> 

If a product category is then display code in product short description

How to display a specific code in a product short description if it is belong to a specific category, i’ve used below code in function to add the code but it is displaying on all products.

    function herara_filter_short_description( $  desc ){     global $  product; if ( is_single( $  product->id ) )         //$  new_desc = 'SKU: ' . $  product->get_sku();         $  new_desc = '<span class="sku_wrapper">SKU: <span class="sku">'. $  product->get_sku() .'</span></span></BR><span class="material_wrapper">MATERIAL: <span class="material">'. $  product->get_sku() .'</span></span>';         //$  new_desc .= $  desc;     return $  new_desc; } add_filter( 'woocommerce_short_description', 'herara_filter_short_description' ); 

How can I properly loop through an array gotten from my wordpress database and display it to my site

I am trying to display some posts information from my database on my live site. I have done the query correctly because I can view the items on the live site. But I need to take three items to display. The post title, the post content which houses the images, and the post id which links to the posts. I seem to have written it correctly but it does not do anything.

This is the code

<?php      global $  wpdb;                    $  results = $  wpdb->get_results( "SELECT * FROM $  wpdb->posts WHERE `post_type`='post' AND `post_status` = 'publish' LIMIT    20" );    foreach ($  results) {       echo "<div class="card">             <div class="img">              <img src="{post_content}" alt="">             </div>             <div class="info">                <h5>{post_title}</h5>             </div>           </div>";   }    //echo "<pre>";print_r($  results);echo"</pre>";                      ?>  

I am quite new to wordpress and PHP in general.

How to modify the WooCommerce shop page to tweak the display price without affecting the actual price under the hood?

The woocommerce shop I am working on imports product price as "£ per g", this is correct for the calculation. On the shop page my client would like the price to be displayed as "£ per 100g" so I need to multiply the price by 100. In functions.php I have the following snippet which lets me change what is displayed (as you can see, I have already edited the text to display correctly), does anyone provide help on how to translate this import price to display price?

add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );     // Change and return $  price_html variable using the $  price and weight amount     let a = 10     function wb_change_product_html( $  price ) {         $  price_html = '<span class="amount">' . $  price . ' per 100g </span>';   // change weight measurement here              return $  price_html;     }          add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart' );     // Change the cart prices with $  price variable and weight amount     function wb_change_product_price_cart( $  price ) {         $  price = $  price . ' per g'; // change weight measurement here              return $  price;     } 

I’m sure that there’s a quick fix here! I have also added a screenshot of the import columns so that the price change I’m looking for can be seen.

enter image description here

How to display the number articles with the same tag inside specific category

I am trying to display the tags that are inside of a specific category. I have this working properly but now I want to show the count next to each tag showing how many articles are in that tag. i.e


I have something that is very close but the problem is that if I have two tags inside a category let’s say apples and oranges, both will show (2), because there are 2 tags in that category in general. I want it to show how many of each tag, like Apples(1), Oranges(2) Here is my code:

 <?php   query_posts('category_name=Health');    if (have_posts()) : while (have_posts()) : the_post();     if( get_the_tag_list() ){         echo get_the_tag_list(); echo"(";echo $  wp_query->found_posts;echo")"; echo '<br>';     }     endwhile; endif;     wp_reset_query(); ?> 

Many thanks in advance.

Can’t display multiple terms with get_the_terms

I am trying to display the terms (from a custom taxonomy) of a single post, but I can’t display more than one term. When I try to display all the terms using a foreach loop, it doesn’t display anything.

Here is one of my attempts :

<?php   $  terms = get_the_terms($  post->ID, 'auteur');  if ($  terms && !is_wp_error($  terms)) {     foreach($  terms as $  term) {         echo $  term->name ;     } }           ?> 

It doesn’t display anything. But the same code without foreach loop displays (as expected) one term :

<?php   $  terms = get_the_terms($  post->ID, 'auteur');     echo $  term->name ;      ?> 

I imagine that I’m missing an evident mistake (I’m a beginner), but I can’t understand what is wrong with the foreach loop…

Thank you !

issue with if/elseif in_array inside foreach loop display only one post

I try to attribute a specific background-color to each post if one of categories of post corresponding to one category. My function works but is restricted to display only one post in result. If I delete this function, all post are displayed.

More informations : On my page I have buttons to filter posts by category. if I click on once I display all post for this category. But each post contains several categories. I have selected some categories to attribute a background colors for each posts, but theses categories are different of the filters buttons.

WordPress display this error "a critical error has occurred on your site, Learn more about debugging WordPress." after the first post displayed.

here is my entire code :

<?php  add_action('wp_ajax_nopriv_filter', 'filter_ajax'); add_action('wp_ajax_filter','filter_ajax');  function filter_ajax(){  $  category = $  _POST['category'];  $  argsf = array(     'post_type' => 'post',     'posts_per_page' => -1,     'order' => 'ASC' );  if(isset($  category)){     $  argsf['category__in'] = array($  category); }  $  postsf = get_posts($  argsf);      if (!empty($  postsf)) {     foreach ($  postsf as $  post) {                  $  link_post = get_permalink( $  post->ID );         $  image_post = get_the_post_thumbnail_url( $  post->ID, $  size = 'large' );         $  item1 = get_post_meta($  post->ID, 'item1', true);         $  item2 = get_post_meta($  post->ID, 'item2', true);         $  item3 = get_post_meta($  post->ID, 'item3', true);         $  item4 = get_post_meta($  post->ID, 'item4', true);         $  title = get_the_title($  post->ID);          $  post_slug = $  post->post_name;         $  cats_post = wp_get_post_categories( $  post->ID );                  function test($  cats_post){             if (in_array("14", $  cats_post)){ echo'#710000';}             elseif(in_array("5", $  cats_post)){ echo'#0a005d';}             elseif(in_array("16", $  cats_post)){ echo'#65a0e8';}             elseif(in_array("13", $  cats_post)){ echo'#90744b';}         }      ?>   <div class="shop w-24 pb-1" style="height:320px; min-width:320px;">     <div class="w-100 h-100 p-2">                      <div class=" " style="background-color:<?php test($  cats_post); ?>">             <div class="">                 <!--<div class="" title="Locer">                 <a href="<?= $  item2; ?>"><i class="icofont-opposite "></i></a>                 </div>-->                 <div class="" title="something">                     <a href="tel:<?= $  item3; ?>"><i class="im im-phone"></i> </a>                 </div>                 <div class="">                     <span><?= $  item1; ?></span>                 </div>              </div>             <span class="text-uppercase col-white tsh332 fwlr text-center" style="font-size:2rem;">                 <?= $  title; ?>             </span>             <?php if ( metadata_exists( 'post', $  post->ID , 'item4' ) ){ ?>                  <div class="">                     <div class="b-yellow b-100">                         <span><?= $  item4; ?> </span>                     </div>                 </div>             <?php } ?>         </div>                      </div> </div> <?php  }  /* fin foreach */     }  /* fin if  */ wp_reset_postdata(); die(); } 


I tried that but it doesn’t work :

  • get_the_category instead of wp_get_post_categories
  • has_item instead of in_array

I don’t know If I must use an other foreach loop and how !


How to display the post using a shortcode in WordPress?

I am trying to display all the post on my page but it’s not displaying. I am using the below code and I added the shortcode gridPost on my page. I am getting the only UL LI but not getting the title name.

function getAllPost(){     $  postData=[];     $  wpb_all_query = new WP_Query(array('post_type'=>'project', 'post_status'=>'publish', 'posts_per_page'=>-1));  if ( $  wpb_all_query->have_posts() ) :     $  postData[]='<ul>';   while ( $  wpb_all_query->have_posts() ) : $  wpb_all_query->the_post();         $  postData[]='<li><a href="'.the_permalink().'">'.the_title().'</a></li>';     endwhile;     $  postData[]='</ul>';     wp_reset_postdata();     else :      $  postData[] ='<p>Sorry, no posts matched your criteria.</p>';   endif;         $  postData = implode( '', $  postData );     return $  postData; } add_shortcode( 'gridPost', 'getAllPost');