Wordpress: How to Put a Function inside PHP? - wordpress

with
<?php
if (is_category())
echo single_cat_title();
?>
i can show up the current category title..
how can i insert the tag single_cat_title() into:
<?php
$recent = new WP_Query("cat=10&showposts=4"); while($recent->have_posts()) : $recent->the_post();
if (in_category(10) && in_category(**Insert Here**)) { ?>
i tried it with
if (in_category(10) && in_category('.single_cat_title.')) { ?>
but no chance...
thank you!

Probably best to put it into a variable
$cat_title = single_cat_title();
then
if (in_category(10) && in_category($cat_title)) { ?>

Related

Show the amount of posts in a tag in a specific category has

I am creating a page template in Wordpress that displays multiple tags that are in a particular category. I have this working, but now I want to have the number of posts within each tag that is displayed as well like if I had a tag called apples with 5 posts it would look like this:
Apples(5)
As of now it just shows Apples
Here is my code I want to modify:
<?php
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name=health');
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
echo "<ul>";
foreach($tag_IDs as $tag_ID){
echo ''.$tag_names[$tag_ID].'';
}
echo "</ul>";
?>
I think you're looking for the following:
Outside the loop for the current query:
<?php echo $wp_query->get_queried_object()->count; ?>
Inside the loop for each terms:
<?php echo $tag->count; ?>
Regards.

$_GET Parameter already exist, replace parameter

I have a question im busy with a filter inside of wordpress. Now i have problem i try to explain.
If a visitor select a "single merk" url gonna be ?merk=Brand1 and than option 2 is select "Soort" than the url is gonna be "?merk=Brand1&soort=Soort1" that works.
But if you have select both, than want to change only ?merk=Brand2 than the url is "?merk=Brand2&soort=Soort1&merk=brand1" how can i make it if you switch between the "merk" that "Brand1" replaced to "Brand2".
Here is the code:
foreach ( $allemerken as $merksingle ) { ?>
<?php if(isset($_GET['merk']) && $_GET['merk'] == $merksingle->slug){?>
<i class="fa fa-times" aria-hidden="true"></i> <?php echo $merksingle->name; ?>
<?php }else{ ?>
<?php echo $merksingle->name; ?>
<?php }?>
<?php }?>
<p class="mt-30"><strong>Soort:</strong></p>
<?php
$soorten = get_terms('warmtepompcategorie');
// echo '<pre>'.print_r($allemerken,true).'</pre>';
foreach ( $soorten as $soort ) { ?>
<?php if(isset($_GET['soort']) && $_GET['soort'] == $soort->slug){?>
<i class="fa fa-times" aria-hidden="true"></i> <?php echo $soort->name; ?>
<?php }else{ ?>
<?php echo $soort->name; ?>
<?php }?>
<?php }?>
Ps. sorry for my bad English, but hope someone can help me out!
I have made a function.
And i did it like this:
function replace_get($newname,$newvalue){
$oldvars = $_GET;
$varsnew = $newname.'='.$newvalue;
foreach($oldvars as $name=> $value){
if($name != $newname){
$varsnew .= '&'.$name.'='.$value;
}
}
return $varsnew;
}
To prevent the values from duplicating I would operate on a $_GET array, or its copy for adding or removing the parameters like:
$_GET['soort'] = 'new value'; // adding, modifying value
unset($_GET['soort']); // removing value
and then using http_build_query() for building the URL-encoded query string like this:
$query = http_build_query(array('aParam' => $data));

Wordpress is_page_template query

i use a Plug-in on my wordpress site which uses a query to show search results, thats the code of the query:
if ( $query->have_posts() )
{
?>
<?php echo $query->found_posts; ?> Ergebnisse gefunden<br />
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<?php if ( has_post_thumbnail() ) { echo '<p>'; the_post_thumbnail("small"); echo '</p>'; } ?>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Previous | Next
<?php
}
else
{
echo "No Results Found";
}
The search should only display results from pages who uses a specific page template (mytemplate.php)
I found the Docs http://codex.wordpress.org/Function_Reference/is_page_template who explain this, but i dont get it to work inside the above query.
any help would be nice :) thx
Please do as below.
if ( is_page_template( '{enter the name of page template with extension}' ) ) {
// Enter your if code here
} else {
// Enter your else code here.
}
For this code to work you must select the page template option while creating the page in WordPress Admin Interface.

How to remove ducplicate tags from custom tag list?

I have this code to display the tags which are used in the current category, includes child categories:
if (is_category( )) {
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
query_posts('category_name='.$yourcat->slug.'');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
Is it possible to remove duplicate tags? Right now the code shows some tags multiple times instead of once.
Another question about this code: How can I output each tag like this to create a checkbox form?
<input type="radio" name="tag" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>> Tag1<br>
Hopefully someone can help me with this. Thanks in advance!
Update:
I forgot to mention that I use the code if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";} at the end of the input field to check the checkbox when the tag is used, but if I use that code (which works in html) the page don't display properly.
I've modified your code a bit, but now you have an array of tag id's which you can use for any purpose, for ex. list added below.
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
echo "<ul>";
foreach($tag_IDs as $tag_ID){
echo ''.$tag_names[$tag_ID].'';
}
echo "</ul>";

Latest Post by Date

How can I achieve something similar to this:
http://anidemon.com/latest-episodes/
Latest Posts are organized under header of the day. I don't about the "View" count.
How can I do this?
Virendar,
Using get_posts() should solve your problem as follows...
<ul>
<?php
$myposts = get_posts('orderby=date&order=DESC');
$tempdate = '';
foreach($myposts as $post) {
$postDate = strtotime( $post->post_date );
if ($tempdate == '' || $postDate < $tempdate) {
$tempdate = $postDate;
echo '<h3>Display Date Title here</h3>';
}
?>
<li><?php the_title(); ?></li>
<?php } ?>
</ul>
Hope this helps! Please note I haven't included any CSS formatting.

Resources