Is it possible with jquery to use the nth-child() selector on an 'a' or an 'a:hover', not just 'li'? - jquery-selectors

Ive created a navigation bar where the hover state of each link has be a different color so im trying to select the a:hover states with jquerys nth-child() selector. i can get it to select the li element but not the a or the a:hover. Currently all the hovers are blue.
here is the jquery code im trying to use:
jQuery(document).ready(function() {
jQuery('#leftbar li:nth-child(3)').css('border-bottom', '#000000 5px solid');
});
Hi the navigation is generated with php, here it is:
<ul id="leftbar">
<?php
$pagepath = "content/pages/";
$legalpath = "content/legals/";
$mainnavpath = "content/.system-use/navigation/";
$mainnavfile = $mainnavpath."mainnav.inc";
if (file_exists($mainnavfile)) {
require $mainnavfile;
sort ($mainfiles);
for($i=0; $i<count($mainfiles); $i++)
{
if (!preg_match("/XX-/",$mainfiles[$i])) {
$displayname = preg_replace("/\.inc/i", "", $mainfiles[$i]);
$displayname = substr($displayname, 3);
echo "<li>";
echo "<a ";
if ($page==$displayname) {echo ' class="active"';} else {echo ' class="prinav"';}
echo "title='$displayname' href='";
if ($useredirect=="yes"){echo '/'.$displayname.'/';} else {echo '/index.php?page='.$displayname;}
echo"' ";
echo "><span>$displayname</span></a></li>\n";
}}
}
else { echo "<strong>No Navigation - Please Login to your Admin System and set the Page Order</strong>"; }
?>
here is the site im working on:
http://entourageuk.com/
Cheers!
Paul

