Count posts of tag and category
-
Hello,
How can I add php count of all results this selection:
<?php
$current_tag = single_tag_title(“”, false);
query_posts(array(
‘cat’=>’29,27,11,32,35,9,58,54,39’,
‘showposts’=>’2’,
‘tag_slug__and’=>array($current_tag),
) );
?>I want to show some link if there are more than 2 posts.
Many thanks for help
-
You don’t need
count()
, the count is already in the query object.
Insert after closing));
but before?>
:global $wp_query; if ( 2 < $wp_query->found_posts ) echo 'your link goes here';
Hello, thank you. it works. In link I have variable – current tag
<?php global $wp_query;
if ( 2 < $wp_query->found_posts ) echo ‘Dal?í ?lánky ‘;
echo $current_tag;
echo ‘‘; ?>If there is 1 result – it display text of that tag. Just this – result of echo $current_tag;
Could you please help me how to write it so that there will not be that result?
I’m not quite sure what behavior you are after. If what you want is to show one link when there are more than 2 posts and not display the content in
$current_tag
unless the post count is 2 or less, so that only one link or the other is output at any time, then this will do it:global $wp_query; if ( 2 < $wp_query->found_posts ) echo 'your link goes here'; else echo $current_tag;
no the current tag is part of link. Link text is More “curent tag”. But this $current_tag; is visible even if the result is 0
OK, forget the
else
suggestion then.Dealing with nothing found is a matter of wrapping the whole thing in another conditional.
if ( $wp_query->have_posts ) { // code here only runs if posts found }
I’m sorry, but I am still not fully understanding what you are after. If the above change is still not doing what you want, the forum’s parser kind of mangled the code you posted. Please re-post it, this time place your code within `
backticks
`Also, please post the desired output for both 1 or 2 results as well as more than 2 results. It’ll also be helpful for me to know the value assigned to
$current_tag
.I’m sorry for being so dense. I wish to fully understand what you want to accomplish so we no longer have to go back and forth with me guessing and being wrong.
Hello, firstly – many thanks for your help and your time.
SO Here is page with rome results
https://www.livit.cz/styl/ikea/In button you can see tag name IKEA – thats correct.
If there is only one restult, there is no button for given category.
But tag name IKEA apears at top of page see screen https://snag.gy/Ux2G48.jpg
Here is all source of it
<?php if ( is_tag()) { ?> <h1><?php single_tag_title(); ?></h1> <p>P?íspěvky ?títku: <strong><?php single_tag_title(); ?></strong> pro vás vypisujeme ?leněny dle hlavních obsahovych kategorií.</p> <h2 id="tag-headline">?lánky</h2> <?php $current_tag = single_tag_title("", false); query_posts(array( 'cat'=>'29,27,11,32,35,9,58,54,39', 'showposts'=>'2', 'tag_slug__and'=>array($current_tag), ) ); ?> <?php while (have_posts()) : the_post();?> <div style="width:47%; float: left;"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail("small"); ?></a> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_author(); ?> <?php the_date(); ?> <?php $key_1_value = get_post_meta( get_the_ID(), 'magazin-druh', true ); // Check if the custom field has a value. if ( ! empty( $key_1_value ) ) { echo '<a href="https://www.livit.cz/magazin?_sfm_magazin-druh='; echo $key_1_value; echo '">'; echo $key_1_value; echo '</a>'; } ?> <p><?php the_excerpt() ?> </p> </div> <?php endwhile; ?> <?php global $wp_query; if ( 1 < $wp_query->found_posts ) echo '<a id="tag-dalsi-link" href="/magazin">Dal?í ?lánky '; echo $current_tag; echo '</a>'; ?> <h2 id="tag-headline">Fotky</h2> <?php query_posts(array( 'cat'=>'30,15,33,36,7,37,59,55,40', 'showposts'=>'3', 'tag_slug__and'=>array($current_tag), ) ); ?><?php while (have_posts()) : the_post(); ?> <div style="width:47%; float: left;"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail("small"); ?></a> </div> <?php endwhile; ?> <?php global $wp_query; if ( 1 < $wp_query->found_posts ) echo '<a id="tag-dalsi-link" href="/fotogalerie">Dal?í fotky '; echo $current_tag; echo '</a>'; ?> <h2 id="tag-headline">Dodavatelé</h2> <?php query_posts(array( 'cat'=>'43', 'showposts'=>'3', 'tag_slug__and'=>array($current_tag), ) ); ?><?php while (have_posts()) : the_post(); ?> <div style="width:47%; float: left;"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail("small"); ?></a> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> </div> <?php endwhile; ?> <?php global $wp_query; if ( 1 < $wp_query->found_posts ) echo '<a id="tag-dalsi-link" href="/magazin">Dal?í dodavatelé '; echo $current_tag; echo '</a>'; ?>
Ah! I see now, thanks for posting all of that. As they say, “A picture (and webpage) is worth a thousand words.”
Just delete the occurrences of
echo $current_tag;
When I said “forget theelse
suggestion then”, I meant the entire line. I should have been more explicit, my apologies.Hi, amny thanks for help, but we are probably not undertanding each other :-).
I need to display button if there are more than 2 satisfying conditon.
(current tag and given category)This button should conssit of current tag text. F.e. “more photos ikea” (ikea is current tag)
So I need to have it there
echo $current_tag;If there are less then 2 result, there should not be link “more photos ikea”
BUT part of link (current tag) is still displaying – rounded in screen:https://snag.gy/Ux2G48.jpg.
Do you have any idea how to solve it?
Regards
JanI think I understand, the problem is I’m not paying enough attention. Yes, you need to echo out
$current_tag
in that context. I had it in my mind that the entire link output was on one line and theecho $current_tag;
was an artifact from my initialelse echo $current_tag;
Breaking up the link into multiple lines is fine, it makes the code more readable. But it breaks the shortcut syntax I originally used. Once code that should only be applied for a particular condition spans more than one line, you need to delimit where the condition applies with curly braces {}.
if ( 1 < $wp_query->found_posts ) { echo '<a id="tag-dalsi-link" href="/magazin">Dal?í ?lánky '; echo $current_tag; echo '</a>'; } ?>
Without the curly braces, only the one following line is managed by the conditional, the rest are always executed regardless of condition.
I’m sorry I didn’t give your issue the attention it deserves.
hi, many thanks. button is okay now. Can I ask you for one more advice?
On screen you can see all possible tags
https://snag.gy/dhVXt3.jpgCondition works only for IKEA, FRANCIE, POLICE and does now work for tag with apostrof like LEVNě, ?ERNOBíLá, PA?í?.
I guess that problem is somewhere here (probably with slug)
‘<?php
$current_tag = single_tag_title(“”, false);
query_posts(array(
‘cat’=>’29,27,11,32,35,9,58,54,39’,
‘showposts’=>’2’,
‘tag_slug__and’=>array($current_tag),
) );?>
‘Have you anz idea how to fix it? (I would pay via paypal some money for these help)
code again:
<?php $current_tag = single_tag_title("", false); query_posts(array( 'cat'=>'29,27,11,32,35,9,58,54,39', 'showposts'=>'2', 'tag_slug__and'=>array($current_tag), ) ); ?>
You must avoid offering payment in these forums, doing so can result in your topic being closed. While I appreciate your willingness to pay, it is not necessary, I’m not in this for the money ??
I think you’re right, that code does have a problem.
single_tag_title()
returns the tag’s name, which is not the same as the tag’s slug, which is what is required in the query. Try this instead:
$current_tag = get_queried_object()->slug;
thank you very much
- The topic ‘Count posts of tag and category’ is closed to new replies.