Tell if a WordPress page has "grandchildren" - wordpress

I want a conditional that checks if a page has at least 2 levels of children; that is, if its children have children. I know you can get ancestors, and get a page's depth, but I can't find a way to see if a page is a grandparent (or great-grandparent, etc.).
How can I construct a conditional statement that returns true if a page has more than one level of children?

you can check if a post has a parent with $post->post_parent, if no parent returns 0.
if($post->post_parent == 0){
// your code
}
The problem here is that i think you can`t check easily if a page has more than one.
You will need to create a custom query to get all pages that have a parent, put that result in an array, then use array_count_values() function to check for duplicates, now you can know if a page has more than one children....
I think you can do it that way

Related

How can i use Custom attributes of html in Google tag manager to track clicks on lots of buttons

I have some long lists of buttons. For example, i have one single list of car models that all have the same custom data attribute of "modellist", while they have also another data attribute which is the name of that car model (Note that these buttons are NOT links).
the attached image
Now, without the need of creating a single tag for each and every one of these buttons, i need to find a faster way for this case using the google tag manager and GA4 (google analytics 4), so that i would be able to track clicks on these buttons. Does anyone know how can i do this?
I very highly appreciate your help & support here.
Here are the steps and how to do it.
1. Create a Custom JavaScript Variable;
Here is the Screenshot
The code is:
function(){
// Get the click element;
var clickElement = {{Click Element}};
// Check there is a closet parent element you want. If it doesn't then return false;
if(!clickElement.closest("div.stepped-selection__list-item"))
return false;
// Check the parent dom has the attribute you want.
// If it does, return the attribute value. Otherwise, return false;
var modelListDom = clickElement.closest("div.stepped-selection__list-item");
if(modelListDom.hasAttribute("data-trackervalue")){
return modelListDom.getAttribute("data-trackervalue");
}
return false;
}
2. Create the trigger
Here is the screenshot:
First, using the click element > match css selector > To catch all the element inside the selector. You can modify it a bit to make it more suitable in your real case.
Second, the Variable we create in step1. It will return false if something not we expected. So we don't want to trigger the tag if the Variable is return false.
3. Create the Tag.
The Tag config is the easiest one.
Just use the Trigger in step2.
And give the event name and event parameter you would like.
Give the event parameter value as the Variable in Step1.

Can't access SelectedNode Value from asp Treeview in javascript

I have an asp page that has a Treeview on it that is populated in code behind (so not data-bound). As I expand the nodes eventually the list of item becomes so big that after the postback, I lose where I was in the tree. So I've been looking into using client-side script to use scrollIntoView but all the examples of how to get the current selected node (the node I just expanded), seem to fail for me.
var elem = document.getElementById('navTree_SelectedNode');
alert(elem.value);
For me the .value is always null, as though no item has been selected. I've tried calling this code on the window.load and also by running a script from the code-behind. So I know there are lots of posts of this is how to do it, but I can't get an ID back of the item so I can then do a document.getElementById() on to then run the scrollIntoView. Maybe I'm trying to access the value too early/late and it's not been set, so where would the best place for me to check this be?
I've also tried accessing it with:
var test = document.getElementById('navTree_Data.selectedNodeID');
and still no luck.
Just a thought, selected is the item I have just clicked on to expand, not Checked as the nodes have check boxes?
Many thanks
The issue was being caused by the .SelectAction on my nodes being set to TreeNodeSelectAction.Expand when it should be set to .SelectExpand.

Sitecore 6.5 change user control dynamically

I have a collection of some items. Using * symbol I set user control (ascx) in presentation details for all of them. Now I have a problem because on of this items has to be display in another control. Is there some trick that allow me to change used control dynamically, for example checking url segment?
I'm guessing you're using wildcard item called * with some presentation details defined on it. And now you want to display different components for one you the urls?
If you want to have completely different presentation, you can add another item as a sibling of the * item and put new presentation detail there. This item will be matched before the wildcard item, if the url segment is equal to this item name.
If you want to change only one or few components, you can use personalization for this component and where the item name compares to ... rule.
Marek's answer is preferable, but for completeness I will provide another potential option.
It depends on how you are handling the wildcards. I don't think it will work if you are using the wildcard item module from the Marketplace and it might not play well with some of your existing code, but here goes...
You could place the required presentation details on the target items themselves. Then when you resolve the wildcard, you would need to change the context item to be selected target item. When the page loads, it will use the presentation of the newly set context item.
One way to achieve this would be to create a custom item resolver
class WildCardItemResolver : ItemResolver
{
public override Process(HttpRequestArgsargs args)
{
base.Process(args);
// if Context.Item is as wildcard
// look up the target item
Context.Item = targetItem
}
}

jsView: How to get current element during "onBeforeChangeEvent"

I am wondering if there is a way that I can get the current element that is triggering the onBeforeChangeEvent function in jsViews. I want to get the current element that it is working on, to do some extra jQuery work on it.
A basic example is below:
$.views.helpers({
onAfterChange: function (ev)
{
//want to get element instance right here as an example.....
if (!PageSettings.cancelUpsert && ev.type == "change")
{
//do somthing to element that is currently being processed.
}
}
});
I searched through the objects being returned, but could find a stable way to get to the element. Any ideas, or tips towards where to look would greatly be appreciated. Thanks!
If you are looking for a data-linked element, such as an input: <input data-link="..." />, whose change event triggered the change, you can get that from either this.linkCtx.elem or ev.target.
More generally, the this pointer is the view object that is being changed.
There are a number of helper methods on the view object that you can use to access different elements within that view. For example this.contents("someSelector") will return a jQuery object that selects top-level elements in that view, and this.contents(true, "someSelector") will apply the selector to filter on all elements in the view (deep search not just top-level).
(You can use the selector "*" to get all elements)

EnumChildWindows doesn't return direct children

I am trying to create an application that dumps all processes and their children controls to a text file. I used EnumChildWindow but it visits all children including children's childdren. I can't create hierarchy using this method.
Is there another way of doing this?
Thanks
You should instead use FindWindowEx() and specify NULL for the hwndChildAfter parameter.
This will enumerate all direct descendant child windows.

Resources