You can't select using a CSS pseudo selector like :hover, but yes, you can select an <a> element.
Whether :nth-child is appropriate depends on your markup. I'm going to assume that each <a> is a child of the <li> elements you're selecting.
If that's the case, then you would just add a to the selector.
jQuery('#leftbar li:nth-child(3) > a').css(...
This uses the > child selector, and is basically saying that I want the <a> element(s) that is a direct child of the <li> element(s) that is the third child of its container and is a descendant of leftbar.

Related

Find and extract content of division of certain class using DomXPath

I am trying to extract and save into PHP string (or array) the content of a certain section of a remote page. That particular section looks like:
<section class="intro">
<div class="container">
<h1>Student Club</h1>
<h2>Subtitle</h2>
<p>Lore ipsum paragraph.</p>
</div>
</section>
And since I can't narrow down using class container because there are several other sections of class "container" on the same page and because there is the only section of class "intro", I use the following code to find the right division:
$doc = new DOMDocument;
$doc->preserveWhiteSpace = FALSE;
#$doc->loadHTMLFile("https://www.remotesite.tld/remotepage.html");
$finder = new DomXPath($doc);
$intro = $finder->query("//*[contains(#class, 'intro')]");
And at this point, I'm hitting a problem - can't extract the content of $intro as PHP string.
Trying further the following code
foreach ($intro as $item) {
$string = $item->nodeValue;
echo $string;
}
gives only the text value, all the tags are stripped and I really need all those divs, h1 and h2 and p tags preserved for further manipulation needs.
Trying:
foreach ($intro->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo $name;
echo $value;
}
is giving the error:
Notice: Undefined property: DOMNodeList::$attributes in
So how could I extract the full HTML code of the found DOM elements?
I knew I was so close... I just needed to do:
foreach ($intro as $item) {
$h1= $item->getElementsByTagName('h1');
$h2= $item->getElementsByTagName('h2');
$p= $item->getElementsByTagName('p');
}

domxpath- how to get content for parent tag only instead of child tags

I, am using domxpath query for fetching content for parent tag only that is (td[class='s']) instead of including div content which is nested inside that td as given below in my code.
<?php
$second_trim='<td class="s" style="line-height:18px;">THIS TEXT IS REQUIRED and <div id="a" style="display:none;background-color:black;border:1px solid #ddd;padding:5px;color:black;">THIS TEXT IS NOT REQUIRED </div></td>';
$dom = new DOMDocument();
$doc->validateOnParse = true;
#$dom->loadHTML($second_trim);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$b = $xpath->query('//td[#class="s"]');
echo "<p style='font-size:14px;color:red;'><b style='font-size:18px;color:gray;'>cONTENT :- </b>".$b->item(0)->nodeValue."</p>";
?>
so how to remove content of that div tag and fetching only td's content any ideas !!
EDIT
If you are only interested in the direct text content modify your xpath query:
$b = $xpath->query('//td[#class="s"]/text()');
echo '<p style="font-size:14px;color:red;">'
.'<b style="font-size:18px;color:gray;">cONTENT :- </b>'
.$b->item(0)->nodeValue
.'</p>';
Right now the result is very specific to the example:
If more than one direct text node exists, its not gone be displayed. To do that foreach through the DOMNodeList $b and echo every selected node value.

How to filter New WP Query by Custom Field Value?

I am creating new pages for each of my categories in wordpress. The post editor has a custom field that allows the selection of a sector type, this gets applied to the post on update. The custom field key is: sector, for custom field meta value options lets use SectorA, SectorB and SectorC. I am using a custom post type called projects.
I followed the advice at this link http://weblogtoolscollection.com/archives/2008/04/13/how-to-only-retrieve-posts-with-custom-fields/
How can I change the query line in the code below so that it filters the loop by a Sector name, lets use SectorA. I'll then reuse the code on each template page changing the value to SectorB and SectorC on the other pages.
I think this needs changing somehow:
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );
Currently it echos the sector value and description value successfully but is showing all the posts. So my attempt to limit it to sectorA using sector=sectorA doesn't seem to work?
This code is in functions.php:
function get_custom_field_posts_join($join) {
global $wpdb, $customFields;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key in ($customFields)) ";
}
function get_custom_field_posts_group($group) {
global $wpdb;
$group .= " $wpdb->posts.ID ";
return $group;
}
And this code is on the Template Page:
<?php /* Begin Custom Field Posts */ ?>
<h2>Custom Posts</h2>
<ul>
<?php
global $customFields;
$customFields = "'sector', 'description'";
$customPosts = new WP_Query();
add_filter('posts_join', 'get_custom_field_posts_join');
add_filter('posts_groupby', 'get_custom_field_posts_group');
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );//Uses same parameters as query_posts
remove_filter('posts_join', 'get_custom_field_posts_join');
remove_filter('posts_groupby', 'get_custom_field_posts_group');
while ($customPosts->have_posts()) : $customPosts->the_post();
$sector = get_post_custom_values("sector");
$description= get_post_custom_values("description");?>
<li><?php echo $sector[0]; ?></li>
<li><?php echo $description[0]; ?></li><br />
<?php endwhile; ?>
</ul>
<?php /* End Custom Field Posts */ ?>
Thanks for your help
May be this what you want
function get_custom_field_posts_join($join) {
global $wpdb, $customSector;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key = 'sector' and postmeta.value = '$customSector') ";
}
and modification of page
$customSector='sectorA';//<--- insert this
add_filter('posts_join', 'get_custom_field_posts_join');
Try using this code.
<?php
$sector = get_post_meta($post->ID, "sector", false);
if ($sector[0]=="") { ?>
<!-- If there are no custom fields, show nothing -->
<?php } else { ?>
<div class="sector">
<h3>Title</h3>
<?php foreach($sector as $sector) {
echo '<blockquote><p>'.$sector.'</p></blockquote>';
} ?>
</div>
<?php } ?>

Default checked checkboxes undefined in php

