I have a post loop (index.php) which shows three post links as headers and above them there's a slideshow based on the slug of the post.
<div class="nuotraukos">
<?php if (have_posts()) : ?>
<?php $nuotraukos = new WP_Query('category_name=nuotraukos&showposts=3');
while ($nuotraukos->have_posts()) : $nuotraukos->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="post" id="post-<?php the_ID(); ?>">
<p class="postmetadata"><?php edit_post_link(__('Edit')); ?></p>
<div class="entry">
<?php
if ( function_exists( 'meteor_slideshow' ) ) {
$slug = basename(get_permalink());
meteor_slideshow('' . $slug. '');
}
the_content('<h2>' . get_the_title() . '</h2>');
?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
Meteor slideshow has a loop itself and I assume that it cancels the post loop after the first query. That's why it prints out only one title (but displays it three times) you can see it in http://studioglamour.co.uk.
The thing is i need three different links displayed below slideshows, but don't know how to fix this.
Try
$slug = basename(get_permalink($nuotraukos->post->ID));
...
the_content('<h2>' . get_the_title($nuotraukos->post->ID) . '</h2>');
Related
I want to display one post at a time inside a div and replace the content to display the next (adjacent) post's content on the click of a button.
I am currently looping through the posts using wp_query, but all posts are displayed stacked on top of each other.
query_posts($args);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
$count = $wp_query->post_count;
$i = 1;
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() && $i < 9) : $wp_query->the_post();
?>
I am pulling in the following content:
<?php echo $i?> / <?php echo $count?>
<?php the_title();?>
<?php the_content();?>
I also have a conditional surrounding the button to pull in the adjacent link's permalink, but it is grabbing the entirety of the permalink when I only need the post's slug. I'm imagining something like this, but it doesn't work:
$next_slug = get_slug(get_adjacent_post(false, '', false));
This is essentially AJAX navigation through posts. This answer should be able to do what you're asking.
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title( '<h1>','</h1>' ); ?>
<div class="post-thumbnail"><?php the_post_thumbnail(array(250, 250)); ?> </div>
<div class="entry-content"><?php the_content(); ?></div>
<div class="btn"><?php next_post_link('%link', '%title'); ?></div>
</article>
<?php endwhile; ?>
</div>
Sup ,I have added 2 posts in BO , and trying to display title of posts with php , but it doesn't work , should i add any any additional function to functions php or not?
Instead of post title it display - "Home" - page title . where i wrong?
Here is some code
<div class="container">
<div class="row"id="blog">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-5"id="blog-post">
<div class="post-title"><?php echo get_the_title($post_id); ?></div>
<div class="post-txt"><?php echo get_excerpt(); ?></div>
</div>
<?php endwhile;?>
<?php endif; ?>
</div>
</div>
you don't need $post_id in your get_the_title() function because you are in a loop. In which file is this code ? Maybe you are not in a query.
Try this before your if statement and show me what you get :
<?php
$queried_object = get_queried_object();
var_dump($queried_object);
?>
I suggest this code.
if ( have_posts() ) {
// Load posts loop.
while ( have_posts() ) {
the_post();
?>
<div class="post-title"><?php echo get_the_title(); ?></div>
<?php
}
}
I am trying to get latest posts to display through a template page i am building for pages, the loop is not running the latest post only one page
ok, I have a simple loop that gets latest post
my loop
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
and content.php
<div class="blog-post">
<h2 class="blog-post-title">
<?php the_title(); ?>
</h2>
<p class="blog-post-meta">
<?php the_date(); ?>by <?php the_author(); ?>
<a href="<?php comments_link(); ?>">
<?php printf(_nx('One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain'), number_format_i18n(get_comments_number())); ?>
</a>
</p>
<?php if ( has_post_thumbnail() ) {?>
<div class="row">
<div class="col-md-4">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="col-md-6">
<?php the_excerpt(); ?>
</div>
</div>
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
</div>
when i run the loop in index.php i get my latest blog post, perfect.
however, i am building a template page, i try and include the loop in this page, i just get one page (not all posts).
my template
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()) ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
If you are using multiple loops on the same page, you must use rewind_posts() like so:
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()); ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
<?php rewind_posts(); ?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
This "resets" the loop to it's original state and allows you to look through the posts again. In your original code you scan through all the posts, and then in your second loop scan through nothing, as you have already scanned through all the posts!
Hmm I have found this solution using a for each rather than the while loop seems to work, but im not sure if its the best way around.
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
</ul>
UPDATE
<?php
$args = array('numberposts' => 5);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
$excerpt = wp_trim_excerpt($recent['post_content']);
$permalink = get_permalink($recent["ID"]);
$title = esc_attr($recent["post_title"]);
$thumbnail = get_the_post_thumbnail($recent["ID"], 'thumbnail');
echo '<li><a href="' . $permalink . '" title="Look ' . $title . '" >' . $thumbnail . $title . '</a></li>';
echo $excerpt;
}
?>
My Wordpress site has a loop that creates posts, and I want to target specific posts to change their css values.
html - index.php
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
content.php
<?php
if (has_post_thumbnail()) {
$thumbnail_data = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'my-fun-size' );
$thumbnail_url = $thumbnail_data[0];
}
?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>')" <?php post_class('container-fluid'); ?> >
<div class="container-fluid">
<div class="col-md-12 text-cell">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
It was suggested that I use 'get_post_meta ...' but I am not familiar with how to use it. I just want to change css values (padding, font-size, etc) for different posts
I would suggest using Custom Fields for this, so that you can define the values in the post itself, as opposed to having to edit the code every time you add a new post.
In the post, make sure you have Custom Fields visible (Togglable from screen options at the top)
Then make a field called "Alignment" or whatever you prefer and assign a value to it. (for example 'left')
Then you can add a conditional in the loop.
<?php $alignment = get_post_meta(get_the_ID(),'Alignment',true);
if($alignment) == 'left'):?>
<p>Do stuff and things here...</p>
<?php endif;?>
You can read more about it here: https://codex.wordpress.org/Custom_Fields
Hopefully that will work for you. If you want to get fancier with it, I would suggest looking at the Advanced Custom Fields plugin, which allows a lot more flexibility and options.
EDIT from comments:
1st option:
Set a field of "ExtraCSS" to "color:green;"
<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>'); <?php echo $extraCSS;?>" <?php post_class('container-fluid'); ?> >
2nd option:
(In your stylesheet:)
article:nth-child(2n+0)
{
color:green;
}
http://www.w3schools.com/cssref/sel_nth-child.asp
I am using the plugins and settings below. I am having some problems displaying my custom post types. I am concentrating on the printers custom post type. I have them displaying under the custom taxonomies section and have created 4 taxonomies (manufactures) so that bit is ok. (http://developmentscene.co/CKH/printers/)
The Problem
When I click on a post on the page above it displays all posts in that category. I just want it to display the post that the link you clicked on and only that post. I have tried to do this using ('posts_per_page' => 1,) But this bit of code just displays the same first post for each post I click on in the category page.
My Ideal Solution
So what I was thinking in my mind was to say only show one post with the ID of the link you clicked previously in the category. I dont know if this is the best way but I have put my code below.
<?php get_header(); ?>
<div id="wrap" class="container">
<section id="content" class="primary" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php $printers = new WP_Query(array(
'post_type' => 'printers',
'posts_per_page' => 1,
)); ?>
<?php while($printers->have_posts()) : $printers->the_post(the_ID()); ?>
<h2 class="post-title" style="color:#333;" ><?php the_title(); ?> - <?php echo the_ID();?></h2>
<hr >
<p><?php the_content(); ?></p>
<p> </p>
<?php the_post_thumbnail(); ?>
<br />
</section>
<section id="sidebar" class="secondary clearfix" role="complementary">
<h3 class="widgettitle" ><b>Features and Benefits</b></h3>
<?php $benefits = get_field( "features-and-benefits" );
if($benefits){
echo '<table cellspacing="0" cellpadding="0"><tbody>' ;
foreach($benefits as $benefits){
echo '<tr><td><p>' . $benefits['features'] . '</p></td></tr>' ;
}
echo '</tbody></table>';
}
?>
<br />
<h3 class="widgettitle" ><b>More Information</b></h3>
<?php $specifications = get_field( "specifications" );;
if($specifications){
echo '<table cellspacing="0" cellpadding="0"><tbody>' ;
foreach($specifications as $specifications){
echo '<tr><td><p>' . '<a target="_blank" href="http://' . $specifications['link'] . '/">' . $specifications['details'] . '</a></p></td></tr>' ;
}
echo '</tbody></table>';
}
?>
</section>
<?php endwhile; ?>
</div>
<?php get_footer(); ?>
Settings
Plugins Used:
* Custom Fields
* Custom Post Type UI
I also have permalinks set to postname.
You are running two loops here, and that is causing all the frustrations. I believe that you added the second loop for your specific needs, so can you can just simply delete the first loop, that is this section
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>