How to remove link around the_attachment_link image in wordpress - php

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; ?>

Related

Php with WordPress how to call up permalinks to two separate posts

i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
you can use echo get_the_permalink($post->ID) to get the uri for the posts
So it looks like in your case you'd need
echo get_the_permalink($post[0]->ID);
and
echo get_the_permalink($post[1]->ID);
in the href
However you're probably better off creating a foreach loop to go through the posts from the get_posts function
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
Okay, first of all, you are not looping the query you have made ( e.g $posts = get_posts( $args ); ) you are just displaying the 1st post's thumbnail and the thumbnail of the current page.
You need to loop the post like this :
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;

How to make php post loop with category icons

So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>

post time don't change in loop - wordpress

I want a simple loop to get latest five posts, which only consist of post title and time i wrote the loop below and the title generates fine, however the time doesn't change. as it get the first post in the loop's time for other posts as well. so all the post time is same.
Please advise why time don't loop ?
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){ ?>
<li class="orbit-slide">
<div>
<a href="<?php echo get_permalink($recent["ID"]); ?>" class="ticker-a">
<span><?php echo get_the_time($recent["g:i a"]); ?> </span>
<?php echo $recent["post_title"]; ?>
</a>
</div>
</li>
<?php }
wp_reset_query();
?>
Try this one
echo get_the_time('', $recent["ID"]);
On a side note. This is a much more efficient piece of code and it is easier to output content.
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
Thanks "Aron" for answer 1 :
echo get_the_time('', $recent["ID"]);
And Thanks "WizardCoder" for the other loop solution:
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>

Advance Custom fields keep returning the same value

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.

PHP loop not being applied on a specific HTML element

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; ?>

Categories