Pourquoi is_page ne fonctionne-t-il pas lorsque je le mets dans le fichier functions.php?
-
-
Une solution dans lefil suivantm'a aidé à résoudre unproblème similaire: https://wordpress.stackexchange.com/questions/225359/is-page-template-is-page-in-functions-php-not-working/225369A solution in following thread helped me solving similar issue: https://wordpress.stackexchange.com/questions/225359/is-page-template-is-page-in-functions-php-not-working/225369
- 0
- 2019-01-19
- Lefan
-
6 réponses
- votes
-
- 2011-06-29
functions.php
esttraitébien avant que vouspuissiez savoir quellepageest chargée.Au lieu d'attribuer une valeur à une variable,mettez votre codeen fonctionet utilisez cettefonction dans lemodèlepage.php
.functions.php
is processed way before you can know which page is being loaded. Instead of assigning value to variable put your code into function and use that function inpage.php
template.-
J'ai égalementessayé avec ce code à l'intérieur d'unefonction,maisil semble qu'ilne renvoie rien.Je sais quemettretout cela danspage.php devrait aider,mais cen'estpas si élégant.I've tried with this code inside of a function as well, but seems like it returns nothing. I know putting it all into page.php should help, but is not so elegant.
- 0
- 2011-06-29
- Wordpressor
-
Jen'utilisepas d'identifiantpour la référence depage,mais le codexmontre que l'identifiant de lapagen'apas deguillemets simples,avez-vousessayé?`is_page (); //Lorsqu'une seulepageest affichée. is_page (42); //Lorsque lapage 42 (ID)est affichée. is_page ('Contact'); //Lorsque lapage avec unpost_title de "Contact"est affichée. is_page ('àpropos demoi'); //Lorsque lapage avec unpost_name (slug) "about-me"est affichée. is_page (array (42,'about-me','Contact')); //Renvoietrue lorsque lespages affichées sont soit l'ID depublication 42.` Cen'estprobablementpas le casmais ça vaut le coupI dont use id's for page reference but codex shows that the id of the page has no single quotes, have you tried that? `is_page(); // When any single Page is being displayed. is_page(42); // When Page 42 (ID) is being displayed. is_page('Contact'); // When the Page with a post_title of "Contact" is being displayed. is_page('about-me'); // When the Page with a post_name (slug) of "about-me" is being displayed. is_page(array(42,'about-me','Contact')); // Returns true when the Pages displayed is either post ID 42.` probably not the case but worth a shot
- 0
- 2011-06-29
- MartinJJ
-
Les citations @Martinne sontpastout àfait correctes,maisne cassent riennonplus -elles sont comparées vaguementpar défaut (sans vérifier si letype correspond)@Martin quotes are not entirely correct, but won't break anything either - it is loosely compared by default (without checking if type matches)
- 1
- 2011-06-29
- Rarst
-
Quefaire si vous souhaitez ajouter unfiltre à la requête?Vousne pouvezpasfaire cela dans lemodèle.Mais vousne pouvezpasnonplus lefaire dans lefichierfunctions.php!What if you want to add a filter to the query? You can't do that in the template. But you can't do it in the functions.php file, either!
- 0
- 2015-04-17
- reggie
-
@reggiepourquoipas?Vous devriezprobablementposer unenouvelle question à ce sujet.:)@reggie why not? You should probably ask new question about it. :)
- 0
- 2015-04-17
- Rarst
-
- 2016-03-06
get_header
devrait fonctionner si vous voulez le laisser dansfunctions.php
add_action('get_header', function() { if ( is_page( '2533' ) ) { // also tested with 'Apple' $bannerimg = 'apple.jpg'; } elseif ( is_page( 'test' ) ) { $bannerimg = 'test.jpg'; } elseif ( is_page( 'admissions' ) ) { $bannerimg = 'admissions.jpg'; } else { $bannerimg = 'home.jpg'; } });
get_header
should work if you want to leave it infunctions.php
add_action('get_header', function() { if ( is_page( '2533' ) ) { // also tested with 'Apple' $bannerimg = 'apple.jpg'; } elseif ( is_page( 'test' ) ) { $bannerimg = 'test.jpg'; } elseif ( is_page( 'admissions' ) ) { $bannerimg = 'admissions.jpg'; } else { $bannerimg = 'home.jpg'; } });
-
- 2011-06-29
Enprolongeant ce que @Rarst apostéet que vous avez commenté,une solutionplus élégante serait de créer votreproprefiltre à l'intérieur depage.phpet de l'accrocher àpartir d'unefonction à l'intérieur dufunctions.php,parexemple:
dans votrepage.php
$bannerimg = apply_filters('my_bannerimg','defualt_img.jpg');
et dans votrefunctions.php
add_filter('my_bannerimg','what_page_is_it'); function what_page_is_it($img){ if ( is_page( '2533' ) ) { return 'apple.jpg'; } elseif ( is_page( 'test' ) ) { return 'test.jpg'; } elseif ( is_page( 'admissions' ) ) { return 'admissions.jpg'; } else { return 'home.jpg'; } }
Extending what @Rarst posted and you commented , a more elegant solution would be to create your own filter inside page.php and hook to it from a function inside the functions.php, for example:
in you page.php
$bannerimg = apply_filters('my_bannerimg','defualt_img.jpg');
and in your functions.php
add_filter('my_bannerimg','what_page_is_it'); function what_page_is_it($img){ if ( is_page( '2533' ) ) { return 'apple.jpg'; } elseif ( is_page( 'test' ) ) { return 'test.jpg'; } elseif ( is_page( 'admissions' ) ) { return 'admissions.jpg'; } else { return 'home.jpg'; } }
-
- 2017-05-12
Ajoutez ceci à votrefunctions.php,changez lenom du script someCodeet lenom de lapage:
add_action('wp_enqueue_scripts', 'wpt_theme_js'); function wpt_theme_js() { if ( is_page('somePage') ) { wp_enqueue_script('someCode_js', get_template_directory_uri() . '/js/someCode.js', '', '', true); } }
Add this to your functions.php, change name of script someCode and name of page:
add_action('wp_enqueue_scripts', 'wpt_theme_js'); function wpt_theme_js() { if ( is_page('somePage') ) { wp_enqueue_script('someCode_js', get_template_directory_uri() . '/js/someCode.js', '', '', true); } }
-
- 2011-06-29
Avez-vous correctement déclaré
wp_head();
etc dans votrethème?Deplus,
is_page
accepte unidentifiant sansguillemets.Leproblèmepeut aussi être lefait que vous êtes déjà sur lemodèle depage,donc c'est unepage,vousferiezpeut-êtremieux d'interroger le
$post->ID
ou de configurer lapagepage-apple.php
Have you correctly declared
wp_head();
etc in your theme?Also,
is_page
accepts an ID without quotes.The problem may also be the fact you are already on the page template so it is a page, you may be better of querying the
$post->ID
or set uppage-apple.php
-
`is_page ()`peut également accepter l'ID commeentier`is_page()` can accept ID as integer as well
- 0
- 2011-06-29
- Bainternet
-
ouaismais unint ne doitpas être citéyeah but an int shouldn't be quoted
- 0
- 2011-06-29
- Alex Older
-
C'est étrange,carje vois des citationspartout: http://codex.wordpress.org/Conditional_TagsThat's strange, because I see quotes everywhere: http://codex.wordpress.org/Conditional_Tags
- 0
- 2011-06-29
- Wordpressor
-
et c'est unemauvaisepratique.and it's a bad practice.
- 0
- 2011-06-29
- Alex Older
-
Je suis d'accord,l'entierne doitpas être cité.Mais PHPne s'en soucierapas detoutefaçon.I agree, the integer should not be quoted. But PHP will not mind either way.
- 0
- 2015-04-17
- reggie
-
- 2011-06-29
Vous devez appeler votrefonction à unmoment donné duprocessus WordPress après la configuration de la requête .
Dans
functions.php
:function mytheme_get_banner_img() { if ( is_page( '2533' ) ) { // also tested with 'Apple' $bannerimg = 'apple.jpg'; } elseif ( is_page( 'test' ) ) { $bannerimg = 'test.jpg'; } elseif ( is_page( 'admissions' ) ) { $bannerimg = 'admissions.jpg'; } else { $bannerimg = 'home.jpg'; } return $bannerimg; }
Ensuite,dans votrefichiermodèle
page.php
,partout où vous devez retourner/afficher$bannerimg
:<?php $bannerimg = mytheme_get_banner_img(); ?>
Ensuite,vouspouvezfairetout ce dont vous avezbesoin avec
$bannerimg
: déposez-le dans unebalise<img>
,etc.You need to call your function at a point in the WordPress process after the Query is set up.
In
functions.php
:function mytheme_get_banner_img() { if ( is_page( '2533' ) ) { // also tested with 'Apple' $bannerimg = 'apple.jpg'; } elseif ( is_page( 'test' ) ) { $bannerimg = 'test.jpg'; } elseif ( is_page( 'admissions' ) ) { $bannerimg = 'admissions.jpg'; } else { $bannerimg = 'home.jpg'; } return $bannerimg; }
Then, in your
page.php
template file, wherever you need to return/output$bannerimg
:<?php $bannerimg = mytheme_get_banner_img(); ?>
Then, you can do whatever you need to with
$bannerimg
: drop it in an<img>
tag, etc.
J'ai unepageintitulée "Apple",l'identifiant de lapage 2533.
Dans lefichierpage.php,j'ai la ligne:
Et cettefonction dansfunctions.php:
Lepointest que $bannerimgfait écho à "home.jpg" sur chaquepage,y compris Apple,testet admissions.
J'aimême vérifiétous les IDen utilisantthe_ID & amp;$page-> ID.Rien.Doncje suppose qu'il y a quelque chose quine vapas avec le code ci-dessus?