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 !
ThanksThe page I need help with: [log in to see the link]
-
You could tweak the widget’s settings so it doesn’t show those articles already featured in the block. This means diving into a bit of code, though. Your theme’s widget probably uses something called WP_Query to fetch those latest articles. You’d need to adjust this query to skip over the articles already shown in your Featured Block.
The idea is to tell the widget, “Hey, ignore these four articles; they’re already being shown up top.” To do this, you’ll need the IDs of the articles in the Featured Block. Once you have those, you can add a line to the widget’s code that basically says, “Exclude these IDs.”
Here’s a rough idea of what that might look like in code. It’s pretty basic and would need to be adjusted to fit your specific setup:
// Imagine these are the IDs of your featured articles $featured_ids = [1, 2, 3, 4]; $args = [ 'post_type' => 'post', 'posts_per_page' => 6, // however many you want to show 'post__not_in' => $featured_ids, // this is the key part ]; $query = new WP_Query($args);
Good Day WP Provider, thank you so much for stopping by and helping me as i wasn’t really sure where to start from.
I’ve carefully read your answer and i have 1 question:
“To do this, you’ll need the IDs of the articles in the Featured Block”. As we post about 1K+ articles a year, i am guessing the article ID we have to exclude will change everytime we post a new article ? Every article we post goes featured first.I am trying to work around this, if i understand correctly.
<?php $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6 )); while($recent->have_posts()) : $recent->the_post(); ?>
Have a good day.
Kevin-
This reply was modified 1 year, 3 months ago by
spojetski.
Good Day,
You’re correct in your understanding that the article IDs to exclude in your widget will change each time a new article is posted, especially since every new article initially goes into the Featured Block. To automate this process, you need a dynamic way of determining which articles are currently in the Featured Block and exclude them from your WP_Query in the widget.
Since you post a large number of articles, manually updating the IDs isn’t feasible. Instead, you can modify your query to automatically fetch the latest articles and exclude them from your widget. Here’s how you can approach this:
- Fetch Latest Featured Articles: First, you’ll need to determine which articles are currently featured. This can usually be done based on categories, tags, or a custom field that marks an article as featured.
- Dynamic Exclusion: Modify your widget’s WP_Query to exclude the latest featured articles. You can do this by fetching the IDs of these articles and using the
'post__not_in'
parameter in your WP_Query, just like you’ve outlined.
Here’s an example of how you might modify your existing WP_Query:
<?php
// Fetch IDs of latest featured articles $featured_posts = new WP_Query([
'post_type' => 'post',
// Define how you determine what's featured, e.g., category or custom field
'category_name' => 'featured',
// or use a custom field or tag
'posts_per_page' => 4,
// assuming you have 4 featured posts
]);
$featured_ids = [];
if ($featured_posts->have_posts()) {
while ($featured_posts->have_posts()) { $featured_posts->the_post();
$featured_ids[] = get_the_ID();
} }
wp_reset_postdata();
// Now, create your original query, excluding featured posts
$recent = new WP_Query([
'cat' => $categories,
'posts_per_page' => 6,
'post__not_in' => $featured_ids,
// exclude featured posts
]);
while ($recent->have_posts()) : $recent->the_post();
// Your loop content here
endwhile; ?>
This code first gets the IDs of your latest featured posts and then uses those IDs to exclude those posts from the subsequent query in your widget.
Remember, the specifics of this code might need to be adjusted based on how your featured articles are determined (like specific categories, tags, or custom fields). Also, ensure to test any code changes in a staging environment before applying them to your live site to prevent disruptions.
Have a great day!
It always amaze me to see how some people are confortable with this while i’ve been struggling to keep up and improve my website for the last 7 years with my basic php/html/sql knowledge !
This may be above my actual skills and understanding but i’m willing to give it a go and understand it to make it work; otherwise i might have to get somebody !
“you’ll need to determine which articles are currently featured. This can usually be done based on categories, tags, or a custom field that marks an article as featured.” Just to make sure i understand this right – > All my articles ever created have gone through the “featured block” at some points, using the TAG: FEAT. You are telling me i should first let my website figure out that the last 4 articles with the Tag (i.e: ‘Category name’, in the code you kindly gave me shoud then be replace with tag name), “FEAT” are to be considered as non-existent for the CATEGORY ROW WIDGET we are working on ?]); $featured_ids = []; if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } }
I am trying to figure what this bit is doing. Thru these conditions, are we saying: “get the 4 (most recent ?) posts where category is “Featured”. If you find posts within the “Featured” Category, get their ID’s. Then, display the 6 (most recents ?) posts in the CATEGORY ROW WIDGET that have not been listed by their ID’s above.” ?
Once again, thank you for providing some help, fits your nickname !
-
This reply was modified 1 year, 3 months ago by
spojetski.
Hello,
I’m glad to hear you’re willing to tackle this challenge! Let’s break down the code and its purpose to make it clearer:
- Identifying Featured Articles: Yes, you’ve understood it correctly. The code is designed to identify the last four articles tagged as “FEAT”. In your case, since you use tags to mark featured articles, you’ll replace
'category_name' => 'featured'
with the appropriate tag query, like'tag' => 'FEAT'
. This part of the code fetches the latest articles with this tag. - The Purpose of
$featured_ids = [];
and the Following Loop:$featured_ids = [];
is initializing an empty array. This array will store the IDs of the featured articles.- The
if
condition and thewhile
loop work together to fill this array. Here’s a breakdown:- The
if
statement checks if there are any posts in the$featured_posts
query. - The
while
loop runs as long as there are posts to process in the$featured_posts
query. - Inside the loop,
the_post()
sets up the post data for each featured article. get_the_ID()
fetches the ID of the current article in the loop, and this ID is added to the$featured_ids
array.
- The
- Excluding Featured Articles from the Category Row Widget:
- The second part of the code (
$recent = new WP_Query([...]);
) is creating a new query for your widget. - The key part is
'post__not_in' => $featured_ids
, which tells WordPress to exclude the posts whose IDs are in the$featured_ids
array from this new query. - The assumption here is that you want to display the 6 most recent posts in your widget, excluding the ones that are currently marked as featured.
- The second part of the code (
In simpler terms, this code first figures out the latest 4 posts that are tagged as featured. Then, it fetches the next 6 recent posts for your widget, making sure that these 6 posts do not include the 4 featured ones.
Remember, you might need to adjust the numbers (like ‘posts_per_page’) based on how many articles you want to fetch and display in different parts of your site.
Finally, if you find the code challenging, there’s no harm in seeking assistance from a professional. WordPress development can be complex, and it’s okay to ask for help when needed.
I hope this explanation helps you understand the process better! Let me know if you have more questions.
Thank you very much. I have a local copy of my website here so i can try and sort out. I have understand some of this will need to be tweaked and adjusted for us, but i am quite confused as to what exactly this will be replacing considering i’ll endup with multiple endwhile & reset_postdata in my widget-catrow.php.
Below is whole section to hopefully make this poor explanation sort of better !
<div class="row-widget-wrap left relative"> <ul class="row-widget-list"> <?php $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6 )); while($recent->have_posts()) : $recent->the_post(); ?> <li> <a href="<?php the_permalink(); ?>" rel="bookmark"> <?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?> <div class="row-widget-img left relative"> <?php the_post_thumbnail('mvp-mid-thumb', array( 'class' => 'reg-img' )); ?> <?php the_post_thumbnail('mvp-small-thumb', array( 'class' => 'mob-img' )); ?> <?php $post_views = get_post_meta($post->ID, "post_views_count", true); if ( $post_views >= 1) { ?> <div class="feat-info-wrap"> <div class="feat-info-views"> <i class="fa fa-eye fa-2"></i> <span class="feat-info-text"><?php mvp_post_views(); ?></span> </div><!--feat-info-views--> <?php $disqus_id = get_option('mvp_disqus_id'); if ( ! $disqus_id ) { if (get_comments_number()==0) { } else { ?> <div class="feat-info-comm"> <i class="fa fa-comment"></i> <span class="feat-info-text"><?php comments_number( '0', '1', '%' ); ?></span> </div><!--feat-info-comm--> <?php } } ?> </div><!--feat-info-wrap--> <?php } ?> <?php if ( has_post_format( 'video' )) { ?> <div class="feat-vid-but"> <i class="fa fa-play fa-3"></i> </div><!--feat-vid-but--> <?php } ?> </div><!--row-widget-img--> <?php } ?> <div class="row-widget-text"> <?php if($showcat) { ?> <span class="side-list-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span> <?php } ?> <p><?php the_title(); ?></p> </div><!--row-widget-text--> </a> </li> <?php endwhile; wp_reset_postdata(); ?> </ul> </div><!--row-widget-wrap-->
Sure, let’s help you integrate the new code with your existing
widget-catrow.php
structure. It sounds like you’re aiming to blend the featured articles logic with your current setup. Here’s a suggested approach:- Identify Featured Articles: First, add the code to identify the last four featured articles (tagged as ‘FEAT’) at the beginning of your
widget-catrow.php
. This would look something like this:php
$featured_posts = new WP_Query(array( 'tag' => 'FEAT', 'posts_per_page' => 4 )); $featured_ids = []; if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } } wp_reset_postdata();
This code fetches the IDs of the last four ‘FEAT’ tagged articles and stores them in
$featured_ids
.Modify Existing Query: In your existing
<ul class="row-widget-list">
section, you need to adjust the WP_Query to exclude these featured posts. Modify the$recent
query like this:$recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids ));
Here,
'post__not_in' => $featured_ids
ensures that the recent posts displayed in the widget do not include the ones marked as featured.Integrate with Your Layout: Keep the rest of your HTML and PHP structure as it is. The loop for displaying the recent posts remains the same:
while($recent->have_posts()) : $recent->the_post(); // Your existing HTML and PHP for displaying each post endwhile;
- Final Steps: After the endwhile, you correctly have
wp_reset_postdata();
. This is good practice and should remain there to reset the post data after custom queries. - Testing: Since you’re working on a local copy, give this a test run. Check if the recent posts are displayed correctly and the featured posts (tagged ‘FEAT’) are excluded.
By following these steps, you should be able to integrate the new functionality with your existing
widget-catrow.php
file. Remember, WordPress can be a bit quirky, so if something doesn’t look right, don’t hesitate to reach out again. We’ll work through it together!I can’t thank you enough for your help.
I am trying to get the hang of it, but i am unfortunately getting an error returned. “Warning: Undefined variable $featured_ids in C:\Users\kevin\Local Sites\test-mobile\app\public\wp-content\themes\flex-mag\widgets\widget-catrow.php on line 51“So, i believe i have followed the first few steps correctly … i believe.
I have implemented this $featured_posts function at the begining of my widget-catrow.php.:$featured_posts = new WP_Query(array( 'tag' => 'feat', 'posts_per_page' => 4 )); $featured_ids = []; if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } } wp_reset_postdata();
I have then edited my $recent function to :
<?php $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids )); ?>
I was a bit confused as to how to implement the while($recent) function, perdon my lack of knowledge about PHP. I actually added it this way just after the $recent function:
<?php while($recent->have_posts()) : $recent->the_post(); ?>
Then, the rest hasn’t been changed. I am trying to figure out what have i done wrong for the variable not to be defined. Learning everyday ! Thank you very much once again.
-
This reply was modified 1 year, 3 months ago by
spojetski.
To troubleshoot this, let’s ensure that the variable is correctly set and accessible:
- Variable Scope: Ensure that the
$featured_ids
variable is defined in the same scope as where it’s being used. If it’s inside a function or conditional statement, it might not be accessible outside of that scope. From your code, it seems you have defined it correctly, but just double-check that there are no conditional statements or functions that might be limiting its scope. - Variable Initialization: Make sure
$featured_ids
is initialized before it’s used in the$recent
query. It should be an array, so initializing it as an empty array at the start of your script might help.
$featured_ids = []; // Initializing the array $featured_posts = new WP_Query(array( 'tag' => 'feat', 'posts_per_page' => 4 )); if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } } wp_reset_postdata();
Check Conditional Logic: Ensure that the block of code that assigns values to
$featured_ids
is actually being executed. If, for some reason,$featured_posts->have_posts()
returns false,$featured_ids
would remain an empty array.Debugging: You can add debugging statements to check whether
$featured_ids
is being populated as expected. Usevar_dump($featured_ids);
after the loop that populates it to see its contents.The
$recent
Query: Ensure that the$recent
query is correctly formatted and that it’s placed after the$featured_ids
array has been populated.$recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids )); while ($recent->have_posts()) : $recent->the_post(); // Your existing HTML and PHP for displaying each post endwhile;
If after these checks the issue still persists, there might be a deeper problem with how the variables are being handled in your script. In that case, consider reviewing the entire script to ensure all parts are correctly integrated and that there are no hidden conditional statements or functions that might be affecting the scope or initialization of your variables.
-
This reply was modified 1 year, 3 months ago by
WP Provider.
Sorry my clumsy finger somehow posted this too early. I’ve edited my previous answer with as many details i could give you.
This isn’t the mobile display but the desktop display on my clone local wordpress. This is the said widget and the said featured post block.<?php /** * Plugin Name: Category Row Widget */ $featured_posts = new WP_Query(array( 'tag' => 'feat', 'posts_per_page' => 4 )); $featured_ids = []; if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } } wp_reset_postdata(); add_action( 'widgets_init', 'mvp_catrow_load_widgets' ); function mvp_catrow_load_widgets() { register_widget( 'mvp_catrow_widget' ); } class mvp_catrow_widget extends WP_Widget { /** * Widget setup. */ function __construct() { /* Widget settings. */ $widget_ops = array( 'classname' => 'mvp_catrow_widget', 'description' => __('A widget that displays a list of posts from a category of your choice.', 'mvp-text') ); /* Widget control settings. */ $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'mvp_catrow_widget' ); /* Create the widget. */ parent::__construct( 'mvp_catrow_widget', __('Flex Mag: Category Row Widget', 'mvp-text'), $widget_ops, $control_ops ); } /** * How to display the widget on the screen. */ function widget( $args, $instance ) { extract( $args ); /* Our variables from the widget settings. */ global $post; $title = apply_filters('widget_title', $instance['title'] ); $categories = $instance['categories']; $showcat = $instance['showcat']; /* Before widget (defined by themes). */ echo $before_widget; /* Display the widget title if one was input (before and after defined by themes). */ if ( $title ) echo $before_title . $title . $after_title; ?> <div class="row-widget-wrap left relative"> <ul class="row-widget-list"> <?php $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids )); ?> <?php while($recent->have_posts()) : $recent->the_post(); ?> <li> <a href="<?php the_permalink(); ?>" rel="bookmark"> <?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?> <div class="row-widget-img left relative"> <?php the_post_thumbnail('mvp-mid-thumb', array( 'class' => 'reg-img' )); ?> <?php the_post_thumbnail('mvp-small-thumb', array( 'class' => 'mob-img' )); ?> <?php $post_views = get_post_meta($post->ID, "post_views_count", true); if ( $post_views >= 1) { ?> <div class="feat-info-wrap"> <div class="feat-info-views"> <i class="fa fa-eye fa-2"></i> <span class="feat-info-text"><?php mvp_post_views(); ?></span> </div><!--feat-info-views--> <?php $disqus_id = get_option('mvp_disqus_id'); if ( ! $disqus_id ) { if (get_comments_number()==0) { } else { ?> <div class="feat-info-comm"> <i class="fa fa-comment"></i> <span class="feat-info-text"><?php comments_number( '0', '1', '%' ); ?></span> </div><!--feat-info-comm--> <?php } } ?> </div><!--feat-info-wrap--> <?php } ?> <?php if ( has_post_format( 'video' )) { ?> <div class="feat-vid-but"> <i class="fa fa-play fa-3"></i> </div><!--feat-vid-but--> <?php } ?> </div><!--row-widget-img--> <?php } ?> <div class="row-widget-text"> <?php if($showcat) { ?> <span class="side-list-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span> <?php } ?> <p><?php the_title(); ?></p> </div><!--row-widget-text--> </a> </li> <?php endwhile; wp_reset_postdata(); ?> </ul> </div><!--row-widget-wrap-->
Above is part of said file catrow.php
-
This reply was modified 1 year, 3 months ago by
spojetski.
Thank you for sharing the code from your
catrow.php
file. The issue you’re encountering is due to the scope of the$featured_ids
variable. In PHP, variables defined outside of a function are not accessible inside the function unless they are explicitly declared as global or passed as parameters. In your case,$featured_ids
is defined outside of thewidget
function, but it’s being used inside this function, leading to the undefined variable error.To resolve this, you have two options:1. Declare
$featured_ids
as a Global VariableYou can declare
$featured_ids
as a global variable inside thewidget
function. However, this is generally not the best practice, especially in WordPress development, as it can lead to conflicts and hard-to-maintain code.2. Refactor Your CodeA better approach is to refactor your code to define and use
$featured_ids
within the scope of thewidget
function where it is needed. Here’s how you could modify your code:function widget( $args, $instance ) {
extract( $args );
// ... [other code] ...
// Identify Featured Articles
$featured_posts = new WP_Query(array( 'tag' => 'feat', 'posts_per_page' => 4 ));
$featured_ids = [];
if ($featured_posts->have_posts())
{
while ($featured_posts->have_posts()) { $featured_posts->the_post();
$featured_ids[] = get_the_ID(); } } wp_reset_postdata();
// Query for recent posts, excluding featured posts $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids ));
// ... [rest of your widget display code] ...
}
In this refactored code, the
$featured_posts
query and the population of$featured_ids
are moved inside thewidget
function. This ensures that$featured_ids
is defined and accessible where it’s used.Remember, after making these changes, test your widget thoroughly to ensure everything is working as expected. WordPress can be finicky with changes, and it’s always good to verify functionality after modifications.
Hi. Thanks you once again.
It has proven difficult for us (yes, my wife tried to give me a hand tonight !) to understand exactly where we were meant to implement all this despite your clear step by step instruction, but i am not giving up !
I have once again provided the code (in full, that time !) with the additions and tweaks we have made in BOLD (i wasn’t too sure where to close that last } after “// … [rest of your widget display code] …” to be honest with you.
I am obviously not there yet, ending up with a parse error “Parse error: syntax error, unexpected token “endwhile” in C:\Users\kevin\Local Sites\test-mobile\app\public\wp-content\themes\flex-mag\widgets\widget-catrow.php on line 100“I am sorry if despite your clear explanation, i have yet to make it work !
<?php /** * Plugin Name: Category Row Widget */ function widget( $args, $instance ) { extract( $args ); add_action( 'widgets_init', 'mvp_catrow_load_widgets' ); function mvp_catrow_load_widgets() { register_widget( 'mvp_catrow_widget' ); } class mvp_catrow_widget extends WP_Widget { /** * Widget setup. */ function __construct() { /* Widget settings. */ $widget_ops = array( 'classname' => 'mvp_catrow_widget', 'description' => __('A widget that displays a list of posts from a category of your choice.', 'mvp-text') ); /* Widget control settings. */ $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'mvp_catrow_widget' ); /* Create the widget. */ parent::__construct( 'mvp_catrow_widget', __('Flex Mag: Category Row Widget', 'mvp-text'), $widget_ops, $control_ops ); } /** * How to display the widget on the screen. */ function widget( $args, $instance ) { extract( $args ); /* Our variables from the widget settings. */ global $post; $title = apply_filters('widget_title', $instance['title'] ); $categories = $instance['categories']; $showcat = $instance['showcat']; /* Before widget (defined by themes). */ echo $before_widget; /* Display the widget title if one was input (before and after defined by themes). */ if ( $title ) echo $before_title . $title . $after_title; ?> <div class="row-widget-wrap left relative"> <ul class="row-widget-list"> <?php // Identify Featured Articles $featured_posts = new WP_Query(array( 'tag' => 'feat', 'posts_per_page' => 4 )); $featured_ids = []; if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } } wp_reset_postdata(); $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids )); ?> <li> <a href="<?php the_permalink(); ?>" rel="bookmark"> <?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?> <div class="row-widget-img left relative"> <?php the_post_thumbnail('mvp-mid-thumb', array( 'class' => 'reg-img' )); ?> <?php the_post_thumbnail('mvp-small-thumb', array( 'class' => 'mob-img' )); ?> <?php $post_views = get_post_meta($post->ID, "post_views_count", true); if ( $post_views >= 1) { ?> <div class="feat-info-wrap"> <div class="feat-info-views"> <i class="fa fa-eye fa-2"></i> <span class="feat-info-text"><?php mvp_post_views(); ?></span> </div><!--feat-info-views--> <?php $disqus_id = get_option('mvp_disqus_id'); if ( ! $disqus_id ) { if (get_comments_number()==0) { } else { ?> <div class="feat-info-comm"> <i class="fa fa-comment"></i> <span class="feat-info-text"><?php comments_number( '0', '1', '%' ); ?></span> </div><!--feat-info-comm--> <?php } } ?> </div><!--feat-info-wrap--> <?php } ?> <?php if ( has_post_format( 'video' )) { ?> <div class="feat-vid-but"> <i class="fa fa-play fa-3"></i> </div><!--feat-vid-but--> <?php } ?> </div><!--row-widget-img--> <?php } ?> <div class="row-widget-text"> <?php if($showcat) { ?> <span class="side-list-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span> <?php } ?> <p><?php the_title(); ?></p> </div><!--row-widget-text--> </a> </li> <?php endwhile; wp_reset_postdata(); ?> </ul> </div><!--row-widget-wrap--> <?php /* After widget (defined by themes). */ echo $after_widget; } /** * Update the widget settings. */ function update( $new_instance, $old_instance ) { $instance = $old_instance; /* Strip tags for title and name to remove HTML (important for text inputs). */ $instance['title'] = strip_tags( $new_instance['title'] ); $instance['categories'] = strip_tags( $new_instance['categories'] ); $instance['showcat'] = strip_tags( $new_instance['showcat'] ); return $instance; } function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( 'title' => 'Title', 'showcat' => 'on' ); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <!-- Widget Title: Text Input --> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" /> </p> <!-- Category --> <p> <label for="<?php echo $this->get_field_id('categories'); ?>">Select category:</label> <select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" style="width:100%;"> <option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>All Categories</option> <?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?> <?php foreach($categories as $category) { ?> <option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option> <?php } ?> </select> </p> <!-- Show Categories --> <p> <label for="<?php echo $this->get_field_id( 'showcat' ); ?>">Show categories on posts:</label> <input type="checkbox" id="<?php echo $this->get_field_id( 'showcat' ); ?>" name="<?php echo $this->get_field_name( 'showcat' ); ?>" <?php checked( (bool) $instance['showcat'], true ); ?> /> </p> <?php } } } ?>
i suggest to use a editor for php to resolve the issue is you need to delete the endwhile in the line 100 i did it for you
<?php /** * Plugin Name: Category Row Widget */ function widget( $args, $instance ) { extract( $args ); add_action( 'widgets_init', 'mvp_catrow_load_widgets' ); function mvp_catrow_load_widgets() { register_widget( 'mvp_catrow_widget' ); } class mvp_catrow_widget extends WP_Widget { /** * Widget setup. */ function __construct() { /* Widget settings. */ $widget_ops = array( 'classname' => 'mvp_catrow_widget', 'description' => __('A widget that displays a list of posts from a category of your choice.', 'mvp-text') ); /* Widget control settings. */ $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'mvp_catrow_widget' ); /* Create the widget. */ parent::__construct( 'mvp_catrow_widget', __('Flex Mag: Category Row Widget', 'mvp-text'), $widget_ops, $control_ops ); } /** * How to display the widget on the screen. */ function widget( $args, $instance ) { extract( $args ); /* Our variables from the widget settings. */ global $post; $title = apply_filters('widget_title', $instance['title'] ); $categories = $instance['categories']; $showcat = $instance['showcat']; /* Before widget (defined by themes). */ echo $before_widget; /* Display the widget title if one was input (before and after defined by themes). */ if ( $title ) echo $before_title . $title . $after_title; ?> <div class="row-widget-wrap left relative"> <ul class="row-widget-list"> <?php // Identify Featured Articles $featured_posts = new WP_Query(array( 'tag' => 'feat', 'posts_per_page' => 4 )); $featured_ids = []; if ($featured_posts->have_posts()) { while ($featured_posts->have_posts()) { $featured_posts->the_post(); $featured_ids[] = get_the_ID(); } } wp_reset_postdata(); $recent = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => 6, 'post__not_in' => $featured_ids )); ?> <li> <a href="<?php the_permalink(); ?>" rel="bookmark"> <?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?> <div class="row-widget-img left relative"> <?php the_post_thumbnail('mvp-mid-thumb', array( 'class' => 'reg-img' )); ?> <?php the_post_thumbnail('mvp-small-thumb', array( 'class' => 'mob-img' )); ?> <?php $post_views = get_post_meta($post->ID, "post_views_count", true); if ( $post_views >= 1) { ?> <div class="feat-info-wrap"> <div class="feat-info-views"> <i class="fa fa-eye fa-2"></i> <span class="feat-info-text"><?php mvp_post_views(); ?></span> </div><!--feat-info-views--> <?php $disqus_id = get_option('mvp_disqus_id'); if ( ! $disqus_id ) { if (get_comments_number()==0) { } else { ?> <div class="feat-info-comm"> <i class="fa fa-comment"></i> <span class="feat-info-text"><?php comments_number( '0', '1', '%' ); ?></span> </div><!--feat-info-comm--> <?php } } ?> </div><!--feat-info-wrap--> <?php } ?> <?php if ( has_post_format( 'video' )) { ?> <div class="feat-vid-but"> <i class="fa fa-play fa-3"></i> </div><!--feat-vid-but--> <?php } ?> </div><!--row-widget-img--> <?php } ?> <div class="row-widget-text"> <?php if($showcat) { ?> <span class="side-list-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span> <?php } ?> <p><?php the_title(); ?></p> </div><!--row-widget-text--> </a> </li> <?php wp_reset_postdata(); ?> </ul> </div><!--row-widget-wrap--> <?php /* After widget (defined by themes). */ echo $after_widget; } /** * Update the widget settings. */ function update( $new_instance, $old_instance ) { $instance = $old_instance; /* Strip tags for title and name to remove HTML (important for text inputs). */ $instance['title'] = strip_tags( $new_instance['title'] ); $instance['categories'] = strip_tags( $new_instance['categories'] ); $instance['showcat'] = strip_tags( $new_instance['showcat'] ); return $instance; } function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( 'title' => 'Title', 'showcat' => 'on' ); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <!-- Widget Title: Text Input --> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" /> </p> <!-- Category --> <p> <label for="<?php echo $this->get_field_id('categories'); ?>">Select category:</label> <select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" style="width:100%;"> <option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>All Categories</option> <?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?> <?php foreach($categories as $category) { ?> <option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option> <?php } ?> </select> </p> <!-- Show Categories --> <p> <label for="<?php echo $this->get_field_id( 'showcat' ); ?>">Show categories on posts:</label> <input type="checkbox" id="<?php echo $this->get_field_id( 'showcat' ); ?>" name="<?php echo $this->get_field_name( 'showcat' ); ?>" <?php checked( (bool) $instance['showcat'], true ); ?> /> </p> <?php } } } ?>
-
This reply was modified 1 year, 3 months ago by
WP Provider.
This, i should not have gone by without finding myself … Shows i have not really gotten my head around that While – Endwhile function.
The good news, is it seems we are moving forward as we have no more error. On the other hand, the widget doesnt show any article. Im not giving up just yet :)))Edit: i have searche for a hint on the back-end, and the “Category Row Widget” has now disappeared from the widget setup page. It comes back after reverting the catrow.php file to the previous version.
check the
WP_Query
parameters, especially ‘cat’ => $categories. Ensure$categories
is getting the correct category ID from the widget settings.and try to debug to see which article has been chosen to distinct between just an ID problem or the function that we created didn’t do the job properly-
This reply was modified 1 year, 3 months ago by
WP Provider.
-
This reply was modified 1 year, 3 months ago by
- The topic ‘Widget cat’ exceptions ?’ is closed to new replies.