Ajouter du code dans <head> sur une base par page / article
-
-
Jene suispas un développeurmaisje devais ajouter du code dans la zone pourintégrer des articles Google dans des articles WordPressindividuels (pas despages).Sur un article Google,lesinstructions d'intégrationincluent "ajoutez cettepartie à la section ,ajoutez cettepartie à l'endroit où vous voulez que l'article apparaisse dans votreblog" (pas desmotsexacts.) Jen'avais aucuneidée de ce qu'est la section Head,puisqueje suis un utilisateur régulier de Joe WordPresset non un codeur.Mais une recherche deplugins a apporté ce résultat: http://wordpress.org/plugins/per-page-add-to/J'espère que cela aide.I am not a developer but needed to add code to the area to embed Google posts into individual WordPress posts (not pages). On a Google post, the embed instructions include "add this part to the section, add this part where you want the post to appear in your blog" (not exact words.) I had no idea what the Head section is, since I am a regular Joe WordPress user and not a coder. But a search of plugins brought this result: http://wordpress.org/plugins/per-page-add-to/ Hope that helps.
-
C'est ce dont vous avezbesoin d'unpluginparfait https://wordpress.org/plugins/header-and-footer-scripts/This is what you need a perfect plugin https://wordpress.org/plugins/header-and-footer-scripts/
- 1
- 2015-10-17
- The KingMaker
-
5 réponses
- votes
-
- 2013-08-16
Comme vous l'avez ditparpage/par article,celafonctionneraitpour chaque article
add_action('wp_head', 'add_link_in_head'); function add_link_in_head() { global $post; if(!empty($post)) { $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); if(!empty($alternate) && !empty($hreflang)) { ?> <link rel="alternate" href="<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" /> <?php } } }
S'iln'y apas de $ Alternate et $ hreflang ,iln'y aurapas de lien.
As you said per page/post basis, this would work for each post
add_action('wp_head', 'add_link_in_head'); function add_link_in_head() { global $post; if(!empty($post)) { $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); if(!empty($alternate) && !empty($hreflang)) { ?> <link rel="alternate" href="<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" /> <?php } } }
If there is no $alternate and $hreflang then there would be no link.
-
- 2013-08-16
Utilisez le code ci-dessous dans la section
<head>
:<?php add_action('wp_head', 'your_function'); function your_function() { //your code will go here } ?>
Pourplus d'informations,vouspouvez visiter ici .
Use the below code in the
<head>
section:<?php add_action('wp_head', 'your_function'); function your_function() { //your code will go here } ?>
For more information you can visit here.
-
- 2013-08-16
Jene saispas comment vousenregistrez vos données,maisen supposant que vous utilisez laméta-boîteintégrée "Champspersonnalisés",vosmultiplespages alternatives devraient apparaître sousforme detableau.
function alternate_pages_links() { global $post; if (!is_singular()) { return; } $pat = '<link rel="alternate" href="%s" hreflang="%s" />'; $lang = get_post_meta($post->ID, 'hreflang', true); $alt = get_post_meta($post->ID, 'alternate'); if (!empty($alt) && !empty($lang)) { foreach ($alt as $a) { printf($pat,$a,$lang); } } } add_action('wp_head','alternate_pages_links');
Cependant,je vois unproblèmepotentiel. Si vous devez avoirplusieurspaires
alternate
/hreflang
associéesparpublication,vous devez lesenregistrer sousforme depaires dans labase de données. Àmoins que vousne lefassiez,iln'y aura aucunmoyen de dire quelhreflang
va avec quelalternate
. Vous devrez créer votrepropreméta-boîtepour cela.I don't know how you are saving your data but assuming that you are using the built in "Custom Fields" meta box your multiple alternate pages should come through as an array.
function alternate_pages_links() { global $post; if (!is_singular()) { return; } $pat = '<link rel="alternate" href="%s" hreflang="%s" />'; $lang = get_post_meta($post->ID, 'hreflang', true); $alt = get_post_meta($post->ID, 'alternate'); if (!empty($alt) && !empty($lang)) { foreach ($alt as $a) { printf($pat,$a,$lang); } } } add_action('wp_head','alternate_pages_links');
However, I see a potential issue. If you are to have multiple associated
alternate
/hreflang
pairs per post, then you need to save them as pairs in the database. Unless you do, there will be no way to tell whichhreflang
goes with whichalternate
. You will need to construct your own meta box for that. -
- 2013-08-16
désolépour celaje n'aipas 50 réputationpour ajouter un commentaire
La réponse dedot1 estparfaitepourinclure le lien alternatif dans la section head obtenir la valeur de laméta depublication,mais vous devezmaintenant ajouter desméta-cléspersonnaliséespour hreflanget des valeurs alternativespour chaquepage/publication àpartir de lamodification de lapublication écran ou créez uneméta-boîtepour cefaire.
// alternate link meta box hook into WordPress add_action( 'admin_init', 'add_my_link_metabox' ); // Add meta box function add_my_link_metabox() { // for posts add_meta_box( 'link_meta_box', __( 'Alternate Link Attr', 'Your Text Domain' ), 'my_link_data_function', 'post', 'side', 'core' ); // for pages add_meta_box( 'link_meta_box', __( 'Alternate Link Attr', 'Your Text Domain' ), 'my_link_data_function', 'page', 'side', 'core' ); } // create a function to display metabox function my_link_data_function() { global $post, $post_id; $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); // generate html for metabox ?> <div class="admin-inside-box"> <p> <input type="hidden" name="my_meta_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>" /> </p> <p> <label>Link alternate</label> <input type="text" class="widefat" name="alternate" value="<?php echo $alternate; ?>" /> </p> <p> <label>Link hreflang</label> <input type="text" class="widefat" name="hreflang" value="<?php echo $hreflang; ?>" /> </p> </div> <?php } // now save metabox data on save post // hook in save_post action add_action( 'save_post', 'save_my_link_data' ); // create function to save values function save_my_link_data() { global $post, $post_id; // verify nonce if ( !wp_verify_nonce( $_POST['my_meta_nonce'], basename(__FILE__) ) ) { return $post_id; } // check if current user can edit post if ( !current_user_can( 'edit_post', $post_id ) ) { return $post_id; } // now save/update the values in post meta keys update_post_meta( $post_id, 'alternate', $_POST['alternate'] ); update_post_meta( $post_id, 'hreflang', $_POST['hreflang'] ); } // hook alternate link into wp_head add_action('wp_head', 'add_link_in_head'); function add_link_in_head(){ global $post; if (!empty($post)) { $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); if (!empty($alternate) && !empty($hreflang)) { ?> <link rel="alternate" href="<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" /><?php } } }
J'espère que cela vous aidera,vouspouvez changer lesnoms desfonctions,etc.
sorry for this i have not 50 reputation to add a comment
dot1's answer is perfect for include link alternate in head section get value from post meta but now you need to add custom meta keys for hreflang and alternate values for each page/post from post edit screen or create a meta box for to do this.
// alternate link meta box hook into WordPress add_action( 'admin_init', 'add_my_link_metabox' ); // Add meta box function add_my_link_metabox() { // for posts add_meta_box( 'link_meta_box', __( 'Alternate Link Attr', 'Your Text Domain' ), 'my_link_data_function', 'post', 'side', 'core' ); // for pages add_meta_box( 'link_meta_box', __( 'Alternate Link Attr', 'Your Text Domain' ), 'my_link_data_function', 'page', 'side', 'core' ); } // create a function to display metabox function my_link_data_function() { global $post, $post_id; $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); // generate html for metabox ?> <div class="admin-inside-box"> <p> <input type="hidden" name="my_meta_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>" /> </p> <p> <label>Link alternate</label> <input type="text" class="widefat" name="alternate" value="<?php echo $alternate; ?>" /> </p> <p> <label>Link hreflang</label> <input type="text" class="widefat" name="hreflang" value="<?php echo $hreflang; ?>" /> </p> </div> <?php } // now save metabox data on save post // hook in save_post action add_action( 'save_post', 'save_my_link_data' ); // create function to save values function save_my_link_data() { global $post, $post_id; // verify nonce if ( !wp_verify_nonce( $_POST['my_meta_nonce'], basename(__FILE__) ) ) { return $post_id; } // check if current user can edit post if ( !current_user_can( 'edit_post', $post_id ) ) { return $post_id; } // now save/update the values in post meta keys update_post_meta( $post_id, 'alternate', $_POST['alternate'] ); update_post_meta( $post_id, 'hreflang', $_POST['hreflang'] ); } // hook alternate link into wp_head add_action('wp_head', 'add_link_in_head'); function add_link_in_head(){ global $post; if (!empty($post)) { $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); if (!empty($alternate) && !empty($hreflang)) { ?> <link rel="alternate" href="<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" /><?php } } }
hope this will help you, you may change function names etc.
-
Est-ce que cela va dansfunctions.php?Je l'ai ajoutéen hautet j'aieu uneerreur: Erreur d'analyse:erreur de syntaxe,'<'inattendu dans/xxxxxxxxx/functions.php sur la ligne 2Doos this go in functions.php? I added it to the top and got a error: Parse error: syntax error, unexpected '<' in /xxxxxxxxx/functions.php on line 2
- 0
- 2013-08-22
- moobot
-
éditéet fonctionnemaintenant,ajoutez ceciet le code de dot1 dansfunctions.phpedited and now working, add this and dot1's code in functions.php
- 0
- 2013-08-22
- Anjum
-
Hmm,j'obtiens uneerreur d'analyse chaquefois quej'ajoute le vôtre ou le code Dots àmesfonctions.phpHmm, im getting a parse error when ever I add either yours or Dots code to my functions.php
- 0
- 2013-08-27
- moobot
-
@moobot ohparseerreur sur quelnuméro de ligne?et vous devez ajouter les deux codesmine et Dot's@moobot oh parse error on which line number? and you need to add both codes mine and Dot's
- 0
- 2013-08-27
- Anjum
-
OK super,cela semblefonctionnermaintenant,maisje doispouvoir ajouterplusieursbalisespour chaquepage car c'est ainsi quefonctionnent lesbalises rel="Alternate".Vous devez référencer le ou les substitutset lapageelle-même.OK great, this seems to be working now, but I need to be able to add multiple tags for each page as that's how the rel="alternate" tags works. You need to reference the alternate(s) and the page itself.
- 0
- 2013-10-21
- moobot
-
- 2013-08-17
Si vous utilisez Transposh (Leplugin quej'ai développé) Pourgérer vos sitesmultilingues,le rel=Alternate sera ajouté automatiquement (avec lapossibilité de le désactiver)
Cependant,la raisonpour laquelleje réponds à cela,c'est que d'aprèsmonexpérience,les résultats de l'ajout de cettebalise onten fait été catastrophiques,doncje ne suispas sûr que l'ajout soit utile dutout.
Bonne chance
If you are using Transposh (The plugin that I have developed) For managing your multilingual sites, the rel=alternate will be added automatically (with the option to disable it)
However, the reason I am answering this, is that in my experience, the results of adding this tag have actually been abysmal, so I am not sure that adding it is helpful at all.
Good luck
Existe-t-il unplugin (ou unmoyen simple)pour ajouter du code à la section
<head>
parpage/par article?J'ai un site Webmultilingueet je doismettreen œuvre l ' rel="Alternate"balisage hreflang ,vous avez doncbesoin d'unmoyen de définir
rel="alternate"
individuellement sur chaquepage/article.J'ai cherché dans lesplugins WordPressmaisje n'ai rientrouvéjusqu'àprésent. Jene suispas vraiment un codeur doncje nepeuxpas créer quelque chosemoi-même,àmoins que cene soitplutôtfacile!
OK,j'essayemoi-même de créer un champpersonnalisépour cettefonctionnalité,voici ce quej'aijusqu'àprésent:
Maintenant,celafonctionnetrèsbien sije ne veux ajouter qu'unepage alternative. Lorsquej'essaye d'en ajouterplus,celan'ajoutera qu'une seuleinstance de
rel="alternate"
,la dernière étant ajoutée remplaçant l'original.Deplus,surtoutes lespages sur lesquellesje n'ai défini aucun de ces champspersonnalisés,j'obtiens unebalise vide:
Quelqu'unpeut-ilme diriger dans labonne directionpour corriger cesproblèmes?