I have thoroughly confused myself.
I have a page with information that is dynamically generated with PHP. I am trying to use jQuery to hide and reveal the information. However, when I execute the code the function only works on the first instance.
Here's the html/php code:
<div id="container">
<?php
foreach ($datas as $name)
{
if ($name['state'] === 'PA')
{
echo
'<input type="hidden" name="id" value="' . $name['id'] . '" />' .
'<h1 id="name">' . htmlentities($name['name']) . '</h1>' .
'<p id="descriptionlist">' .
htmlentities($name['description']) . ' ' .
'<br />' .
'<ul id="link">' .
'<li class="l1">' .
'' . $name['sname'] . '' .
'</li>' .
'</ul>' .
'</p>' .
'<div id = "locBar' . $name['id'] . '">' . '<div id="locText' . $name['id'] . '">' .
'<h2 id="location">Location</h2>' .
'</div>' . '</div>' .
'<div id="locDiv' . $name['id'] . '">' .
Here's the jQuery script:
$(document).ready(function(){
$('#locBar').click(function(){
$('#locDiv').slideToggle('slow')
})
});
Obviously, jQuery doesn't understand that if I am clicking the div in the second iteration, that anything should happen. How do I fix this?
What does the generated html look like?
Im guessing as this is the beginning of a foreach loop, you are generating multiple divs with the same ID (locBar and locDiv). You should never have multiple items with the same ID.
Change it to use class instead
Related
$sjb_attach_resume = '<div class="col-md-3 col-xs-12">'
. '<label for="applicant_resume">' . apply_filters('sjb_resume_label', __('Attach Resume', 'simple-job-board')) . '<span class="sjb-required required">*</span></label>'
. '</div>'
. '<div class="col-md-9 col-xs-12">
<div class="form-group">'
. '<input type="file" name="applicant_resume" id="applicant-resume" class="sjb-attachment form-control "' . apply_filters('sjb_resume_required', 'required="required"') . '>'
. '<span class="sjb-invalid-attachment validity-note" id="file-error-message"></span>'
. '</div>'
. '</div>'
. '<div class="clearfix"></div>';
echo apply_filters('sjb_attach_resume', $sjb_attach_resume);
I assume you didn't write the code, since you're asking this. Honestly and respectfully you should learn the basics before playing around with file uploads etc. as it can be a major security risk if not handled properly.
As for your question, you can remove the required-attribute completely from the file input:
$sjb_attach_resume = '<div class="col-md-3 col-xs-12">'
. '<label for="applicant_resume">' . apply_filters('sjb_resume_label', __('Attach Resume', 'simple-job-board')) . '<span class="sjb-required required">*</span></label>'
. '</div>'
. '<div class="col-md-9 col-xs-12">
<div class="form-group">'
. '<input type="file" name="applicant_resume" id="applicant-resume" class="sjb-attachment form-control ">'
. '<span class="sjb-invalid-attachment validity-note" id="file-error-message"></span>'
. '</div>'
. '</div>'
. '<div class="clearfix"></div>';
echo apply_filters('sjb_attach_resume', $sjb_attach_resume);
...or remove it by modifying the filter applied, by adding a code snippet to your theme's functions.php file:
add_filter( 'sjb_resume_required', '__return_empty_string' );
I do split the $post_content using <!--nextpage--> as separator, but maybe there is another method to obtain the number of pages of a single post because I'm doing it many times on large posts.
The WP_Post class doesn't have a member like $post_pages or something.
Use this for a link to previous posts with directional:
<?php previous_post_link(); ?>
Or this for a link to previous posts that without "<<":
<?php previous_post_link('<strong>%link</strong>'); ?>
The code needs to be inside the loop.
Or
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>
I'm developing a News website in Wordpress, and I need to show in a bootstrap carousel the first three posts, my problem is that I need to add the "active" class only at the first of the three elements, but really don't know how to. Here's my code:
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
?>
I've already tried a answer found on this site (this one):
$isFirst = true;
foreach ($recent_posts as $recent) {
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ": <strong>" .$recent["post_title"] . '</strong></a></div>';
$isFirst = false;
?>
but it just printed me the "active" words.
Thanks for your help
You need to set $i so that you can count how many times you have gone through the loop and do some logic with it, like in my example below. Instead of having two lines of code that are nearly identical like I have done below though, you should be able to do the if conditional right around the class active. I didn't do that so you could clearly see the conditional and the count of the loops through the array.
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
$i = 0;
foreach ($recent_posts as $recent) {
if ($i == 0) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
} else {
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
$i++;
}
?>
Can I have the following code:
foreach ( $categories as $category ) {
echo '<div class="wwd">
<img src="/images/' . $category->name . $category->term_id . '.jpg" alt="' . $categories->category_name . '" />
<div class="wwd-title">' . $category->name . '</div><br/>
</div>';
}
I am trying to add this code before /images/
<?php bloginfo('template_directory'); ?>
At present the code generates the img src="/images/..." but I want the sites url to go before the /images, when I have tried it I cant seem to get it to either work or display the correct path. The problem seems to with the apostrophe types used but I cant seem to figure out what should go where.
Thanks
Paul
the wordpress bloginfo function outputs the data to the browser. Instead use get_template_directory_uri to save the result to a variable:
$templateurl = get_template_directory_uri();
foreach ( $categories as $category ) {
echo '<div class="wwd">
<img src="' . $templateurl . '/images/' . $category->name . $category->term_id . '.jpg" alt="' . $categories->category_name . '" />
<div class="wwd-title">' . $category->name . '</div><br/>
</div>';
}
bloginfo will try to echo out the value of the parameter specified so you should try another method of just getting the information you want it. Say for example bloginfo returned your template directory you could do this:
foreach ( $categories as $category ) {
echo '<div class="wwd">
<img src="' . bloginfo('template_directory') . '/images/' . $category->name . $category->term_id . '.jpg" alt="' . $categories->category_name . '" />
<div class="wwd-title">' . $category->name . '</div><br/>
</div>';
}
The get_template_directory function might be more appropriate: http://codex.wordpress.org/Function_Reference/get_template_directory
I'm building a website with a vertical drop-down menu. One of the menu items should contain an image and the menu title.
Joomla puts the image before the text, in the following way:
<li class="item-153 current active"><a class="menu_immagine" href="whatever" ><img src="whatever.png" alt="whatever" /><span class="image-title">whatever</span></a></li>
What I'd like to do is putting the text before the image, like this:
<li class="item-153 current active"><span class="image-title">whatever</span><a class="menu_immagine" href="whatever" ><img src="whatever.png" alt="whatever" /></a></li>
How can I do this in Joomla?
Thank you very much for your help...
Thanks to Lodder, I made a template override. Then I modified the file myTemplate/html/mod_menu/default_component.php.
I changed this
if ($item->menu_image)
{
$item->params->get('menu_text', 1) ?
$linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" /> <span class="image-title">' . $item->title . '</span> ':
$linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}
else {
$linktype = $item->title;
}
to this:
if ($item->menu_image)
{
$item->params->get('menu_text', 1) ?
$linktype = '<span class="image-title">' . $item->title . '</span> <img src="' . $item->menu_image . '" alt="' . $item->title . '" /> ':
$linktype = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}
else
{
$linktype = $item->title;
}
That's it! That was so easy, after all, that's a matter of knowing how to do it! ;)