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('Read more', 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:
printf('Read more', post_permalink($post->ID));
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:
printf('Read more', 'http://www.yoururl.com');
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:
Take a look at this plugin;
Create a text field named URL;
Use get_post_meta() to access it in the front-end;
Your line of code would like something like this:
printf('Read more', get_post_meta( $post->ID, 'URL');
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:
WordPress primarily uses posts: these are specific entries for your site. Depending on the nature of the site, they can be blog posts, reviews, news articles, anything really. Just remember that each post is it's own particular thing inside WordPress
The way a post looks is determined by the template that is used.
The bit of code that you posted is a loop; that one in particular is taking the content of the variable $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
Inside the {} brackets, $post refers to that particular post. $post->ID returns the unique ID number for that post.
The 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.
The 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.
Related
Last year i've created a website with the Wordpress theme Let's Travel. I now have an issue regarding the displayed search results and I hope someone has a solution. The search results look like this:
Screenshot Searchresults
Looks ok, but i've marked the part that I don't want in there. That are the titles of the tabs on the actual page. I just want the first part of the actual text showing here. Or, even better: make this a custom excerpt (so it's using the excerpt you can create on every page by yourself).
The code providing this piece of information, looks like this:
<?php print wp_kses_post(Letstravel_TPL::clear_urls( wp_trim_words( wp_strip_all_tags(do_shortcode(get_the_excerpt())), 20 ) )); ?></p>
I've tried all kinds of different options with this code, but nothing seems to work. Does anyone have any tips regarding this issue? I am not very familiair with php, so I'm sorry if this is a very easy question :)
Thanks!
I am new to wordpress, I have searched this thing on net but unable to find exact solution.
I have created a post that contains the Actor's Profile and the list of Movies Actor has worked in. Each Movie has also a different Wordpress Post that contains Movie details.
Now i want to create a link on each Movie, by which user can view the details of that particular movie.
This following link gives the result
<a href="http://localhost/wordpress/2015/10/09/movie-main-page">
but if i add this in each post, then it would be very difficult for me in future, when i will be uploading the site on web server, to change each link individually on every post.
I believe that there must be some way out there better than what i doing here, but somehow i am unable to find that trick.
Kindly guide me.
Thanks
You can use the get_site_url() template tag to return the site url and concatenate into the string. I haven't tested this code, so it might need some tinkering, but it should get you started:
<?php echo '<a href="' . get_site_url() . '/2015/10/09/movie-main-page">' ?>
wordpress link to anchor on another page
If Your Movie Page URL is Same Then You can use
echo get_permalink('11');
Here 11 is id of the page/post.
or if you have different different post for movies that can have different urls.
You can use wp_query loop for each post
[https://codex.wordpress.org/Class_Reference/WP_Query][1]
and use simply **echo get_permalink();**
There are two ways to achieve what you're doing.
The first and easiest way would be to run a search and replace on the database to change any URL's in your posts to the new URL. You should have a look at https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ .
You could also use Advanced Custom Fields to Achieve this. You would need to use the post object field to link to another page within wordpress. With the post object field you can create a field for each post that links to another post which you can then display in your template. Take a look here https://www.advancedcustomfields.com/resources/post-object/. With this method you could transfer all the content and associated links would still work.
Here is an image of my comment format: http://oi40.tinypic.com/8w07jt.jpg
The hyperlink to each individual comment is the date and time with the url in the format of, for example: /nottingham/#comment-184
After going through my comment templates I found that these two functions do the following,
get_comments_link : Retrieves the link to the current post comments
AND
comments_link: Displays the link to the current post comments
The problem is I have no idea where to include these functions so it creates a hyperlink to every comment within every comment. Once this link has been created I'd also like to assign a div to it so I can format it to be in the bottom right corner of each comment.
My final goal is to manipulate the link so rather than the comment link itself it will automatically work in the facebook.com/sharer.php which I feel I can do myself once I figure out the previous part. Essentially it will be a share on facebook button, but I have found no plugins which do this yet and thought describing it as a hyperlink would be an easier way to explain what I'm trying to do.
Elsewhere I've been told the following: but not sure how to implement this...
In functions.php I'd add a 'get_comments_link' filter to return the desired link, including the tag you need. Note that this is a filter, not a function. There is function with the same name, but it's part of WordPress itself. Use the filter to add a div to the existing link. Then style the new div to position it where you want it. I notice that the Photoria theme repositions the Reply link, which is coded at the bottom but displayed at the top. In the same way, your new will be coded at the top but displayed at the bottom. Also see add_filter in the Codex. Add the new div to $link before returning it. The new div will need a class attribute so that you can work with it in CSS to make it look the way you want.
I hope I've made the question clear, please ask if I haven't. Thanks in advance!
You need to edit your comment template in your Wordpress theme.
Default is located in /comments.php
Some more informations could be found here : http://codex.wordpress.org/Function_Reference/comments_template
Finally answered it myself, the code used to display the comment link was:
<img src=""
Which was inserted in my comments template file within my comments body div which I then put into another div so I could format it itself to where I wanted it positioned.
Currently in the process of making the link automatically share through the facebook sharer...
Hope this helps anyone with the same problem.
I'm trying to develop web application (php/mysql), sort of blog site where ~10 posts are listed on front page. Only first 4-5 lines of each post are displayed and when user clicks the title it opens new page with full post displayed.
Users will use TinyMCE to post with very limited functionality, so bold, italics, underline, undo/redo and bullet lists only. No HTML view, no links nor images so nothing fancy.
Now, as two displays of the same posts are involved, so the one on front page where part is displayed only, and full version, I'm not sure if I should:
when user submits post I should clear it up with something like HTMLPurifier and store one full version in database. Then cut first 4-5 lines of it, and clear it with HTMLPurifier again to make sure all tags are closed properly. This is to make sure the text I cut doesn't finish with html tag cut in half but rather is valid (x)html properly closed. I would store this shorten front-page version somewhere in database again. So I would have two versions in database ready, full post version and shorten front-page version. Then when someone accesses the site and displays front page with 10 posts, these just need to be read from database and displayed. No need for reading full version, cutting the top of it, making sure all tags are properly closed etc. etc. for all 10 posts every time someone opens the site. The obvious problem would seem that part of content is doubled in database so full version and short version.
Another option I thought about would be when user submits post, clear it up with HTMLPurifier, and store only full version to database. Then when someone accesses the site, cut top part of each blog post, clear it up with HTML purifier (again!) to make sure its all valid, tags are closed etc. and display on front page. Now the problem seems that HTMLPurifier would have to be run and clean every single time when someone accesses the short version of the post, so e.g. 10x for frontage for each visit and so on.
I used HTMLPurifier above as an example only as I'm sure there are other tools douing same thing so please advice if anything else would be more appropriate in my situation. Also I will probably allow users to display more than 10 posts per page.
Ok, I'm sure there is some common pattern to deal with this sort of site and if so please give me an idea of how it should be done as my thought above seems to have many obvious problems. As always your help is much appreciated.
EDIT: I should have clarified.. Its more like an exercise to me and I wanted to learn how this sort of site should be done. Same concept can be implemented on other sites so advertising, forums etc. anything with posting really. For blogging I sure could use something ready but this time I wanted to learn.
When a user posts a blog entry, all you need to do is insert the entire post into the database. Before inserting, you need to escape/clean-up the post so that nothing harmful is inserted.
When viewing the post you will have two type of views - the excerpt and the full. The excerpt will only be the first 4-5 lines of the post. To get this you need to TRUNCATE the post before outputting it. To do that, you need to create a truncate function that will limit the number of words/characters.
You do not need to insert the post twice ( excert and full ) into the database.
For instance:
// Excerpt Post
echo $postTitle;
echo truncate($postBody); // where truncate is the function you used to trim
// Full Post
echo $postTitle;
echo $postBody;
Hope this gets you started!
I'm trying to create an auto generated post excerpt from the current page's post content using a function in my theme's header file. The post excerpt will be used as the page's meta description. Can someone give me an idea of how you might go about this once you've got the post content into a string variable?
The somewhat tricky part is that, in order to predict a viable stopping point for the post excerpt, I'd like to specify that the cutoff point be the end of the first paragraph of text.
And for that reason, it does not make sense to load the entire post content into the string I'm using. Can I grab the first paragraph without having to load the entire post content string?
And I'm not certain how to test for that in php. Would regex be the only way?
You can't parse HTML with regex. Posts are stored formatted in HTML (i.e. <p></p> <br /> etc).
I'm also assuming you are implementing this on a blog with lots of existing posts.
What you can do is:
Retrieve the post and run it through an XML Parser. Grab the first paragraph. This is incredibly expensive for such a simple task.
Use a quick tag in the post to denote the excerpt stop point, strip HTML from everything to the left of it. Similar to the <-- more --> tag.
Store an excerpt with each post, I believe WP already has facilities for that.
It would be much, much easier if you could simply select the excerpt without having to do any additional fiddling in order to use it, so the time to handle it is when a post is saved.
So, if you can initially select each post, parse it, get the first paragraph and insert it into another table, then have your plugin do that when each new post is saved, you're home. Naturally, you'd update the same if a post was edited (making that optional).
Just please, please, please don't introduce a plugin in WP that uses regex to parse a context free language. Its just asking for trouble.