HMTL original rendered:
<div class="col-md-4">
<div class="row">
3 - Blog Post
</div>
<div class="row">
<img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />
</div>
</div>
HTML after setup_postdata( $post );
<div class="row">
<div class="col-md-4">
<div class="row">
<a href="http://localhost/PHP/wordpress/3-blog-post/">
3 - Blog Post </a>
</div>
<div class="row">
<img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />
Bem-vindo ao WordPress. Esse é o seu primeiro post. Edite-o ou exclua-o, e então comece a escrever!
</div>
</div>
Weird things:
The code:
<div class="container">
<div class="row">
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post)
{
?><div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']); ?>">
<?php echo $post['post_title']; ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div><?php
}
wp_reset_query();
?>
</div>
</div>
</br>
</br>
<div class="container">
<div class="row">
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']) ?>">
<?php echo $post['post_title'] ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
</div>
<div class="row">
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
</div>
When I change the position of the code and remove title and thumbnail, the code works well:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
I call the recent posts in my wordpress / woocommerce page, but when I call the excerpt, it didn't appear, title and thumbnail are ok. I have worked on it all day long, but I couldn't figure out what happen. When I change "echo $my_excerpt" with a simple word, it works. The problem should be when I put the content in a array. I had followed the documentation as you can see:
https://codex.wordpress.org/Function_Reference/get_the_excerpt
Examples
get_the_excerpt() can be used to retrieve and store the value in a variable, without outputting it to the page.
<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
My code:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<div class="col-md-4">
<div class="row">
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo $post['post_title'] ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
</div>
Edit: I have altered your code try using this one.
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post)
{
setup_postdata( $post ); // Edit: This will force wordpress to setup the data
?><div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']); ?>">
<?php echo $post['post_title']; ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div><?php
}
wp_reset_query();
?>
Try this. I have provided post id to your get the excerpt.
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
Well, after a plenty of options, tests, feedbacks and research, I've found a way to make it.
Thank you everyone!
My final code:
<!-- Testando novo formato -->
<p class="display-4" style="text-align:center">Testando - Posts Recentes</p>
<div class="container">
<div class="row">
<?php
query_posts( array('posts_per_page'=>3) );
while ( have_posts() ) : the_post();
?>
<div class="col-md-4">
<div class="card" style="width: 20rem; margin-bottom:3rem; margin-top:3rem;">
<img class="card-img-top img-fluid" src="<?php the_post_thumbnail(); ?>">
<div class="card-block">
<h4 class="card-title"><?php the_title(); ?></h4>
<p class="card-text">
<?php
the_excerpt();
?>
</p>
<a class="btn btn-primary" href="<?php the_permalink(' ') ?>">Ler o post</a>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query(); // resets main query
?>
</div>
</div>
Related
i have a custom taxonomy genre, i want to show all taxonomy in genre.php page. when I try to use this code php echo $term->slug;
the result does not meet my expectations :
http://localhost/site/action
I want the result like this :
http://localhost/site/genre/action
I don't know where the error is, I've been looking but haven't found a solution.
this is my code genre.php
<?php
/*
Template Name: Genre
*/
get_header(); ?>
<div class="content">
<div class="main-content">
<div class="main-container">
<div id="list_categories_categories_list">
<?php get_template_part( 'template-parts/ads-bottom' ); ?>
<div class="headline">
<h1>
Genre
</h1>
</div>
<div class="box">
<div class="list-categories">
<div class="margin-fix" id="list_categories_categories_list_items">
<?php
$terms = get_terms( array(
'taxonomy' => 'genre',
'hide_empty' => false,
'number' => 20
) );
foreach ($terms as $term){ ?>
<?php $image = get_term_meta( $term->term_id, 'image', true ); ?>
<a class="item" href="<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
<div class="img">
<?php if ( $image != '' ) {
echo wp_get_attachment_image( $image, "", ["class" => "thumb"]);
}
?>
</div>
<strong class="title"><?php echo $term->name; ?></strong>
<div class="wrap">
<div class="videos">0 videos</div>
<div class="rating positive">
81%
</div>
</div>
</a>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
get_footer();
You can use this code it will give results as per your requirement.
<?php
/*
Template Name: Genre
*/
get_header(); ?>
<div class="content">
<div class="main-content">
<div class="main-container">
<div id="list_categories_categories_list">
<?php get_template_part( 'template-parts/ads-bottom' ); ?>
<div class="headline">
<h1>
Genre
</h1>
</div>
<div class="box">
<div class="list-categories">
<div class="margin-fix" id="list_categories_categories_list_items">
<?php
$terms = get_terms( array(
'taxonomy' => 'genre',
'hide_empty' => false,
'number' => 20
) );
foreach ($terms as $term){ ?>
<?php $image = get_term_meta( $term->term_id, 'image', true ); ?>
<a class="item" href="<?php echo get_term_link($term->term_id); ?>" title="<?php echo $term->name; ?>">
<div class="img">
<?php if ( $image != '' ) {
echo wp_get_attachment_image( $image, "", ["class" => "thumb"]);
}
?>
</div>
<strong class="title"><?php echo $term->name; ?></strong>
<div class="wrap">
<div class="videos">0 videos</div>
<div class="rating positive">
81%
</div>
</div>
</a>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
get_footer();?>
The PHP code seems to be blocking or interfering with my advanced custom fields as it doesn't display unless I remove the PHP code then it does. I can't figure out where the issue is.
Any help is much appreciated.
PHP
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) {
$newsCat = 'cat=5,7&showposts=3';
} else {
$newsCat = 'cat=6,8&showposts=3';
}; ?>
Advanced Custom Fields
<div class="carousel-item active">
<div class="row py-5">
<?php if( have_rows('block') ): ?>
<?php while( have_rows('block') ): the_row();
// vars
$content = get_sub_field('content');
?>
<div class="col-lg-4 col-md-4">
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) { ?> <!--Check if url contains the word "items" -->
<h2 class="fw-b c-blue mt-0">Ár bhFís</h2>
<?php } else { ?>
<h2 class="fw-b c-blue mt-0">Our Vision</h2>
<?php } ?>
</div>
<div class="col-lg-8 col-md-8">
<p class="c-blue mb-0"><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
Fixed it by just changing how News articles where added.
<div class="row pt-4 pb-3">
<?php
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'post'
);
// 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="col-lg-4 col-md-4 col-sm-6 mb-5">
<div class="w-100 mb-2 px-2">
<img class="w-100" src="<?php $featimage = the_post_thumbnail_url('news-image'); ?>" alt="">
<p class="text-muted mt-4 mb-2"><?php echo get_the_date('dS M, Y'); ?></p>
<h3 class="c-blue"><?php the_title(); ?></h3>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
I am creating a portfolio section.the live site is here, http://www.nayeemriddhi.info/testproject/portfolio/. There need three loop for showing item. But fact is that, when i open portfolio item, the right sidebar item showing item from the beginning, as i created the loop. but i want to show right sidebar item as a current item for the portfolio images. is there any idea for showing right sidebar item as a current item.
the code is below,
<?php
/*
Template Name: Portfolio
*/
get_header(); ?>
<!-- Banner -->
<section class="page-banner" >
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="page-title ">Portfolios</h1>
<span class="page-tag-line">See our recent works</span>
</div>
</div>
</div>
</section>
<div class="wave-divider-pages"></div>
<section>
<div class="container gal-container">
<?php
$args = array(
'post_type' => 'custom_portfolio',
'posts_per_page' => '-1',
);
// the query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) ?>
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<?php $globalID = get_the_id(); ?>
<!-- Item-->
<div class="col-md-4 col-sm-6 co-xs-12 gal-item">
<div class="box">
<a class="trigger" data-iziModal-open="#modal<?php the_ID(); ?>">
<img src="<?php echo get_post_meta(get_the_ID(), 'portfolio_image', true); ?>">
</a>
</div>
</div>
<!-- Modal-->
<div id="modal<?php the_ID(); ?>" class="iziModal portfolio" data-izimodal-title="Portfolio Title" data-izimodal-subtitle="Web Design" style="max-width: 1200px important;">
<div class="col_one_third p-20">
<?php
$args2 = array(
'post_type' => 'custom_portfolio',
'posts_per_page' => '6',
);
// the query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) ?>
<?php while ( $query2->have_posts() ) :
$query2->the_post() ; ?>
<div class="col_half p-10">
<a href="#<?php the_ID(); ?>-<?= $globalID; ?>" data-toggle="tab">
<img src="<?php echo get_post_meta(get_the_ID(), 'portfolio_image', true); ?>" class="portfolio-thumb"/>
</a>
</div>
<?php endwhile; wp_reset_query(); ?>
<div class="col_full p-10">
<div class="portfolio-links">
Launch Website
Request a Quote
</div>
</div>
</div>
<div class="col_two_third col_last">
<div class="tab-content ">
<?php
$args3 = array(
'post_type' => 'custom_portfolio',
'posts_per_page' => '-1',
);
// the query
$query3 = new WP_Query( $args3 );
// The Loop
if ( $query3->have_posts() ) ?>
<?php while ( $query3->have_posts() ) :
$query3->the_post() ; ?>
<div class="tab-pane active" id="<?php the_ID(); ?>-<?= $globalID; ?>">
<img src="<?php echo get_post_meta($globalID, 'portfolio_image', true); ?>" class="img-responsive"/>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
</div>
<!-- Item End-->
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
<section>
</section>
<div class="wave-divider-common"></div>
<?php get_footer(); ?>
Thanks for help...
Modify your html for first anchor tag as
<a class="trigger" data-iziModal-open="#modal<?php the_ID(); ?>" data-id="<?php the_ID(); ?>-<?= $globalID; ?>">
<img src="<?php echo get_post_meta(get_the_ID(), 'portfolio_image', true); ?>">
</a>
Add this JS
<script type="text/javascript">
$(document).ready(function(){
$('a.trigger').on('click', function (e) {
var getDataId = $(this).data('id');
$('.iziModal a[href="#' + getDataId + '"]').tab('show');
});
});
</script>
My current blog page shows all my blog posts in a grid of 3 by 'x'. However at the top I want to display the latest blog post as some sort of a featured post and thus style it a bit different (i.e full width). I tried doing it through css with :first-child but that didn't really work well. So now I'm trying the php approach. I however have no clue how to approach this. Can anyone show me where to start? This is my current code.
<section id="blogs" class="cards-list">
<div class="container cards">
<div class="row center-xs">
<?php
if(get_post_type() == 'post') {
$currentBlog = get_the_ID();
} else {
$currentBlog = '';
}
$loopBlog = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'post__not_in' => array($currentBlog)
));
while ( $loopBlog->have_posts() ) : $loopBlog->the_post();
$blogIntro = get_field('blog_intro');
$blogImage = get_field('blog_image');
$blogImageUrl = $blogImage['sizes']['large'];
?>
<div class="col col-xs-12 col-md-4">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="card card-event">
<figure style="<?php if($blogImageUrl != '') { echo "background-image:url('".$blogImageUrl."');"; } ?>"></figure>
<div class="content">
<span class="tag"><?php the_time('M d Y'); ?></span>
<div class="link"><h3><span><?php the_title(); ?></span></h3></div>
</div>
</a>
</div>
<?php
endwhile; wp_reset_query();
?>
</div>
</div>
You should be able to use current_post inside the loop and output different markup for the first post:
while ( $loopBlog->have_posts() ) : $loopBlog->the_post();
$blogIntro = get_field('blog_intro');
$blogImage = get_field('blog_image');
$blogImageUrl = $blogImage['sizes']['large'];
?>
<?php if ($loopBlog->current_post == 0): ?>
<!-- Output some other markup for the first post here -->
<div class="container-fluid">
</div>
<?php else: ?>
<div class="col col-xs-12 col-md-4">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="card card-event">
<figure style="<?php if($blogImageUrl != '') { echo "background-image:url('".$blogImageUrl."');"; } ?>"></figure>
<div class="content">
<span class="tag"><?php the_time('M d Y'); ?></span>
<div class="link"><h3><span><?php the_title(); ?></span></h3></div>
</div>
</a>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
I'm trying to use the Infinite Scroll plugin for Wordpress. I set it up and it doesn't work.
Screen of settings:
Template file
<div id="posts-wrapper" class="row main">
<?php $counter = 1; ?>
<?php if ( have_posts() ){ while ( have_posts() ){ the_post(); ?>
<?php $url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
<div class="single-post <?php if( $counter == 5 ) { echo "col-xs-12 col-sm-12 col-md-8 col-lg-8"; } else { echo "col-xs-12 col-sm-6 col-md-4 col-lg-4";} ?>">
<div class="thumbnail">
<div class="category-wrapper"><?php the_category(', ');?></div>
<a class="href-overlay" href="<?php the_permalink(); ?>"><div class="wrapper">
<div class="<?php if( has_term( 'hot', 'hot', $post->ID ) ){ echo "inside inside-hot"; } else { echo "inside";} ?>"><div class="image-cover" style="background-image: url(<?php echo $url[0]; ?>)"></div></div>
</div></a>
<div class="post-wrapper">
<p class="post-created-by">Napisane przez <strong><span class="special"><?php echo get_the_author(); ?></span></strong> | <i class="glyphicon glyphicon-comment"></i> <?php comments_number( '0' , '1' , '%' ); ?></p>
<h2 class="post-title"><?php the_title(); ?></h2>
</div>
</div>
</div>
<?php $counter++ ; ?>
<?php }else{ ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
</div>
<div id="paginacja">
<?php next_posts_link('wczytaj więcej »')?>
</div>
What am I doing wrong?