I have a PHP while loop as follow:
<?php while (have_posts()) : the_post(); ?>
<li>
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, large );
$alt_text_title = $attachment->post_title ;
//print_r($attachment);
echo "<img src=\"$image_attributes[0]\" alt=\"$alt_text_title\">";
}
}
?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</li>
<?php endwhile;?>
The a tag inside the h3 makes all the titles hyperlinks however I would like one of these title not be a link therefore not to be affected by the a tag at all..
Is this possible?
Edit. A safer way it to do it via post ID.
<?php $postID = $post->ID;
echo $postID;
// You could delete this line once you have the PostID.
if($postID == '1') { ?>
<h3><?the_title();?></h3>
<?php } else { ?>
<h3><?the_title();?></h3>
<?php } ?>
You could always check the content of the_title() with an if and display the a tag only when necessary.
<?php if(the_title() === "your title") {?>
<h3><?php the_title(); ?></h3>
<?php } else {?>
<h3><?php the_title(); ?></h3>
<?php } ?>
$title=get_the_title();
if( /*post meets some condition*/) {
?><h3>$title</h3><?php
} else {
?><h3><?php the_title(); ?></h3><?php
}
You just need a simple if statement. You can build the condition on any number of things in the example ill use the full title but it would probably be better to use the id of the post or something like that:
<?php if('Ignore this title' == get_the_title()): ?>
<h3><?php the_title() ?></h3>
<?php else: ?>
<h3><?php the_title(); ?></h3>
<?php endif; ?>
Related
So to elaborate, I have 11 posts and in each of those posts I input an image into advance custom fields form. But when I call them I get back 11 results but from just 1 post.
Here is what I'm working with. Just to tell you this is in functiuons.php since I want to get this as a shortcode so I can use it on multiple post types.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('slider') ): ?>
<?php while( have_rows('slider') ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I have almost identical code on the template I created, but that one works, this one does not and I don't know why.
Pass id of current post for the acf have_rows() function. I have done that using , $currentId = get_the_ID(); and then using the variable $currentId where I need.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post );
$currentId = get_the_ID();
?>
<?php if( have_rows('slider', $currentId) ): ?>
<?php while( have_rows('slider', $currentId) ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I had to add
global $post;
to the top of my function for it to work. Thanks for your help anyway guys.
I have issue with getting all posts with a category("case"), if I remove the 'cat' => $cat_id line i get all posts in posttype "Feed"
How do I get out only posts with category 'case'?
$cat_id = get_cat_ID('case');
var_dump($cat_id); //responce is 40
$args = array(
'post_type' => 'Feed',
'posts_per_page' => -1,
'cat' => $cat_id,
);
while ( $loop->have_posts() ) : $loop->the_post();
var_dump($loop);
endwhile;
wp_reset_query();
<ul>
<?php
global $post;
$args = array( 'category' => 'your category name id''offset'=> 1, );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
for more information check https://codex.wordpress.org/Template_Tags/get_posts
Please try this code :
change your category id or post id
<?php if (is_category('3') ) { ?>
<?php query_posts('p=17'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} elseif (is_category('13') ) { ?>
<?php query_posts('p=11'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;
} else { ?>
//whatever goes in here
<?php } ?>
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.
This is what I have in the loop that grabs the posts and displays them on the index page...
<?php the_attachment_link( $attachment->ID , false); ?>
And this is what it results in...
<a href="linkToImage">
<img src="sorceOfImage"/>
</a>
I just want that middle part without having it wrapped in a link. Any ideas? Thank you so much!
what about this:
<?php echo wp_get_attachment_url( $id ); ?>
outputs something like http://example.net/wp-content/uploads/filename
You can get the image only:
<?php echo wp_get_attachment_image( $attachment->ID ); ?>
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
<?php if (have_posts()) : while(have_posts()) : the_post(); ?>
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) : ?>
<ul>
<?php foreach($attachments as $attachment): ?>
<li>
<a href="<?php echo wp_get_attachment_url($attachment->ID, true); ?>">
<img src="<?php echo wp_get_attachment_url($attachment->ID, true); ?>" width="100%"/>
<h3><?php echo $attachment->post_title; ?></h3></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endwhile; endif; ?>
Can i make a If Else-statement that finishes a div before doing "else"?
I want to cycle through my posts and put the posts from "specialCat" in one div.
<div id="the-div-i-want-to-close">
<?php if (in_category('specialCat')) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
Thanks a lot :)
Your best bet is to modify the query calling your special category then finishing the loop with the rest of the posts.
<?php
$args = array(
'category_name' => 'special-cat', // Use Category slug
'posts_per_page' => -1 // Get all the post in SpecialCat
);
$special_query = new WP_Query( $args );
?>
<div id="the-div-i-want-to-close">
<?php
while ( $special_query->have_posts() ) : $special_query->the_post();
$no_repeat[] = $post->ID ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; ?>
</div> <?php // Close your special div after looping through all SpecialCat posts
// Start the loop again
if (have_posts() ) : while (have_posts() ) : the_post();
if (in_array( $post->ID, $no_repeat ) ) continue; // Exclude the posts we already got above ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
You can include HTML within a for each loop, so long as you close the PHP tags (or include the HTML in an echo statement).
Would this do it for you?
<div id="the-div-i-want-to-close">
<?php $isOpen = true; ?>
<?php if (in_category('specialCat')) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php if($isOpen) { ?>
</div>
<?php $isOpen = false; ?>
<?php } ?>
<?php the_title(); ?>
<?php } ?>