I am trying to retrieve data through my template in wordpress using get_post_meta() function. This works fine if I put the code above the div which is using a javascript, but it doesnt work if I put this function after the java script.
My php template file is as follows.
Here is the div using the javascript file ( lets call it div 1)`
<div style="width: 100%;margin: 0 auto; clear: both; ">
<div style="width: 800px">
<ul class='tabs'>
<?php $the_query = new WP_Query( 'showposts=3' ); ?>
<?php
$i=1;
while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href='#tab<?php echo $i; ?>'><?php the_title(); ?></a></li>
<?php $i++; endwhile;?>
</ul>
</div>
<div class="clear"></div>
<div style="background:#d0d8e0; position:relative; width: 96%; margin: -1% 4% 4% 2%; padding: 40px 0 20px 0; ">
<?php $the_query = new WP_Query( 'showposts=3' ); ?>
<?php
$i=1;
while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div id='tab<?php echo $i; ?>'>
<div class="tab" >
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
</div>
<?php $i++; endwhile;?>
</div>
<div class="clear"></div>
`</div></div>
And the code I am putting after this div to retrieve values from data base is( lets call it div 2)
<div class="group group-custom" >
<?php $url = get_post_meta($post->ID,'wpcf-youtube-url', true); ?>
<div style="float: left">
<iframe type="text/html" width="480" height="315" <?php echo wp_oembed_get( 'http://www.youtube.com/watch?v=' . $url ); ?></iframe>
</div>
<div class="video-area">
<h2><?php echo "" .get_post_meta($post->ID,'wpcf-video-line',TRUE)?></h2>
<br/>
<p><?php echo "" .get_post_meta($post->ID,'wpcf-video-text',TRUE)?></p>
<a class="button" href=""></a></div></div>
If I put the div 2 after div 1 in my php template, it doesn't work but if I switch the divs, it works normally. Here is my javascript file ( which I dont see a problem)
<script language="JavaScript">
$(function() {
$('ul.tabs').each(function(){
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');
// If the location.hash matches one of the links, use that as the active tab.
// If no match is found, use the first link as the initial active tab.
$active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active.attr('href'));
// Hide the remaining content
$links.not($active).each(function () {
$($(this).attr('href')).hide();
});
// Bind the click event handler
$(this).on('click', 'a', function(e){
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();
// Update the variables with the new link and content
$active = $(this);
$content = $($(this).attr('href'));
// Make the tab active.
$active.addClass('active');
$content.show();
// Prevent the anchor's default click action
e.preventDefault();
});
});
});
The live version of this work is http://cmi.pixstreammedia.com.s177734.gridserver.com/
I found the problem. I have to use <?php wp_reset_query() ?> after the new wp_query function is over to restores the $wp_query and global post data to the original main query.
http://codex.wordpress.org/Function_Reference/wp_reset_query
Related
I am making a shortcode of a slider that shows my WP featured posts and that has the possibility to open the details of each post through a hidden div. The slider is based on this one from w3schools (from which you can see the code and test online from this address: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow
The problem is that the slider is only visible in php once I click the next arrow and from there it works correctly. I don't know why, since in HTML it works fine.
The error apppears: Cannot read property 'style' of undefined in
slides[slideIndex-1].style.display = "block";
My CSS:
#slider {
width: 70%;
margin: auto;
position: relative;
font-size: 15px;
}
.slide {display: none;}
My PHP:
<?php $query = new WP_Query( $args );
ob_start();?>
<div id="slider" class="slider">
<div class="arrows">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a></div>
<?php
while( $query->have_posts() ) :
$query->the_post();
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post-
>ID), '');
$featuredID = get_post_thumbnail_id();?>
<div class="slide fade" id="slide">
<h2 class="zz-sc-jscss"><?php the_title(); ?></h2>
By <?php the_author(); ?> on <?php echo get_the_date( 'l, F d, Y' );
?>
<?php echo the_post_thumbnail();?>
<?php
echo "<div"; echo ' class="n"'; echo "id=".$featuredID.">";
$info = $featuredID;echo "</div>";
?> <div class="open">
<?php echo get_post_field('post_content', $post->ID);?>
<span class="cerrar">X</span></div></div>
<?php endwhile;
wp_reset_postdata();
return ob_get_clean();
}
My JS:
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("slide");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";
}
The problem seems to be in the "slider" div, it does not seem to be closed. Also try moving it after the slide generation loop.
<div id="slider" class="slider"></div> <--missing
<div class="arrows">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
Sorry, I'm just a complete novice in PHP
Background: This code is a loop of three slider as you can see here - http://rashelectrical.com.au/. I need to make the H1 tag only on the first slide and make the other two H2 tag
Question:
How can I make the 2nd and last loop to H2 and H1 only on the first
loop?
Thanks
Here is my code:
https://jsfiddle.net/4jm5vhbf/
<?php while(have_rows('banner_slider')) : the_row();?>
<div class="sl" >
<div class="header-content relative-block align-center text-center flex-container align-middle" style="background:url('<?php the_sub_field('image');?>') no-repeat">
<div class="row">
<div class="columns">
<div class="header-content-inner relative-block animate-children">
<h3><?php the_sub_field('subheading');?></h3>
<h1><?php the_sub_field('heading');?></h1>
<p><?php the_sub_field('description');?></p>
<div class="button-group align-center">
<?php $n = 1; while(have_rows('button_links')) : the_row();
$link_lr = get_sub_field('link');?>
<?php if($n == 2){
$n2 = 'hollow';
}else{
$n2 = '';
}?>
<a class="button <?php echo $n2; ?>" href="<?php echo $link_lr['url']; ?>"><span><?php echo $link_lr['title']; ?></span></a>
<?php $n++; endwhile;?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;?>
Its kinda hard to tell without seeing the full code, maybe something like that?
<!-- Instead of H3 tag here, use P tag with css class and change the font size with css -->
<h3><?php the_sub_field('subheading'); ?></h3>
<!-- count the heading elements -->
<?php $headings = count( get_sub_field( 'heading' ) ); ?>
<!-- Condition here if heading 1 echo H1 else echo H2 -->
<?php echo ($headings == 0) ? '<h1>' . the_sub_field('heading') . '</h1>' : '<h2>' . the_sub_field('heading') . '</h2>' ?>
<p><?php the_sub_field('description'); ?></p>
<!-- Your rest of code -->
Also for SEO and accessibility reasons, your HTML markup should considering the "Semantic" of your Heading tags .. so don't use H3 tag before H1 tag.
Following is the code that retrieves and outputs state name from custom select field into a navigation menu. For eg: if there is 5 post under state New York, this code shows only one post of New York in navigation drop down menu as redundant values are removed.. but what i want to do is , when the user clicks new york, it redirects to a new page where all the posts under New York will be displayed.. Same with all the states as well because this is a dynamic menu
$args = array('post_type' => 'article-form', 'showposts' => -1, 'orderby' => 'date', 'order' => 'DSC');
$city = new WP_Query($args);
$states = [];
while ( $city->have_posts() ) :
$city->the_post();
$state = get_field('state');
if ( !in_array($state, $states) ) :
$states[] = $state;
?>
<li>
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #9c9c9c;">
<?php echo $state;
?>
</a>
<hr style="margin: 0">
</li>
<?php
endif;
endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
Try pushing unique values into an array
<?php $stateArray = array(); ?>
<?php while ($city->have_posts()):$city->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #9c9c9c; ">
<?php
$state = the_field('state');
if (!in_array($state, $stateArray)):
echo $state;
array_push($stateArray, $state);
endif;
?>
</a>
<hr style="margin:0">
</li>
<?php endwhile; ?>
I hope it helps!
P.S Try to avoid inline css, use and external css file for any kind of stylining. Keep you html files as clean as possible.
#user3127632's idea of pushing values to an Array to check if they've been printed before is a good one, but there are a couple of mistakes in his/her code.
If you need to store an ACF's field value to a variable, you should use get_field() instead of the_field() as the last one doesn't return the value but print it. Also, you need to move the li and a nodes inside the condition, otherwise you won't be echoing the label in your link, but the duplicated link will still be there...
So, it would be something like this:
<?php
$states = [];
while ( $city->have_posts() ) :
$city->the_post();
$state = get_field('state');
if ( !in_array($state, $states) ) :
$states[] = $state; // A more efficient way to push values to an array
?>
<li>
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #9c9c9c;">
<?php echo $state; ?>
</a>
<hr style="margin: 0">
</li>
<?php
endif;
endwhile;
?>
I hope this helps!
My homepage has a vertical slider that displays the titles of 5 posts at the top of my homepage. Each post that displays has a background image that is shown behind it. It is currently working well, although I would to display a different background image behind each post display. I have no clue as to how to do this using PHP code. I have posted the code below that shows the file that contains the JCarousel functionality. I'm guessing I need to setup some type of condition that allows a new background image to display with each 'list' item?
Here's the code:
<div id="home-gallery-wrapper">
<ul id="home-gallery" class="jcarousel-skin-home clearfix">
<?php
global $post;
if ( get_option('minimax_hl_show') == '1' ) {
$my_query = get_pages('include='.get_option('minimax_hl_ID').'');
} else {
$my_query = get_posts('include='.get_option('minimax_hl_ID').'');
}
foreach($my_query as $post) :
setup_postdata($post);
?>
<li style="width:910px"><div style="
background:url('<?php bloginfo('template_directory'); ?>/images/banner3.png')
center top no-repeat;">
<table align="center" style="width:910px;
height:200px">
<tr>
<td style="vertical-align:top">
<div style="width:90%; display:block; margin-left:auto; margin-right:auto">
<h2 style="text-align:center; border:none; text-decoration:none; font-family:calligraffiti;
margin-top:15px; font-size:1.8em; line-height:35px; color:#fff; position:relative; z-index:10">
<a style="color:#fff; border:none; text-decoration:none"
title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink(); ?>"
rel="bookmark"><?php the_title(); ?></a></h2>
</div>
</td></tr>
</table></div>
<?php
$key="show-video";
if ( get_post_meta($post->ID, $key, true) != '' ) {
?>
<div class="video"><?php echo get_post_meta($post->ID, $key, true); ?></div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("embed").attr("wmode", "transparent");
});
</script>
<?php
} else if ( has_post_thumbnail() ) {
?>
<a title="Permanent Link to <?php the_title(); ?>"
href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
<?php } ?>
</li>
<?php
endforeach;
?>
</ul>
</div><!-- end home-gallery-wrapper -->
<script type="text/javascript">
function mycarousel_initCallback(carousel)
{
// Disable autoscrolling if the user clicks the prev or next button.
carousel.buttonNext.bind('click', function() {
carousel.startAuto(0);
});
carousel.buttonPrev.bind('click', function() {
carousel.startAuto(0);
});
// Pause autoscrolling if the user moves with the cursor over the clip.
carousel.clip.hover(function() {
carousel.stopAuto();
}, function() {
carousel.startAuto();
});
};
jQuery(document).ready(function() {
jQuery('#home-gallery').jcarousel({
vertical: true,
scroll: 1,
easing: 'easeInOutBack',
auto: 2,
wrap: 'last',
initCallback: mycarousel_initCallback,
animation: 800
});
});
</script>
Create 5 different banners with the colours of your choice and call them banner1.png, banner2.png, banner3.png etc.
Then replace
foreach($my_query as $post) :
setup_postdata($post);
?>
<li style="width:910px"><div style="
background:url('<?php bloginfo('template_directory'); ?>/images/banner3.png')
center top no-repeat;">
With this
$i = 0;
foreach($my_query as $post) :
setup_postdata($post);
?>
<li style="width:910px"><div style="
background:url('<?php bloginfo('template_directory'); ?>/images/banner<?php $i++; ?>.png')
center top no-repeat;">
I have a parent page that acts as menu for my portfolio.
It pulls in thumbnail images from the child pages which i have been able to accomplish with magic fields and some code. It dumps the images into a grid layout. The thumbnails are pulled into one container div like so:
div id="folio-content">
<div class="thumb-container">
<div class="thumb"><img src="/images/pic.jpg"/>
</div>JCPenny</div>
... </div>`
when the div gets filled up with 2 thumbnails I want to create a new container div and fill it with 2 images again and so on after 2 images.
So, if you had 4 images it would look like this.
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
this is the code I am using in my page.php file.
<?php get_header(); ?>
<div id="folio-content">
<?php
$projectpage = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($projectpage as $page)
{
$content = $page->post_content;
if(!$content)
continue;
if ($count == 10) --- this is geting 10 images now, but I want to get them all.
break;
$count++;
$content = apply_filters('the_content', $content);
?>
<div class="thumb-container">
<div class="thumb"><a href="<?php echo get_permalink($page->ID); ?>"<?php echo get_image ("thumbnail",1,1,1,$page->ID);?></a>
</div><?php echo $page->post_title ?>
</div>
<?php
}
?>
</div><!--/close set!-->
</div>
also, how can I get ALL child pages? I have it set to 10 now with this if ($count == 10)
any help? thanks a ton again!!!!
I'm not familiar with "get_pages" but since Wordpress treats posts and pages in an identical manner you could use this.
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
The -1 removes the limit and gets ALL the specified pages.
I have cobbled together some code, that sort of sounds right but does not work at all! Which I am not surprised. But it is a starting point - please take a look at this code, maybe it is step in the right direction?
<?php
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
if (have_posts()) :
$i=0; // counter
while(get_posts()) : the_post();
if($i%2==0) { // if counter is multiple of 3, put an opening div ?>
<!-- <?php echo ($i+1).'-'; echo ($i+2); ?> -->
<div>
<?php } ?>
<div class="single_item">
<h2><?php the_title(); ?></h2>
</div>
<?php $i++;
if($i%2==0) { // if counter is multiple of 3, put an closing div ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php
if($i%2!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>
<?php endif; ?>