Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Coppa WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Come collegarsi a link esterni dal titolo del post in WordPress

Nota editoriale: guadagniamo una commissione dai link dei partner su WPBeginner. Le commissioni non influenzano le opinioni o le valutazioni dei nostri redattori. Per saperne di più su Processo editoriale.

Volete aggiungere un link esterno come titolo di un post in WordPress? A volte potreste voler condividere un link con i vostri utenti. Invece di inviarli a un post, potreste volere che il titolo del post rimandi a un altro sito web. In questo articolo vi mostreremo come inserire un link esterno nel titolo del post in WordPress.

Adding External Link to WordPress Post Title

Metodo 1: Collegare il titolo del post a un link esterno in WordPress utilizzando un plugin

Questo metodo è più semplice ed è consigliato ai principianti.

La prima cosa da fare è installare e attivare il plugin Page Links To. Per maggiori dettagli, consultate la nostra guida passo passo su come installare un plugin di WordPress.

Dopo l’attivazione, è sufficiente creare un nuovo post o modificarne uno esistente. Noterete il nuovo meta-box “Page Links To” sotto l’editor del post.

Adding a custom URL in page links to

Cliccare su ‘Un URL personalizzato’ per aggiungere il link che si desidera al titolo del post. Ora è possibile salvare o pubblicare il post.

È tutto. Il titolo del post sarà ora collegato all’URL personalizzato fornito.

Non è necessario utilizzarlo solo per i link esterni. Potete anche usarlo per inviare gli utenti a diversi post e pagine del vostro sito WordPress.

Metodo 2: Aggiungere un link esterno al titolo del post utilizzando il codice

Questo metodo richiede l’aggiunta di codice al vostro sito WordPress. Potete utilizzare questo metodo se vi sentite a vostro agio nell’incollare snippet dal web in WordPress.

È sufficiente aggiungere questo codice al file functions.php del vostro tema o a un plugin specifico per il sito.

function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);

if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='external_url') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}

Questo codice cerca semplicemente un campo personalizzato contenente il vostro URL personalizzato. Se il post ha il campo personalizzato, viene visualizzato il titolo del post collegato all’URL.

Il passo successivo è quello di sostituire la visualizzazione predefinita del titolo del post del vostro tema con questa funzione. La si trova in archives.php, content.php, category.php e altri template. Il suo aspetto è simile a questo:

<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>

È necessario sostituirlo con questo codice:

<?php print_post_title() ?>

La parte del codice è finita, ora è necessario aggiungere l’URL esterno al post. Basta modificare il post o crearne uno nuovo. Nella pagina dell’editor del post, cercate la casella dei campi personalizzati.

Se non si riesce a vedere la meta casella dei campi personalizzati, è necessario fare clic su Opzioni schermo nell’angolo in alto a destra dello schermo. Si aprirà un menu in cui si dovrà selezionare la casella accanto a “Campi personalizzati”.

Show custom fields meta box on the post edit screen in WordPress

La casella dei campi personalizzati si trova sotto l’editor dei post.

Cliccate su “Inserisci nuovo” e inserite external_url nel campo “Nome” e l’URL che volete aggiungere al titolo del post nel campo“Valore”.

Adding new custom key

Ora è possibile salvare o pubblicare il post. Il titolo del post sarà ora collegato all’URL aggiunto nel campo personalizzato.

La prossima volta che si vorrà aggiungere un link, basterà selezionare il campo personalizzato external_url dal menu a discesa e inserire il link esterno nel campo value.

Speriamo che questo articolo vi abbia aiutato a capire come collegare i link esterni dal titolo del post in WordPress. Potreste anche voler consultare la nostra guida su come aggiungere un’icona di collegamento esterno sul vostro sito WordPress.

Se vi è piaciuto questo articolo, iscrivetevi al nostro canale YouTube per i video tutorial di WordPress. Potete trovarci anche su Twitter e Facebook.

Divulgazione: I nostri contenuti sono sostenuti dai lettori. Ciò significa che se cliccate su alcuni dei nostri link, potremmo guadagnare una commissione. Vedi come WPBeginner è finanziato , perché è importante e come puoi sostenerci. Ecco il nostro processo editoriale .

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Il kit di strumenti WordPress definitivo

Ottenete l'accesso gratuito al nostro kit di strumenti - una raccolta di prodotti e risorse relative a WordPress che ogni professionista dovrebbe avere!

Reader Interactions

22 commentiLascia una risposta

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. John says

    Will this work on images relating to the post titles as well? So clicking on either image or post title takes you to the page on the external site?

  3. Alessandro says

    is there a way to link post titles to custom links, but only if they are present in one page (so not for the whole site)?

    Example: search results page of plugins like search & filter or toolset

  4. Kam says

    Thank you. This is really helpful. Would this plugin work for automated RSS feeds? i.e. for aggregator sites? I’m assuming no, as the titles are constantly changing. I’m trying to take the user to the original source with one click on the title.

    • WPBeginner Support says

      You would want to reach out to the support for the aggregator tool you are using for the options you have available.

      Admin

  5. mostafa says

    Thank you for this tutorial. It helped me a lot but how to use this for cpt (in my case a testimonial) and open the link in a new tab . Thank you.

  6. mehmet says

    Thank you for useful information.
    My English is a little bad.
    I want to use this kind of plugin on my site
    But the bold type in the text will be automatically linked to the text.
    Links to other posts within the site
    Is there such an extension?

  7. Tammy says

    Is there a way to require the external url to be unique? I’m using wp-directory and would love to be able to make this custom field “unique required”

  8. Melch Wanga says

    Its a good post indeed. In response to Toni, in my case I am developing a website for film production company and I have “Equipment Hire” as a services in ‘service’ custom post type. Equipment Hire is quite huge and I am using WooCommerce to add the various equipment hence I have Shop page that lists all the equipment available for hire. The trick is to ensure that whenever a user clicks on “Equipment Hire” service, they are directed to the Page set as the Shop page instead of the default Equipment Hire single post page.

  9. Palashtd says

    Recently I have started blogging.
    I could think how to add External Links from the Post Title. But finally I got an awesome tip from this post. I have tested by following this tutorial. I am happy to get these tips because it’s working for me.
    Thanks for sharing this information.

  10. Toni Weidman says

    I’m not sure why you would want to do this. Can you clarify what the purpose of this process is. Thank you.

Lascia una risposta

Grazie per aver scelto di lasciare un commento. Tenga presente che tutti i commenti sono moderati in base alle nostre politica dei commenti e il suo indirizzo e-mail NON sarà pubblicato. Si prega di NON utilizzare parole chiave nel campo del nome. Avremo una conversazione personale e significativa.