on my WordPress front-page.php theme-page i use the WordPress Loop to show a number of Youtube Videos. I use acf to get the Youtube-URL and create the iframe from the url with the function convertYoutube. Now when i execute the loop each item is showing 5 videos (number of posts im querying) and each video, also the videos from the other posts. What am i doing wrong?
<?php get_header(); ?>
<main>
<div class="inner">
<div class="video-wrapper">
<?php
// video function
function convertYoutube($string) {
return preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
"<iframe src=\"//www.youtube.com/embed/$2?rel=0\" allowfullscreen></iframe>",
$string
);
}
// args
$args = array(
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => 5,
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="video">
<?php
$title = get_the_title();
echo $title;
$videoURL = get_field('video_url');
$videoEmbedCode = convertYoutube($videoURL);
echo $videoEmbedCode;
?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
</div>
</main>
Related
Main string of code (that doesn't work):
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3,
'type_of_site' => 'landing_page_type',
);
$webTariffs = new WP_Query($args);
while ($webTariffs->have_posts()) {
$webTariffs->the_post();?>
<div class="pricing-item">
<div class="pricing-item-header">
<h3><?php the_title() ?></h3>
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
</div>
</div>
<?php } ?>
The ways i tried to solve this problem:
put insted of the_field('tariff_price_kn') - echo('hi') - the code worked
add $post_id -> the_field('tariff_price_kn', $post_id)
$currencyKune = get_field('tariff_price_kn'); And then echo $currencyKune
P.S 3) i don't know exactly where should i put $currencyKune = get_field('tariff_price_kn'), so i put it before while at first time - doesn't work and at the second time i put it after $webTariffs->the_post();
Try this :
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-6">
<h2 class="the-title"><?php the_field('tariff_price_kn', $post->ID); ?> + <?php the_title() ;?> </h2>
</div>
<?php endwhile; else: ?> Nothing here <?php endif; ?>
<?php wp_reset_query(); ?>
I am working on a website that is based on Wordpress. My client wants to display the latest post snippet under the header on the main page. I was able to do that but instead of fetching the post's title in the loop it is fetching the page title and displaying it there.
Here is my code :
<?php
$postslist = get_posts('numberposts=1&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?><?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have also tried get_the_title(); but it did not work as well.
The SITE ITSELF
From wordpress codex
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
$recent_posts = wp_get_recent_posts(array('numberposts' => 1);
foreach( $recent_posts as $recent )
echo $recent["post_title"];
if you want to use the loop
$args = array('posts_per_page' => 1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?> <?php the_excerpt(); ?>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please use this:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
<div class="entry">
<h3><?php echo $recent['post_title'] ?></h3>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
<?php echo get_excerpt($recent['ID']); ?>
</div>
}
?>
It worked. There is a plugin called smart post lists that was not letting it work. Now it is working fine.
It was my mistake to not disable it and test this. My code is correct.
I need to display the content of child pages on a parent page in Wordpress (not posts). This works, displays the title of the child page, but I can not get the content to display. (last 2 lines)
global $post;
$args = array( 'numberposts' => 1, 'category_name' => 'foundation','post__in' => get_option( 'sticky_posts' ) );
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) {
$the_query->the_post();
?>
<article class="lead">
<h1><?php the_title(); ?></h1>
<section>
<?php the_content(); ?>
</section>
</article>
<?php
}
$newQuery = new WP_Query();
$all_pages = $newQuery->query(array('post_type' => 'page'));
$foundation = get_page_by_title('Foundation');
$foundation_children = get_page_children($foundation->ID, $all_pages);
query_posts('pagename=foundation');
while(have_posts()) {
the_post();
the_title();
the_content();
}
foreach($foundation_children as $tc) {
echo $tc->post_title; // this works
echo $tc->the_content; // this doesn't
}
The correct variable is post_content.
Try echo $tc->post_content;
Reference: http://codex.wordpress.org/Class_Reference/WP_Post
[SOLVED]
I'm trying to integrate Flex Slider (flexslider.woothemes.com) with WordPress. I've included all JS/CSS files needed.
Then, I added this code to show the featured images of a certain category - the famous WP_Query with $args set to a certain category. Then using echo the_post_thumbnail(); I'm showing the posts' images. It's working fine. I just need to make the images linked to post URLs (imgs are hrefed). Please provide help and thanks in advance.
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider ?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php }
// The Slide's Image
echo the_post_thumbnail();
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php
// Reset Post Data
wp_reset_postdata();
?>
New code that's working:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider
?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php }
// Reset Post Data
wp_reset_postdata();
?>
If I understand what you're asking, I think the answer is
echo '<a href="'. the_permalink() .'">';
echo the_post_thumbnail();
echo '</a>';
I created a custom page template to display the latest 12 posts with their respective title and excerpt, but I tought that It would be easier if I could call this with a shortcode.
This is the loop in "post-grid.php" which calls to those 3 things.
<section class="post-grid">
<?php
$grid = array('post_per_page' => 12);
$query = new WP_Query( $grid );
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="grid-posts">
<h2><?php the_title(); ?></h2><br>
<?php the_post_thumbnail('featured'); ?><br>
<?php the_excerpt() ?><br>
</div>
<?php endwhile; // end of the loop. ?>
</section>
How can I create a shortcode that executes that loop?
I know how to add a shortcode using
add_shortcode('postGrid', 'postGrid');
function postGrid()
{
//Code here
}
But im not sure how to implement the above as a function. I appreciate your help!
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $page,
);
query_posts($args);?>
hope this will help you :)
Point related to add Post Thumbnail:
// check if the post has a Post Thumbnail assigned to it.
<?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_content(); ?>
Hope this help you :)
Since you aren't editing any code - you are creating your own - just put all that code in the call back function as is and it should work.
add_shortcode('postGrid', 'postGrid');
function postGrid()
{
<section class="post-grid">
<?php
$grid = array('post_per_page' => 12);
$query = new WP_Query( $grid );
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="grid-posts">
<h2><?php the_title(); ?></h2><br>
<?php the_post_thumbnail('featured'); ?><br>
<?php the_excerpt() ?><br>
</div>
<?php endwhile; // end of the loop. ?>
</section>
}