I am having trouble with checkboxes. What I am doing is displaying a list of checkboxes, if previously checked they will show the check mark, then you submit them and another php should recognize which were checked and which weren't. My script works fine for boxes previously unchecked, if you check them the action php recognizes it, but for boxes already checked I get Notice - undefined variable - for the boxes (even if unchecked/checked again). I really can't seem to find my way around this.
My code is
$ind=0; //counting variable
//generating checkboxes from an xml
foreach($xml as $checkbox)
{
$checks=$xml->checkbox[$ind]->active; //the active tag has a 0 or 1 stored.
echo "Activate ".$ind; // shows activate 0, activate 1, etc...
echo "<form name='checkb' action='show.php' method='post'>
echo "<input type='checkbox' name='checks[]' class='act' value='".$ind."'";
if($checks==0){ echo ">";} else{echo " checked ='checked'>";}
echo "<input type='hidden' name='ind' value=".$ind.">";
$ind=$ind+1;
echo "<input type='submit' name='sub' value='Submit'/> </form>"; }
On my action php I have
$chks = $_POST['checks'];
$N = count($chks);
echo("Active checkboxes ");
for($i=0; $i < $N; $i++)
{
echo($chks[$i] . " ");}
All this worked well until I decided to show if the boxes had been previously checked. So I guess the question is, why won't php recognize checked=checked as a true value? Or is there any other way to do this?
Thanks!
Seems like a lot of issues here.
Why are you outputting a form for each checkbox?
Where is your submit for the form?
You need a space in front of checked='checked' where you echo it out - echo " checked='checked'>"
If you move form output out of the loop, you will need to add your incremented value to the hidden input name property as well (or make it an array like checks, otherwise you will
only get one value for the field.
My attempt to try and clean it up a little.
$ind = 0;
echo "<form name='checkb' action='show.php' method='post'>";
foreach($xml as $checkbox){
echo "Activate $ind";
echo "<input type='checkbox' name='checks[]' class='act' value='$ind' ".(($checks == 0) ? " />" : " checked='checked' />";
echo "<input type='hidden' name='ind_$ind' value='$ind' />";
$ind++
}
echo "</form>";
Notice, like Mike said before that checked='checked' has the space before so that there is seperation between components. Also, your hidden elements all had the same names, so I added the $ind quantifier.
Hope this helps.

Zend_Navigation rendering submenu with partial

I've posted an edit to my question. While working on it I noticed the problem is easy to simplify.
I need a custom format of my submenu so i have to use partial. But then the problem occurs.
The below code shows the INCORRECT level (0):
echo $this->navigation()->menu()
->setMinDepth(1)
->setMaxDepth(1)
->setRenderParents(false)
->setOnlyActiveBranch(true)
->renderPartial(null, array('partials/menu.phtml', 'default'));
The below code shows the CORRECT menu level (1)
echo $this->navigation()->menu()
->setMinDepth(1)
->setMaxDepth(1)
->setRenderParents(false)
->setOnlyActiveBranch(true)
->render();
Any ideas? Guys please. I would appreciate any help!
Edit
My partials/menu.phtml:
foreach ($this->container as $page)
{
$active = $page->isActive();
echo '<div class="item">';
echo '<a class="'. ($active ? 'active' : '') .'" href="' . $this->baseUrl($page->getHref()) . '">' . $page->getLabel() . '</a>';
echo '</div>';
}
EDIT 2
My understanding of Zend_Navigation was, first to prepare container and than put it through partial.
$nav = $this->navigation()->menu()->setOnlyActiveBranch(true)->getContainer();
echo $this->navigation()->menu()->renderPartial($nav, array('/partials/menu.phtml', 'default'));
What is the point of setting set{Min/Max}Depth, parentRendering at the container when passing it anywehere is useless?
I use this code:
<?=$this->navigation()->menu()->renderPartial(null, 'shared/menu.phtml')?>
you should pass true to the method $page->isActive(true) so that also functions in depth.
in your partial
foreach ($this->container as $page) {
$active = $page->isActive(true);
if (count($page->getPages())) {
foreach ($page->getPages() as $subPage) {
$active = $subPage->isActive(true);
echo '<div class="item">';
echo '<a class="'. ($active ? 'active' : '') .'" href="' . $this->baseUrl($subPage->getHref()) . '">' . $subPage->getLabel() . '</a>';
echo '</div>';
}
}
}
before the second foreach you could add a check if and when to show the submenu.
my 2 cent.
EDIT
try this:
$partial = array('partials/menu.phtml', 'default');
echo $this->navigation()->menu()
->setMinDepth(1)
->setMaxDepth(1)
->setRenderParents(false)
->setOnlyActiveBranch(true)
->setPartial($partial)
->render();
Came across this while searching for an answer to the same problem. Having looked through the code for Zend_View_Helper_Navigation_Menu, it doesn't look like any of the view helper options are passed through to the view partial, although I don't see why they couldn't be... (in ZF 1.12 take look a line 736 of Zend_View_Helper_Navigation_Menu, the only thing passed is the container itself, the options array could easily be passed along with it, or the container prefiltered, may be worth filing a feature request with ZF)
These options are purely a way of filtering the Zend_Navigation_Container for rendering with the default renderMenu method. As you say, it seems you can accomplish the same thing by first filtering the container and then passing it as the first argument of the renderPartial method
In your main view
Find the container of the submenu located in navigation config. Then echo this container using said partial.
$pages = $this->navigation()->findOneBy('label', 'Label of your submenu');
echo $this->navigation()->menu()->renderPartial($pages,module/partials/menu.phtml');
In the partial (module/partials/menu.phtml)
Customise. This example iterates over the top level pages of your chosen container.
foreach ($this->container as $page) {
echo $this->navigation()->menu()->htmlify($page) . PHP_EOL;
}