I am wanting to cross link to a couple of other sites without viewers needing to go to a separate page and then click the link. Is there/could there be a way to directly link the title of a pinned post so it goes straight to the link, rather than the post. If that makes sense?
Yep, that makes sense. Iâve heard this request several times â weâll definitely add it, just need to figure out how to fit it in with the platform. Ideally, weâd continue using the current Publish -> Pin workflow, and have the platform recognize the post as a direct link.
Makes sense. If all there is to a post is a link, off you pop!
The other day I discovered a little trick to do just this - replace a pinned post with a link to another webpage.
All you need is the link to the pinned post and the link of what you want to replace the pinned post with. Then add this to your âCustom Javascriptâ:
var a = document.querySelector('a[href="https://blog.cjeller.site/search"]');
if (a) {
a.setAttribute('href', 'https://search.cjeller.site');
}
And off you pop! Might be a bit of a hack until more customization becomes available, but I think it does the job.
It does, indeed!
Thank you very much, very helpful!
@cjeller1592, Iâm confused exactly what text to replace with which link. Could you please produce an idiot guide for me, perhaps with âpinnedpost.comâ and âlandingpost.comâ as the fillers instead of other addresses?
Also, will this work for pages internal to the blog and outside the blog as well?
I would be glad to explain it a little further.
And actually, there is simpler code to accomplish this (courtesy of @Cat). In your Customize settings of your blog, go down to Custom JavaScript for your blog you can put in this in:
const pinned = document.querySelector('a[href*="https://pinnedpost.com"]');
pinned.href = 'https://landingpost.com';
Letâs break it down. The first step is writing code to find the pinned post whose link we want to change. That is the first part:
const pinned = document.querySelector('a[href*="https://pinnedpost.com"]');
The code says âCalled âpinnedâ whatever element has this pinnedpost.com link.â So in your case, you will replace the above with the link to the post on your blog you want to go to another webpage. For example:
const pinned = document.querySelector('a[href*="https://write.as/cjeller/social"]');
The next line of code is where the magic happens. Now that our code identifies the element with the pinned post link, the second step is replacing that pinned post link to the link for our landing post. That is the second part:
pinned.href = 'https://landingpost.com';
All that code says is, âHey, remember that âpinnedâ element you found above? Change the url to landingpost.com instead.â So as with the example case above, I will put in the social page I want it to land on instead:
pinned.href = 'https://social.com/cjeller';
The pinned post can only be pages internal to the blog. The landing pages, however, can be anything internal to the blog and outside of it as well.
Hope that helps and let me know if you have any further questions!
Does this only work once per blog, @cjeller1592? I tried it on stjosephworkshop.org and it worked fine for my link to shepherdsandhalos.org but adding a second round to link to catholichalos.org broke the first and the second never worked. Thinking I may best just have a pinned âSister Sitesâ post. Grin. What am I missing?
EDIT: The error was âPinnedâ has already been declared. I changed the second one to âpinned2â but that didnât work. Iâm going to go with a Sister Sites post. Itâs kinder to my non-tech skill set. Grin.
Hi @DeaconPatrick,
You are on the right track with changing the second one to pinned2. The catch is that you also have to change the second line of code to match pinned2. So the second will look like this:
const pinned2 = document.querySelector('a[href*="https://pinnedpost.com"]');
pinned2.href = 'https://landingpost.com';
So in total your custom JavaScript will look something like this:
const pinned = document.querySelector('a[href*="https://pinnedpost.com"]');
pinned.href = 'https://landingpost.com';
const pinned2 = document.querySelector('a[href*="https://pinnedpost.com"]');
pinned2.href = 'https://landingpost.com';
The sister sites page is an equally fine solution, but I just wanted to let you know that this can work on multiple pinned posts. Hope that helps and let me know if you need more clarification.
Iâve thought about this a little myself. But wouldnât it be simpler to use a CSS ::after
property rather than using JavaScript to replace text thatâs already been rendered?
I think we are trying to change the âhrefâ of the pinned post instead of the text itself. Do you think that would be possible with the ::after
property? On first blush I thought CSS could not manipulate the âhrefâ in that way. If thatâs not the case I would be glad to learn how it works.
Upon further investigation, I think youâre right. My limited experience suggested that it would just insert arbitrary text, which could then be interpreted as its own HTML. But unless the write.as backend does that, I donât think it would work.
Yeah, I donât think the backend does that currently. But like @matt said, we might try to make some adjustments in the future where the backend will recognize a post as a direct link via some kind of markup. Until that point I think the two lines of JavaScript is a good solution but I am more than receptive to alternatives!
I gave this a go and it doesnât seem to be working, I thought a second pair of eyes may see what I did wrong. Thanks
const pinned = document.querySelector('a[href*="https://write.as/timapple/social-feed"]');
pinned.href = 'https://social.timapple.com/';
Never mind, I figured it out, I had the first URL screwed up.
This works a treat! Does anyone know how I can get this to work, but have the âclickâ opening up the webpage in a new tab (rather than the same one)?
Using Timâs sample code, if you add the line below, it should open up a new browser tab when you click the link.
pinned.target = "_blank";