• I really like what I have been able to do with this plugin so far. I can set control functions based on a specific page. I.E. All the Post widgets on a certain page have an Excerpt of 25 words. My question is: Is there anywhere that expands how to apply a function to a specific Post widget. If I have more than one Post widget on the same page, I may want the category for one widget to be Cats and the other to be Dogs. I am trying to choose these Categories dynamically and hot have to hard code them in the Post settings. Thank you for any suggestions.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Do this

    1. Create a snippet, which has a function, which controls, which category to show.
    2. Install plugin “Widget Logic” if you don’t have yet installed
    3. Set as condition the function, which you have created with Code Snippet

    At this way you can use snippets in order to control visibility of widgets.

    Thread Starter LesTexas60

    (@lestexas60)

    I can do that with Dynamic Conditions. It’s not showing or hiding the widget that is the issue. I want to be able to set Widget number one to show Cat Category and widget number 2 to show Dog Category. Both widgets are on the same page. The next time I go to that page from a different button and it will show Birds and Lizards based on the URL variable. I can’t figure out how to set the function towards a specific widget on the page.

    Ok. Widget Context, Widget Logic and Dynamic conditions just handle the visibility of the widget itself.

    If you want to control the content I don’t see any other way than create your own PHP code and user you own functions as shortcodes.

    If you use shortcodes, all can be inside same text widget like
    [catCats]
    [catDogs]

    See https://codex.www.ads-software.com/Conditional_Tags
    maybe you can use some category related conditions using some internal functions.

    Generating the actual list, you apparently needs to study how to create a proper function for that purpose. Maybe you can find build-in functions for this purpose.

    Thread Starter LesTexas60

    (@lestexas60)

    It’s my understanding that the purpose of this plugin is to create snippets of PHP code. As I’ve said, I’ve already done this. It works well. The I’ve used code to apply the snippet to a specific page, but it affects both Post widgets I have on the page. What I need is some help to apply the code to a specific Post widget on that page so my function code only changes the parameters of a specific Post widget. I do’t know how to identify one Post widget on a page from another. ??

    AS far as I know that is possible to set for widgets only visibility related conditions.

    You can use for content only those conditions, which the plugin has fields in the backend.

    You should build your own widget, which has an additional field to set conditions, what to show.

    You can do widgets with Code Snippet and you don’t need to create entire plugin. I copied widget.php from ppb style pack and I changes some code. I get the widget to the backend and it worked the same ways as it worked in the plugin.

    If you find somewhere the original widget code, you can create almost similar widget, but you must change some names an add a field.

    But creating separate shortcodes, you can get different results.
    In practise you can build a shortcode, which work like widget, but the control is outside from the widget.

    Thread Starter LesTexas60

    (@lestexas60)

    THank you for the input but we are just not communicating. Each Post widget you use has it’s own Query variables. I want 2 Post widgets on the same page. One to show Dogs and one to show Cats. I want to set those 2 query variables through code. I just don’t know how to identify each separate Post widget on the same page.

    I don’t figure which widget you use and what fields that widged has.
    I don’t figure how you in overall can set additional query variables for widgets by using Code Snippet, because the usually get variables from fields, which each widget has.

    Does some widget some hooks? I don’t know any widget, which use hooks. If a widget has hooks of course then it is possible to control widget with Code Snippet.

    I just don’t know how to identify each separate Post widget on the same page.

    What you want might be impossible. When WordPress generates the code, it sets for each widget a number by using consecutive numbering. But I believe that the number becomesin the final generating process. If the number becomes in the final process, it is impossible to control the content by using the consecutive numbering. WordPress stores the consecutive numbering into database, because if you delete a widget and create a new, it has unused number. But if WordPress puts the number in the final run, it doesn’t help you at all even if WordPress stores the numbering into database. You could not use it as condition, what to show.

    If that, what I assume, is true, you can control the content only by creating shortcodes instead of using specific widget.

    I don’t know how WordPress generates the final code. My presumption might be incorrect.

    Thread Starter LesTexas60

    (@lestexas60)

    tapiohuuhaa, I don’t know if this is your plugin but it almost seems like you don’t understand what this plugin does. The plugin lets me create special code that will then be applied to the website. I can set it across the entire site or per page if I like. I can also modify settings like Query Category. This is a good plugin , I just need to find out more about the code in WP that I can affect.

    I do. There are these ways to use this plugin:

    1. Use existing action or filter hooks.
    2. Create shortcuts, which can be used in any page and text widgets.
    3. Use you own function calls in modified templates.

    But you are writing about widget. I don’t know how you have used your code in widgets. I just try to tell, that apparently you can’t use the id of the widget as any condition.

    I don’t understand how you have used this:

    I can also modify settings like Query Category

    Thread Starter LesTexas60

    (@lestexas60)

    I see you help with a lot of support responses, Are you the author of this plugin?

    Here is an example of my entry:It’s code I have written. This sets the length of the post excerpt on different pages but if there are 2 post widgets on the same page, both posts are set to the same length. Trying to figure out how to identify just one post widget.

    $currentpage = $_SERVER[‘REQUEST_URI’];
    if(“/page-6/”==$currentpage) {
    function mytheme_custom_excerpt_length( $length ){
    return 30;
    }
    } elseif (“/page-5/”==$currentpage) {
    function mytheme_custom_excerpt_length( $length ) {
    return 20;
    }
    } elseif (“/page-4/”==$currentpage) {
    function mytheme_custom_excerpt_length( $length ) {
    return 5;

    }
    }

    add_filter( ‘excerpt_length’, ‘mytheme_custom_excerpt_length’, 999 );

    • This reply was modified 5 years, 3 months ago by LesTexas60.

    I’m not author.

    But as I wrote, I suppose that you cant’t separate contents of the same type of widget from each others the way you want. You should build the desired functionality using just one widget.

    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @lestexas60,

    I’m a little confused about your use of the word ‘widget’, as in WordPress that typically refers to a sidebar widget like you can manage on the Appearance > Widgets admin menu. However, it looks like you are referring to individual posts here, is that correct?

    If so, you can use functions like get_post_ID() within filter hooks like expert_length to conditionally change the length of the excerpt based on the current post. Here’s an example:

    add_filter( 'excerpt_length', function ( $number ) {
    	$post_id = get_the_ID();
    
    	if ( ! $post_id ) {
    		return $number;
    	}
    
    	if ( 56 === $post_id ) {
    		return 30;
    	
    	} elseif ( 1 === $post_id ) {
    		return 20;
    	
    	} elseif ( 3801 === $post_id ) {
    		return 5;
    	}
    
    	return $number;
    } );

    Wrong answer. sorry.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Apply a snippet function to a specific Post Widget’ is closed to new replies.