I’m trying to change the destination of oNe read more links in wordpress:
I found this code in the frontpage.php:
<?php
foreach ($query as $post) {
setup_postdata($post);
printf('<div>');
printf('<div class="box">');
printf('<h4>%s</h4>', $post->post_title);
printf('<img src="%s" />', wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), 'full')[0]);
printf('<p>%s</p>', get_the_excerpt($post->ID));
printf('<a href="%s" class="button">Read more</a>', post_permalink($post->ID));
printf('</div>');
printf('</div>');
}
wp_reset_postdata();
?>
I think I can fiddle around and edit the links so they go to a page of my design through this PHP, however I want to know how to do it through the WordPress Admin interface.
Because supposedly wordpress should make this easy for you, but I can’t seem to find anything on these read more links save the code I found in the PHP.
Do you know the “WordPress way” to change the link destinations?
After reading your comment to the link provided the reply is simple: The “WordPress” way is the code and the provided URL has everything you need to know about it. There’s no Admin way to do this, at least in a native way.
However, in the provided code you have a line that is showing that Read More, which is:
basically what this line does is to point to that respective post: it calls the post_permalink function and then the $post->ID tells the function which post to open.
In theory, you can change your link by changing that line to simple HTML:
This should solve your problem.
Correct way to do it:
Add a custom field to the post, insert the url and load it in the loop.
EDIT: to address the OP question better
Since this is a loop, represented in your code by the foreach, the code is executed once and on every loop the $post->ID changes automatically. By changing the loop by a fixed URL, your 3 posts will link to the same place.
There are several ways to change the behavior, the easiest, cleanest more efficient is to add a custom field to the post. This will create a field in your Post admin area, you insert your URL and access it in the frontend. This way all your posts can link where you want.
Ex:
Your line of code would like something like this:
The ‘read more’ links point towards the url for that particular post; changing the destination doesn’t make much sense given the context. It sounds like your issue isn’t so much about changing the destination of the link per se, rather you need to change the design of the page that it actually links to. If that’s the case you should look at either page templates if the destination is a page, or custom post types if it’s a type of post.
Looking at some of your other questions I gather that you’re new to WordPress. Here’s a brief summary of how your example is working so that you can hopefully form a better understanding:
$query
(which in your example contains three posts) and then for each post runs all the functions in between the{}
brackets. This is why you end up with three posts/images/links; the function runs three times, once for every post it finds inside of$query
{}
brackets,$post
refers to that particular post.$post->ID
returns the unique ID number for that post.get_the_excerpt()
function fetches the excerpt (a short bit of content) for that particular post. You pass it$post->ID
, so that it fetches the excerpt for the correct post.post_permalink()
function is what figures out what the url to that particular post. Again, in your example you’re passing it$post->ID
You should read up on The Loop; it forms the basis of how WordPress works. If you can get your head around that you’re halfway there.