WordPress Related Posts thumbnails work but permalink error - php

I am attempting to show 4 related posts beneath each WP post. The thumbnails work correctly, pulling in the feature image and the title, but the permalink does not. It comes in as "http://example.com/original-post-name/< ? the_permalink(); ? >/" (spaces added) and is clickable but of course no content found. This exact code works fine on a different site of mine, but not this new one. I'm sure it could be improved - I'm fairly new to wordpress theming.
<?php
// Default arguments
$args = array(
'posts_per_page' => 4,
'post__not_in' => array( get_the_ID() ),
'no_found_rows' => true,
);
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<!--<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute(
'echo=0' ) ); ?>"><?php the_title(); ?></a>-->
<div class="relatedthumb">
<a rel="external" href="<?the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br
/>
<?php the_title(); ?>
</a>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
Apologies if this has been asked before - I've tried searching and hoping this is a simple solution. Thank you!

You are using the wrong opening tag for php
<a rel="external" href="<?php the_permalink(); ?>">
So it's a typo

Related

In Wordpress, can I add a text string to the end of url with the_permalink(); function?

I am using the following code to create a list of child pages for my website. I want all of the urls to have something like #theheading added to the end so that when you go the page, it goes to a certain spot on the page that has that id.
/* List Child Pages version 2 */
<?php
function wpse_list_child_pages_two( $cats = [] ) {
global $post;
$current_ID = $post->ID;
if ( is_page() && $post->post_parent ) {
$child_of = $post->post_parent;
} else {
$child_of = $current_ID;
}
// Get the category IDs for passed in category names for query.
$cats = ( array ) $cats;
$cats = array_map( 'get_cat_ID', $cats );
$args = [
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $child_of,
'order' => 'ASC',
'orderby' => 'menu_order',
'category__not_in' => $cats,
'after' => 'top'
];
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<?php
$current = function( $output ) use ( $current_ID ) {
return get_the_ID() === $current_ID ? $output : '';
};
?>
<li class="nav-item nav-item<?php the_ID(); echo $current( ' active ' ); ?>">
<a href="<?php the_permalink(); ?>" <?php echo $current( 'aria-current="page"' ); ?>><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
<?php endif; wp_reset_postdata();
}
If I add #theheading into my html like below, it adds in after the / in my permalink
<a href="<?php the_permalink(); ?> #theheading" <?php echo $current( 'aria-current="page"' ); ?>><?php the_title(); ?></a>
Is there a simple way to pass a sting onto the end of the the_permalink(); results before the closing slash in my url?
Tried to add #theheading into the html after the function.
You could with javascript. For WordPress you may utilize the hook the_permalink https://developer.wordpress.org/reference/hooks/the_permalink/
Example from there:
function append_query_string($url) {
// return add_query_arg($_GET, $url);
return $url . "#theheading";
}
add_filter('the_permalink', 'append_query_string');
By the way, did you try your html without the space between the URL and the hash?
<a href="<?php the_permalink(); ?>#theheading" <?php echo $current( 'aria-current="page"' ); ?>><?php the_title(); ?></a>
Okay, so it turns out that the original way I tried will work at going to #theheading. For some reason when I initially tested it, it just refreshed the page without going to #theheading so I assumed it was from the / between the url and #theheading (example: someurl/#theheading), but it is working now. You are right though, the extra space should be removed. So what is below will work.
<a href="<?php the_permalink(); ?>#theheading" <?php echo $current( 'aria-current="page"' ); ?>><?php the_title(); ?></a>

Add an if alternative statement inside another if statement within a loop with php to create two links (one real and one empty)

First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) ) {
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post ) {
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
}
echo '</ul>';
}
?>
Any Idea why it is not working?
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)

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, an image that is pulled in by category, link to a parent posts page not to its category page it is located in. Wordpress

I'm new to php (' still struggle with syntax :-) ') and wordpress and I'm building a web-site where I am looping to my category page all the images tagged with that category. I need the images to be automatically linked to its parent post which should be a Clients page essentially. The code i'm using right now links me to the same category page instead of client page.
This is the code i'm using now to pull the images.
<?php
$query_images_args = array(
'cat' => 3,
'post_type' => 'attachment',
'post_mime_type' => 'image,video',//img & video files include
'post_status' => 'inherit',
'orderby' => 'ACS',
'posts_per_page' => 30,
);
$query_images = new WP_Query( $query_images_args );
if($query_images->have_posts()) :
while($query_images->have_posts()) :
$query_images->the_post();
?>
<a href="<?php get_permalink( $parent_id ); ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php echo $images = wp_get_attachment_image( $query_images->posts->ID, 'thumbnail' ); ?>
</a>
<?php endwhile; ?>
<?php else : ?>
<p>No media file yet</p>
<?php endif;
/* Restore original Post Data */
wp_reset_postdata(); ?>
I hoped that this part <a href="<?php get_permalink( $parent_id ); ?> will link me to the clients page but it still links me to category page.
I think that the I have something wrong with hierarchy or the way do the linking part.
I have also checked this resource but it does not seem to do anything :
https://wordpress.stackexchange.com/questions/188736/get-the-title-and-url-of-the-attachment-parent-post
I think that you are using a variable $parent_id that isn't set.
Try replacing:
<a href="<?php get_permalink( $parent_id ); ?>"
with:
<a href="<?php get_permalink( $post->post_parent ); ?>"

Wordpress Get Posts AJAX infinate scroll

I'm working on a website http://www.matchlessphotography.com which has got a great display of photos - it tiles for ages...
Essentially the client would like this to continue on infinately.
I have no idea how to do this and have had a look at some tutorials without any bearing...
This is how I am currently getting the posts:
<?php
global $post;
$args = array('numberposts' => 104, 'meta_key=visible&meta_value=yes');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<a class="photohovera" href="<?php the_permalink(); ?>">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<? echo $url ?>&h=138&w=197" width="197" height="138" title="<?php the_title(); ?>" />
<span class="spanno">
<strong class="title_blog_mini_post">
<?php the_title(); ?>
</strong>
</span>
</a>
<?php endforeach; ?>
I guess I just need to do this again but with an increased offset each time...?
Paul Irishs jQuery Plugin Infinite Scroll is the way to go here.
The plugin looks for a pagination container e.g. div.navigation and an item container for the items you are going to retrieve like .photohovera in your case.
WordPress provides a function for displaying pagination links. It's called paginate_links and its default output should match the requirements of the plugin.
However you will need to change they way you are getting your posts right now from get_posts() to a WP_Query Object:
<?php
$args = array('numberposts' => 104, 'meta_key=visible&meta_value=yes');
$myposts = new WP_Query( $args );
$results = $myposts->get_results();
foreach( $results as $post ) : setup_postdata($post); ?>

Categories