Widget cat’ exceptions ?
-
Good Day Everybody,
I am looking for help today.
I am using The “Widgets Options” Plugin in order to show/hide plugins from fthe homepage of my website depending on the device used.
I have a “featured block” on top on the mobile version, wich displays the 4th latest articles. On the mobile version, i have organized said plugins to display – below the “Featured Block” a list of the latests articles through an existing widget provided by the theme creator. The problem i have is the redundancy, as the said widget also shows the latest 4 articles that are already displayd in the Featured block.I would like to implement a condition/exception in said widget, so that it would avoid displaying the 4 latest articles that are already in the Featured Block.
If you have any idea on how to do that, i am more than happy to hear you out !
Thanks
The page I need help with: [log in to see the link]
-
Hello, i hope you are having a great week-end ! Unfortunately the more we dive into that topic, the more i realise i am not skilled enough to answer you despite your help and guidance. I don’t really know how to check if $categories is getting the correct category ID (wich is “all category”, in that case) considering (i believe) we used the same function structure and it was able to display all the categories before. I will look at debugging today and hopefully be able to give you some more informations.
I was thinking on top of my head earlier, rather than trying to identify the 4 articles that are actually labelled with a FEAT tag, could it be less of an hassle to tell the widget something along the lines of “get the latest 12 articles, but hide first 4 ?” no matter the tag, considering all the articles have the same tag and will therefore end up in that featured block. (Yes, it may seem simpler for me to say it like that, not knowing what it would really imply coding wise !)Hello, and i hope you are having a great week-end too
To check if$categoriesis getting the correct category ID (which is “all category” in your case), you can add a simple debugging line in your code. For example, you can useerror_log()to output the value of$categoriesto the debug log. Add this line just before your WP_Query:error_log('Selected Category: ' . $categories);
Ensure that debugging is enabled in yourwp-config.php:define('WP_DEBUG', true);define('WP_DEBUG_LOG', true);
Then, check the debug.log file in thewp-contentdirectory after loading the widget to see the output.Alternative Approach for Displaying Posts
Regarding your idea of fetching the latest 12 articles but hiding the first 4, this is possible. You can modify your WP_Query to fetch the latest 12 posts and then use a counter in your loop to skip the first 4 posts. Here’s a simplified example:$recent = new WP_Query(array( 'posts_per_page' => 12, // Add other necessary arguments));$counter = 0;if ($recent->have_posts()) {while ($recent->have_posts()) {$recent->the_post();$counter++;if ($counter <= 4) continue; // Skip the first 4 posts// Your code to display the post} }wp_reset_postdata();
This approach will fetch the latest 12 posts but only display posts 5 to 12 in your widget.
I hope this helps! If you encounter any more issues or have further questions, feel free to ask.Hello WP Provider,
My week-end is going well !
I am not certain this is what we were looking for, but my log shows “PHP Warning: Undefined array key “categories” in C:\Users\kevin\Local Sites\test-mobile\app\public\wp-content\themes\flex-mag\widgets\widget-catrow.php on line 128″
I actually tried the second option. The good news is with this method, the plugins doesnt disappear from the pannel and it shows something on the screen. But it selects a random (1) article and only one.
Have a great satursday night with your close ones !Hello,
I’m glad to hear that you’ve tried the debugging and the alternative approach for displaying posts. Let’s address the issues you’re encountering:
- PHP Warning about Undefined Array Key “categories”: This warning suggests that the
$categoriesvariable is not being correctly set or passed to your widget. To resolve this, ensure that the$categoriesvariable is properly assigned from the widget’s instance settings. This should happen in thewidgetfunction of yourmvp_catrow_widgetclass. Here is a revised snippet:
function widget( $args, $instance ) { extract( $args ); global $post;// Assign categories from the widget instance $categories = $instance['categories'] ?? 'all';// Fallback to 'all' if not set// ... rest of your code ...}This change ensures that
$categorieswill have a default value if it’s not set in the widget’s settings.Displaying a Single Random Article: The issue where it’s showing only one random article instead of the desired number of posts is likely due to the query setup. To display the latest 12 articles, excluding the first 4, you should ensure that your
WP_Queryis set up correctly. Here’s a revised version of the query and loop:$recent = new WP_Query(array( 'posts_per_page' => 12,// Add other necessary arguments like 'orderby' => 'date'));$counter = 0;if ($recent->have_posts()) {while ($recent->have_posts()) {$recent->the_post();$counter++;if ($counter <= 4) continue;// Skip the first 4 posts// Your code to display the post// ...} }wp_reset_postdata();Make sure that this loop is within your widget’s
widgetfunction and that it is correctly iterating over the posts.Try implementing these changes and see if they resolve your issues. If you still encounter problems or have more questions, feel free to ask. Have a great evening too!
- PHP Warning about Undefined Array Key “categories”: This warning suggests that the
The topic ‘Widget cat’ exceptions ?’ is closed to new replies.