How to add <a href> to link other post in Wordpress - php

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.

Related

Change URL of previous posts and next posts link in Wordpress

I'd like to change the URL of the <?php previous_posts_link ?> and <?php next_posts_link ?> that are being outputted by Wordpress.
Problem:
When I am on my single page, I am getting this kind of an url:
http://example.com/post-title/2/
What I like to achieve:
When I am on my single page, I would like to get:
http://example.com/2/
So, the URL without the post title!
I don't know if this is even possible, but I'd like to understand if this is doable or not.
Could someone help me out here, please?
Get next/previous post object using get_previous_post() & get_next_post() and build your custom url.
You can create custom URLs for your pages within the .htaccess file. I found a useful link that might be able to help you out from another thread. php custom link and custom pages

How do you override the meta deta to create multiple unique share links on a single Wordpress post?

Typically when you create social share links within a page, Facebook/Twitter pull the meta deta from the og tags to create the thumbnail; however, I am displaying a newsletter on a single page with multiple unique stories. I'd like each individual story to be shareable with a unique title and description, but I can't figure out how to give each share button a unique title and description.
I've attached a single Facebook share link below. As you can see I have it linked to the ID #top-story, so it will load the page at that story. I want the title and description of that story to be in the thumbnail though, not the meta data from the overall page.
<a class="link" title="Facebook" href=http://www.facebook.com/sharer.php?u=
[insert_php]the_permalink();[/insert_php]#top-story&t=
[insert_php]the_title();[/insert_php]" target="_blank" rel="noopener">
EDIT: I don't think I explained in the best possible way, but believe this example would help. The link below accomplishes what I am trying to replicate.
http://theskimm.com/2017/06/22/skimm-for-june-23rd-4
What I would do is this (in fact, this is how theskimm does it):
Generate a "slug" for each article, probably based on the title then use this as an anchor for the article section (eg: <a name="slug-of-article"></a>)
Link to the newsletter page as usual, but pass a query string such as ?jumpto=slug-of-article
Write a javascript handler that checks the jumpto query string parameter, and if it exists, scroll to that ID
You can see this working from theskimm here:
And here's how theskimm actually handles the request:

Wordpress, editing links

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.

Wordpress sidebar to load specific posts

I am building a website in Wordpress and I need a sidebar that when you click the name of each post on the sidebar, it loads that specific post in the middle of the page.
I am unsure of how to go about doing this.
I am new to wordpress and php and any help is appreciated :)
Well basically as far as I understood you want to replace the current middle content with post content.
To do that you will have to use AJAX connection. You will need to pass post id to function, where you will retrieve post data and return it back to the page, where you will need to just insert it in the content.
I suggest that you check these links -
http://api.jquery.com/jQuery.ajax/
http://codex.wordpress.org/AJAX
Example -
http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/
http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/
A more simple solution would be just to add the_permalink(); to page link, so it directly links to the post.

Add facebook like to every post

I am trying to add facebook like button in every idea user posts. I followed the answer
How to embed a facebook like button on every webpage of my website? .
My URL is like this type :
mydomain.com/index.php?menu=post&post_id=12
I add the URL to facebook upto mydomain.com/index.php?menu=post . Now if I like the post with post_id 12 and then visit say post with post_id 13, it is already liked ! Where I am doing wrong ? I am using smarty template engine.
When you put the button's HTML into your website, don't specify a URL at all.
That way, it will take the URL of whatever page it's placed onto automatically.
if you are using fb meta tags for the like, it takes it from the index page. your going to need to change the title, and all the meta tags everytime post_id changes.
Search google for several like scripts, and you could implement your with that.
Hope I helped!

Categories