This is what I have:
This is what I'm trying to achieve:
This is the code I'm trying to modify to achieve it:
<?php do_action( 'bbp_theme_before_topic_title' ); ?>
<span class="bbp-topic-started-in"><?php
printf( __('<a class="dog" href="%1$s">%2$s</a>', 'bbpress' ),
bbp_get_forum_permalink( bbp_get_topic_forum_id() ),
bbp_get_forum_title( bbp_get_topic_forum_id() ) );
?></span>
<a class="bbp-topic-permalink" href="<?php bbp_topic_permalink(); ?>"><?php bbp_topic_title(); ?></a>
As you can see, it assigns the class "dog" to all forum links. I need it to assign separate classes to the different forums, based on the forum ID, so that I can style them differently.
What I'm thinking is maybe an if statement, but I'm not sure how to execute it properly. Anyone have any suggestions?
You can try something like this:
<?php do_action( 'bbp_theme_before_topic_title' ); ?>
<span class="bbp-topic-started-in">
<?php
$forumId = bbp_get_topic_forum_id();
// Replace these values with your forum IDs and css classes
if ($forumId == 5) {
$class = "dog";
} elseif ($forumId == 6) {
$class = "cat";
} else {
// Remember to specify a default css classname
// in case we didn't match any of the 'if' cases above
$class = "mouse";
}
// Use the $class variable that we set above to dynamically
// replace the classname with the value we want
printf( __('<a class="' . $class. '" href="%1$s">%2$s</a>', 'bbpress' ),
bbp_get_forum_permalink($forumId),
bbp_get_forum_title($forumId);
?>
</span>
Notice how I set bbp_get_topic_forum_id() to a variable and use it both in the if and in the printf further down. Doing this is good practice as it avoids needlessly calling that function multiple times.
Related
In a display of wordpress, I used the esc_html__() method to escape the string and add variables for safe use in HTML output.
My code is as follows:
<?php
global $product, $post;
$posts_id = $product->get_id();
$reserve_price = get_post_meta($posts_id, '_auction_reserved_price', true);
if ($product->is_reserved() === true && $product->is_reserve_met() === false) : ?>
<p class="reserve hold" data-auction-id="<?php echo esc_attr( $product->get_id() ); ?>">
<?php echo apply_filters('reserve_bid_text', esc_html__('Reserve price has not been met, needed to enter $ %s or more', 'wc_simple_auctions', $reserve_price)); ?>
</p>
<?php endif; ?>
But my variable is not output to the final value, the output string I get is this:
I tried before $reserve_price is a non-empty variable, but esc_html__() doesn't output the correct information to the page.
I am not quite sure about this reason.
"I tried before $reserve_price is a non-empty variable, but esc_html__() doesn't output the correct information to the page."
You can not use placeholders in esc_html__() function. It only retrieves the translation of a given text and escapes it for safe use in HTML output. Which means you could use it to:
Escape html markups + translation
However, if you would need to:
Escape html markups + translation + placeholder(s)
Then you could use the combination of esc_html, sprintf and __() functions, like so:
$test_value = '<strong>5.7<strong>';
echo esc_html(
sprintf(
__('This is a test for %s', 'your-text-domain'),
$test_value
)
)
Which will output this:
And if the provided text does not have any html tag(s), then it'd be something like this:
$test_value = 5.7;
echo esc_html(
sprintf(
__('This is a test for %s', 'your-text-domain'),
$test_value
)
)
Now, if we apply the same principles to your snippet, then it would be something like this:
<p class='reserve hold' data-auction-id='<?php echo esc_attr($product->get_id()); ?>'>
<?php
echo apply_filters(
'reserve_bid_text',
esc_html(
sprintf(
__('Reserve price has not been met, needed to enter $ %s or more', 'wc_simple_auctions'),
$reserve_price
)
)
);
?>
</p>
Let me know if you could get it to work!
I'm trying to do something that seems really simple but I can't figure it out.
In my category template, I have this shortcode:
<?php echo do_shortcode( '[jobs categories="manufacturing"]' ); ?>
I also have this to show the title of the category:
<?php single_cat_title(); ?>
I would like to put them both together so I can pass the category into the shortcode so it fetches the correct job category listings. So I created this:
<?php $category = single_cat_title(); ?>
<?php echo do_shortcode('[jobs categories=' . $category . ']'); ?>
but it doesn't seem to work properly - Am I doing something wrong? Is this even possible?
Many thanks!!
If you look at the documentation for the single_cat_title() function, you'll see that it can either display or return the value, and it accepts two parameters $prefix and $display with default values of '' and true respectively.
What this means, is that just using single_cat_title(); will print Category Title to the document.
If you want to use it as a variable (without printing it to the document), you'll need to set the second parameter to false:
$category = single_cat_title( '', false );
This will define the $category variable for you, without printing anything, and you can then pass it to your shortcode (also note, that typically you'll want quotes around your attribute values in shortcodes):
echo do_shortcode( '[jobs categories="' . $category . '"]' );
You can make it a bit more succinct as well:
<?php
$category = single_cat_title( '', false );
echo do_shortcode( "[jobs categories='$category']" );
?>
I made the ACF plugin group with files to download. In group I have fields "File 1", "File 2"...etc.
I would like to display all attached files to page. It is possible to display all fields belong to group? I try with basic code, but in this case I have only 1 file.
How can I add iteration to this or display all fields?
<?php
$file = get_field('attachment_1');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<ul>
<li><a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<ul>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
As all your fields are set up individually, it isn't just a matter of looping through an array of all your fields of the same type (i.e. just your file fields).
There are a few ways that might work for you:
Option 1.
If all the field names for your files follow the same naming pattern and are named sequentially, you could loop using the name.
Example, assuming your fields are named attachment_1 up to attachment_5:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
// display file details as appropriate
}
}
Option 2.
If the file field names do not follow the same pattern, you could loop through an array of the field names.
Example:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
// Create an array with the field names of all your files
// N.B. This also lets you specify the order to process them
$file_fieldnames = array('file_1', 'file2', 'another_file');
foreach ($file_fieldnames as $fieldname) {
$file = get_field($fieldname);
if( $file ){
// display file details as appropriate
}
}
Option 3. If you want to loop through ALL fields on the post/page, you can save the fields into an array.
This might seem like the most generic approach at first, but it is complicated by the fact that you don't know what type each field is in order to know how to process and display them... you first have to work out what field type it is. You could do this by name (similar to above) or you could try to identify what each field by checking the field content.
Note, checking the field content is very risky, as there are other field types that can have similar featured (e.g. a file is not the only type that can have a url) so I wouldn't advise that strategy unless you are 100% sure you'll never change the field group or add another field group to the post/page.
Example:
$fields = get_fields();
foreach ($fields as $fieldname => $content) {
if (is_string ($content)){
// display the string
}
else if (is_array($content) && $content['url']) {
// then you could assume its a file and display as appropriate
}
}
Note that none of this code is tested. However it should give you an idea of the logic behind each option so you can decide what works for you.
UPDATE based on new code provided:
See below based on the code in your JSFiddle. I've ignored the caption outside the file list because it makes no sense - every file can have its own caption.
<?php
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// now display this file INSIDE the loop so that each one gets displayed:
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php endif; ?>
</li>
<?php
} // end if
} // end for loop
?>
<ul>
If you understand arrays, I'd suggest you add the file details into an array and then do a second loop to display the files... however I'm guessing you're not that proficient with basic coding constructs as you don't understand loops, so I've tried to keep it simple. I strongly recommend that you do some tutorials on programming basics if you are attempting to write code.
Okay, so i've got a request for this update which I thought might be simple enough but is turing out a bit tricky than I though.
basically the site has these sub ids for each property 'Vest' and 'Verve' they want each one to be a different colour.
The css that's link to them is <span class="proptery-type-grid"> so obviously I'd add in a second css for each colour, but the id's in the php are being called like this: <span class="proptery-type-grid"<?php echo inspiry_get_property_types( $post->ID );?></span>
so what would be the best way (i'm thinking like a if or else statement) to go about targeting the individual ids to change the colours of them?
Thanks
Maybe you can use a ternary statement? http://us2.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Psuedocode:
echo ($post->ID('1') ? '<span class="proptery-type-grid"<?php echo inspiry_get_property_types( $post->ID );?></span>' : '<?php echo inspiry_get_property_types( $post->ID );?>');
I've solved it with
<span class="proptery-type-grid"><?php $post_terms = get_the_terms( $post->ID, 'property-type' );
foreach( $post_terms as $term) {
if( $term->name == 'Verve') {
echo '<span class="proptery-verve">Verve</span>';
} elseif( $term->name == 'Zest') {
echo '<span class="proptery-zest">Zest</span>';
} else {
echo '<span class="proptery-type-grid type-none"></span>';
}
}?></span>
Making mobile site with Concrete5 and using page list block with custom template. I'm trying to count sub pages using PHP.
<?php foreach ($pages as $page):
// Prepare data for each page being listed...
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$mies = 0;
?>
<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" data-theme="c"><div aria-hidden="true" class="ui-btn-inner ui-li"><div class="ui-btn-text"><a target="<?php echo $target ?>" class="ui-link-inherit" href="<?php echo $url ?>">
<h2 class="ui-li-heading"><?php echo $title ?></h2>
<p class="ui-li-desc"><?php echo $description ?></p>
</a>
</div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow"></span><span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($mies) ?></span></div></li>
<?php endforeach; ?>
So, probably need to use Count function(or length?), I don't know. If I am editing wrong place please advice if you have any experience in Concrete5 cms.
If you want to show the corresponding page number in the span element in your code:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo $mies; ?></span>
If you want to show the remaining sub-pages, then in the html code snippet above just replace $mies with count($pages) - $mies like:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($pages) -$mies; ?></span>
You would first have to initialise $mies before you start the forloop so it should be something of the form:
<?php
$mies = 0;
foreach ($pages as $page):
//Rest of your code and increment $mies with every iteration
$mies ++; //This ensures that you are counting the corresponding pages
?>
If you want to get the count of total number of sub-pages, you just have to echo out $mies outside the for block may be like:
<?php
endforeach;
echo $mies; //Shows you the total number of pages processed inside the for loop.
//or Just find the length of pages array
echo count($pages);
?>
As far as getting the length of array is concerned you could use count or sizeof. I stumbled upon a SO question about using count or sizeof method for finding the length of an array.
This should get you started in the right direction.
You need the parent ID;
$parentId = Page::getCollectionParentId();
Note that Page::getCollectionParentId() gets the current page's parent ID,so you may want to try;
$parentId = $page->getCollectionParentID();
Then create a new PageList to filter with and filter by the parentId;
Loader::model('page_list');
$pl = new PageList();
$pl->filter(false, '(p1.cParentID IN ' . $parentId . ')');
// Get the total
var_dump($pl->getTotal());
This is untested but the theory makes sense.
This is likely a bit simpler.
$pl->getTotal()
$pl is the PageList object that is set in the controller.
Also, these days you can just use the h() method instead of writing out $th->entities()
Edit: I should clarify that you don't need to do a $pl = new PageList() because $pl is already set to the PageList object in the controller and passed to the view.