Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Sidebars & Widgets (http://forum.bytesforall.com/forumdisplay.php?f=14)
-   -   Right and left Sidebar behave different with same PP (http://forum.bytesforall.com/showthread.php?t=23945)

dadadesign Feb 7, 2017 12:24 PM

Right and left Sidebar behave different with same PP
 
Hi everyone,

I am desperetly seeking for help. I am using this PHP code in PHP Code Widget:
Code:


<?php
// Retrieve consistent random set of posts with pagination function mam_posts_query($query) {
  global $mam_posts_query;
  if ($mam_posts_query && strpos($query, 'ORDER BY RAND()') !== false) {
      $query = str_replace('ORDER BY RAND()',$mam_posts_query,$query);
      //print_r("<p>$query</p>");
  }
  return $query;
}
add_filter('query','mam_posts_query');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$seed = date('Ymdh');  // Use date('Ymdh') to get an hourly change
global $mam_posts_query;
$mam_posts_query = " ORDER BY rand($seed) "; // Turn on filter
$args = array(
  'caller_get_posts' => 1,  // Stickies will be repeated if this is not set
  'orderby' => 'rand',      // This MUST be in the query - the filter checks for it
  'paged' => $paged,
  'category_name'  => 'anwaelte',
        'posts_per_page' => 4,
                'year'    => 2017
);
query_posts($args);
$mam_posts_query = ''; // Turn off filter
// echo "<br />Seed is $seed, Paged is $paged";
if (have_posts()) {
  while (have_posts()) {
      the_post();
      echo '<br /><a href="' . get_the_permalink(). '">'
 . get_the_title() . '</a>'
 ;
  }
}  ?>

If I put the widget in the right sidebar, it shows 4 post titles linked to their permalinks and the content of the actual Post in the centre column - as expected. Unfortunately I have to put the widget in the left sidebar. There it Shows the same entries, BUT in the Centre Column I see the excerpts of the four posts, not the content of the actual post as it should be.

How can I manage that the left Sidebar shows the same behaviour as the right Sidebar?

Every hint is gladly appreciated!

juggledad Feb 7, 2017 12:51 PM

Your code throws an error. In the beginning:
HTML Code:

// Retrieve consistent random set of posts with pagination function mam_posts_query($query) {
  global $mam_posts_query;
  if ($mam_posts_query && strpos($query, 'ORDER BY RAND()') !== false) {
      $query = str_replace('ORDER BY RAND()',$mam_posts_query,$query);
      //print_r("<p>$query</p>");
  }
  return $query;
}  <==============this is unmatched.

you have an unmatched '}'

dadadesign Feb 8, 2017 12:57 AM

Thank You very much for your quick response, Juggledad!

I did copy and paste with an iPad, with which I am not very familiar, sSorry about that. Here is the correct code:

Code:


<?php
 
// Retrieve consistent random set of posts with pagination
function mam_posts_query($query) {
  global $mam_posts_query;
  if ($mam_posts_query && strpos($query, 'ORDER BY RAND()') !== false) {
      $query = str_replace('ORDER BY RAND()',$mam_posts_query,$query);
      //print_r("<p>$query</p>");
  }
  return $query;
}
add_filter('query','mam_posts_query');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$seed = date('Ymdh');  // Use date('Ymdh') to get an hourly change
global $mam_posts_query;
$mam_posts_query = " ORDER BY rand($seed) "; // Turn on filter
$args = array(
  'caller_get_posts' => 1,  // Stickies will be repeated if this is not set
  'orderby' => 'rand',      // This MUST be in the query - the filter checks for it
  'paged' => $paged,
  'category_name'  => 'anwaelte',
        'posts_per_page' => 4,
                'year'    => 2017
);
query_posts($args);
$mam_posts_query = ''; // Turn off filter
// echo "<br />Seed is $seed, Paged is $paged";
if (have_posts()) {
  while (have_posts()) {
      the_post();
      echo '<br /><a href="' . get_the_permalink(). '">'
 . get_the_title() . '</a>'
 ;
  }
}  ?>

I am still seeking for a solution, in which placing the PHP widget on the left sidebar leads to same results as placing it on the right.

juggledad Feb 8, 2017 04:29 AM

What's happening is you are messing with the default query - not a good idea.

In simple terms:
- with widget in left sidebar
-- Page starts to load
-- WP has a default query it is going to use
-- the header, left and left inner sidebar code run
-- your code sets information for 'the loop' and runs the query
-- sidebar finished and center colum gets built
-- loop runs again using data setup by your query
-- right-inner and right sidebar code run
-- footer code run

- with loop in right sidebar
-- Page starts to load
-- WP has a default query it is going to use
-- the header, left and left inner sidebar code run
-- sidebar finished and center colum gets built
-- loop runs using default query options
-- right-inner and right sidebar code run
-- your code sets information for 'the loop' and runs the query with your options
-- footer code run

You need to look at how to create a new query in your code

dadadesign Feb 8, 2017 11:32 AM

Thank you again, Juggledad!
Your detailed list of the different processes is enlightening. I will try to follow your advise to create a new query.


All times are GMT -6. The time now is 06:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.