si le message a du contenu
4 réponses
- votes
-
- 2013-11-06
Le contenuest unepropriété de l'objet
post
,pas de l'objet de requête.Utilisez à laplace
$post
ouget_post()
:if( '' !== get_post()->post_content ) { // do something }
The content is a property of the
post
object, not of the query object.Use
$post
orget_post()
instead:if( '' !== get_post()->post_content ) { // do something }
-
- 2017-09-10
Et qu'enest-il
if ( !empty( get_the_content() ) ){ //code }
What about
if ( !empty( get_the_content() ) ){ //code }
-
Vousne pouvezpaspasser unefonction à `empty ()`en tant que variable.Vous devez d'abord le stocker dans une variable.Même dans ce cas,celane fonctionnerapas,carilpeut y avoir desespaces vides dans votre contenu.You can not pass a function to `empty()` as a variable. You must first store it in a variable. Even then, it won't work, since there might be some empty spaces in your content.
- 0
- 2017-09-10
- Jack Johansson
-
Celafonctionnepourmoi!êtes-vous sûr?Aumoinsen PHP 7This is working for me! are you sure? At least in PHP 7
- 1
- 2017-09-11
- Juan Solano
-
- 2015-12-01
Celafonctionne égalementet teste des élémentstels que desbalises deparagraphe vides ou
dans le contenu,ce quipourraitentraîner l'échec d'une vérificationnormale.Voir http://blog.room34.com/archives/5360 pour l'idée originale -il suffit d'enregistrericipour queje puisse le retrouver.: O)Mettez ceci dans votrefunctions.php:
function empty_content($str) { return trim(str_replace(' ','',strip_tags($str))) == ''; }
if (function_exists('empty_content') && empty_content($post->post_content)) { ... }
Cela renverra
true
si le contenuest vide,false
sinon.This also works, and tests for things like empty paragraph tags or
in the content which might cause a normal check to fail. See http://blog.room34.com/archives/5360 for the original idea - just recording it here so I can find it again. :O)Put this in your functions.php:
function empty_content($str) { return trim(str_replace(' ','',strip_tags($str))) == ''; }
And put this where you want to run the check:
if (function_exists('empty_content') && empty_content($post->post_content)) { ... }
That will return
true
if the content is empty,false
if not. -
- 2019-07-11
J'aiimplémenté desméthodes "has_content ()"plusieursfois depuis des annéesmaintenantet il y atoujours assez detempsentre les deux,je dois donceffectuer unenouvelle recherchepour répondre à cette question.
detoutefaçon - c'estma solution,quej'aimetrouver laprochainefoisici - donc c'estpour référence.
toutes lesfonctions "à l'intérieur de laboucle"peuvent être remplacéespar un objetpost "post_content"
dansfunctions.phpet fichiers similaires:
// write inside the loop $the_content = apply_filters('the_content', get_the_content()); if ( !empty($the_content) ) { echo $the_content; } // with post object by id $post = get_post(12); // specific post $the_content = apply_filters('the_content', $post->post_content); if ( !empty($the_content) ) { echo $the_content; }
commefonction
// call inside the loop function mytheme_has_content(){ return !empty(apply_filters('the_content', get_the_content())); }
modèle dans laboucle:
<?php if ( $customQuery->have_posts() ) {?> <?php while ( $customQuery->have_posts() ) { $customQuery->the_post(); ?> <?php $the_content = apply_filters('the_content', get_the_content()); ?> <!-- html --> <?php if ( !empty($the_content) ) { ?> <div class="content"> <?php echo $the_content; ?> </div> <?php } ?> <?php } ?> <?php wp_reset_postdata(); ?> <?php } ?>
i have implemented some "has_content()" methods for several times over years now and there is always enough time in between so i need to search again a bit to answer this question.
anyways - this is my solution, which i like to find the next time here - so its for reference.
all "inside loop" functions can be replaced by a post objects "post_content"
in functions.php and similar files:
// write inside the loop $the_content = apply_filters('the_content', get_the_content()); if ( !empty($the_content) ) { echo $the_content; } // with post object by id $post = get_post(12); // specific post $the_content = apply_filters('the_content', $post->post_content); if ( !empty($the_content) ) { echo $the_content; }
as function
// call inside the loop function mytheme_has_content(){ return !empty(apply_filters('the_content', get_the_content())); }
template inside the loop:
<?php if ( $customQuery->have_posts() ) {?> <?php while ( $customQuery->have_posts() ) { $customQuery->the_post(); ?> <?php $the_content = apply_filters('the_content', get_the_content()); ?> <!-- html --> <?php if ( !empty($the_content) ) { ?> <div class="content"> <?php echo $the_content; ?> </div> <?php } ?> <?php } ?> <?php wp_reset_postdata(); ?> <?php } ?>
-
c'est la deuxièmefois queje consulte ceci,je laisserai un commentaire à chaquefois queje reviens à desfins statistiquesthis is the second time i consult this, i will leave one comment every time now i come back for statistik purpose
- 0
- 2020-03-10
- Thomas Fellinger
-
troisièmefoisicithird time here
- 0
- 2020-08-19
- Thomas Fellinger
Je suisen train de créer un site WordPress d'unepage.Je reçois despages répertoriées dansmon site quin'ontpas de contenu.Parexemple,j'obtiendrai lapage deblog vide ainsi que lemodèle deblog.J'ai doncpensé queje pourraisjeter un chèquepour voir si lapage a du contenuet sielle continueet affiche cetteinformation.J'ai dumal à lefairefonctionner.J'utilise une requêtepersonnaliséepour lapage d'accueil.Alorsj'aipensé queje pouvaisfaire ça
Leproblèmeest quej'obtiens uneerreur sur ce codeet jene peuxpas comprendrepourquoi.J'obtiens cetteerreur