I'm trying to create 3 rows of buttons to select. Think sorting boxes. AT&T has it on this page. Where you see iPhone, Samsung and then sorting. I'm trying to do the same thing. AT&T's example page
The first row will be sub categories of parent categories. That part I was successful in completing. However, I'm struggling to call to all the parent attributes.
<?php
global $product;
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
$product_attributes = array_keys($product_attribute);
$count = 0;
echo '<a class="archive-attributes" href="#">';
foreach ($product_attributes as $product_attribute){
$attribute_name = ucfirst( str_replace('pa_', '',
$product_attributes) );
echo $attribute_name . '';
if($count != count($product_attribute)) echo '';
else echo '';
}
echo '</a>';
}
?>
I only need the parent for this row. When one of these is clicked the children of that attribute will display below it. What am I doing wrong?
I'm a Dutch ICT student asking for help,
I know how to change the column layout based on the width of the screen but I want to change the layout for odd and even objects.
I want to accomplish the following:
So I want my layout to be:
col[7]col[5]
col[5]col[7]
col[7]col[5]
col[5]col[7]
I can't do this manually because I linked it with WordPress, so when I post a new item I want it to automatically get the right column layout.
My code looks like this:
Please try below solution hope this work for you.
With the using Modulo, for that, you need to declare $i=0; variable in above while loop also set $i++; at the bottom for increment $i value.
Please use below code.
<?php
$i=0;
while( $qry->have_posts() ): $qry->the_post();
if ($i % 2 == 0)
{
echo '<div class="col-md-7">7</div>';
echo '<div class="col-md-5">5</div>';
}
else
{
echo '<div class="col-md-5">5</div>';
echo '<div class="col-md-7">7</div>';
}
$i++;
?>
<?php endwhile; ?>
I am trying to display a line of text if a specific category (in this case: advisories) is empty, but the code I am trying to use does not display anything.
<?php
if ( in_category( 'advisories' )->count > 0 ) {
echo test;
}
?>
Try this one,
Solution
$custom_terms = get_terms();
foreach ($custom_terms as $term) {
if ($term->count != 0 && $term->name == "Uncategorized") {
echo $term->name . " - blank" . '<br>';
}
}
Screenshot
Hope this will helps you.
For more example, please visit
get_terms Wordpress
WordPress to test for an empty term or category
You can try this one
$category = get_category($id);
$count = $category->category_count;
if( $count > $something ) {
// stuff
}
Ok, so i have created a post slider that contains 10 posts.
I have a php code that generate the slider and make each of these posts to have a specific class (small, middle, large image)
Problem:
I can't retreive the post link so it can be clickable.
$post->post_title
How can i add the:
$post->post_title
The code looks like this:
if($index === 0){
$output .= "<div class=\"page page-one\">";
}
$itemClass = $index < 2 ? "item-size-medium" : "item-size-small";
$img = get_the_post_thumbnail_url($post, 'large');
$output .= <<<HTML
<div class="item $itemClass">
<div class="item-img">
<img src="$img">
</div>
<div class="item-title">
<h1>
$post->post_title
</h1>
</div>
</div>
HTML;
if(($index !== 0 && ($index%5) === 0) || 1 == 2){
$output .= "</div>";
}
What's up Dragos. Assuming you're inside a loop (you referenced $post), you can use permalink or likely it'll be within the $post object if looping over a WP_Query. Reference
Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.
I want to show only 3 post on my website.
I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
I want to show the complete title of my wordpress blogpost but specific letters of description.
Also the description should end up with a word not some piece of non-dictionary word ending with "..."
How this can be done, i have heard that it can be done through RSS.
Can somebody help me?
To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".
First we will make a script to trim the description and to put "..." in last:-
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Now we will define the variables in which we store the values:-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)
for ($i=0; $i<3; $i++)
{
$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}
?>
Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-
FIRST RECENT POST:
<?php echo $item_title[0]; ?>
<?php echo substrwords($item_description[0],70); ?>
SECOND RECENT POST:
<?php echo $item_title[1]; ?>
<?php echo substrwords($item_description[1],70); ?>
THIRD RECENT POST:
<?php echo $item_title[2]; ?>
<?php echo substrwords($item_description[2],70); ?>
Hope this can solve your problem. By the way Nice question.
Click here for the original documentation on displaying RSS feeds with PHP.
Django Anonymous's substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.
Full Code:
blog.php
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.
Example:
social.php
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
Also, this code is plug-n-play friendly.
Why not use the Wordpress REST API to retrieve posts -
API URL is : https://public-api.wordpress.com/rest/v1/sites/$site/posts/
where $site is the site id of your wordpress blog
or else simply use this plugin -
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html