Show Multisite Subpages WordPress - php

I have a Multisite installation:
1: The main site - domain.com
2: other sites - domain.com/some-url
FIXED: So far so good. My script shows the content automatically below the header and not where my shortcode is inserted.
function subsites_list_sites() {
$subsites = get_sites();
if ( ! empty ( $subsites ) ) {
echo '<div class="subsites-container">';
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars( $subsite )["blog_id"];
$subsite_name = get_blog_details( $subsite_id )->blogname;
$subsite_link = get_blog_details( $subsite_id )->siteurl;
echo '<div class="subtite-item site-' . $subsite_id . '">
<a class="thumb" href="' . $subsite_link . '">Here i want the featured image/thumbnail to display</a>
<a class="title-link" href="' . $subsite_link . '"><h3 class="title">' . $subsite_name . '</h3></a>
<a class="excerpt" href="' . $subsite_link . '"><p>Here i want excerpt<p></a>
<a class="btn-link" href="' . $subsite_link . '">GO TO WEBSITE</a>
</div>';
}
echo '</div>';
}
}
add_shortcode( 'subsites_list_sites_sc', 'subsites_list_sites' );

Shortcodes should return the html not print it in the shortcode function.
Try like this:
function subsites_list_sites() {
$subsites = get_sites();
$returnHtml = '';
if ( ! empty ( $subsites ) ) {
$returnHtml .= '<div class="subsites-container">';
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars( $subsite )["blog_id"];
$subsite_name = get_blog_details( $subsite_id )->blogname;
$subsite_link = get_blog_details( $subsite_id )->siteurl;
$returnHtml .= '<div class="subtite-item site-' . $subsite_id . '">
<a class="thumb" href="' . $subsite_link . '">Here i want the featured image/thumbnail to display</a>
<a class="title-link" href="' . $subsite_link . '"><h3 class="title">' . $subsite_name . '</h3></a>
<a class="excerpt" href="' . $subsite_link . '"><p>Here i want excerpt<p></a>
<a class="btn-link" href="' . $subsite_link . '">GO TO WEBSITE</a>
</div>';
}
$returnHtml .= '</div>';
}
return $returnHtml;
}
Output
The return value of a shortcode handler function is inserted
into the post content output in place of the shortcode macro. Remember
to use return and not echo - anything that is echoed will be output to
the browser, but it won't appear in the correct place on the page.
https://codex.wordpress.org/Shortcode_API#Output

Related

Issue with PHP executing twice

I am displaying a list of filters using Isotope. The functionality works when the first part of the code works however, when the screen is resized the posts don't filter when it executes the last part... Any help is much appreciated!
<?php
$filterLinks = array();
$newarray = array();
$starter = array('name'=>'View All','slug'=>'*');
$filterLinks[] = $starter;
$taxterms = get_terms( $customTax );
if ( ! empty( $taxterms ) && ! is_wp_error( $taxterms ) ){
foreach ( $taxterms as $taxterm ) {
$datafilter = '.' . $taxterm->slug;
$newarray = array(
'name' => $taxterm->name,
'slug' => $datafilter,
);
$filterLinks[] = $newarray;
}
}
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
?>
Functionality Works:
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
Functionality Doesn't Work:
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
Is this because i'm executing it twice, just hiding one on desktop and showing the other on mobile?
Looking for some direction. Thanks in advance!
You're using id="filters" twice, which is improper use of the id attribute, since it should be unique. Not sure if that is of any concern?

Permalink of specific post ID

Below the code I have on my page tot display the permalink of q specific post in Wordpress. It works, but I have the feeling it can be easier. Can somebody explain how?
$post_id = 26; // post id
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$content = $queried_post->post_content;
$perma = get_permalink($post_id);
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
}
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo $content;
Well your code seems ok. However there are some improvements that can be made.
For example, when you have the $queried_post object, you don't need to create additional variables for content and title. You can use this object properties to get the values.
Also you can use wordpress get_the_post_thumbnail to show featured image.
Some formatting and it's almost perfect.
$post_id = 26; // post id
$queried_post = get_post($post_id);
echo '<a href="' . get_permalink( $post_id ) . '" title="' . $queried_post->post_title . '">';
echo $queried_post->post_title;
echo '</a>';
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail( $post_id, 'full', array('width' => '100%') );
}
echo $queried_post->post_content;
try this code
$post_id = 26;
if ( has_post_thumbnail($post_id) )
{
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id),’thumbnail’ );
}
echo '<a href="' . get_the_permalink($post_id) . '" title="' .get_the_title($post_id). '">';
echo get_the_title($post_id);
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo get_the_content($post_id);
In response to your comment, if you want to simplify this line:
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
You could do:
echo '' . $title . '';
So replacing multiple echo; with the "." to 'continue' without breaks.

Fatal error: Call to a member function url() on a non-object on line 8

I am new to PHP, currently getting error saying
Fatal error: Call to a member function url() on a non-object on line 8
Below is the code I am trying
<?php
$subpages = $site->pages()->children()->visible();
$image_url = $subpages->image()->url();
$title = $subpage->title()->text();
foreach($subpages as $subpage) {
echo '<div class="col-md-4">';
echo '<h2>' . $title . '</h2>';
echo '<a href="' . $subpage->url() . '" title="' . $title . '">';
echo '<img src="' . $image_url . '" alt="' . $title . '" class="img-responsive img-thumbnail">';
echo '</a>';
echo '</div>';
}
?>
Here's your code corrected:
<?php
$subpages = $site->children()->visible();
foreach($subpages as $subpage) {
$image_url = $subpage->image()->url();
$title = $subpage->title()->html();
echo '<div class="col-md-4">';
echo '<h2>' . $title . '</h2>';
echo '<a href="' . $subpage->url() . '" title="' . $title . '">';
echo '<img src="' . $image_url . '" alt="' . $title . '" class="img-responsive img-thumbnail">';
echo '</a>';
echo '</div>';
}
?>
What's wrong with your code:
You should use $site->children() to list all children of the site
See kirby docs
You're defining $image_url and $title before the foreach which is not correct. I moved them just at the beginning of the foreach
loop. Also corrected image_url to use subpage instead of subpages.
You're using text() on your title. That doesn't exist, use either kirbytext() or html() depending on what you want to do. See the
docs.
You should check what are the values and methods in $subpages? i think there is problem when you assigning the values to the variable.

How to change class of first echo result in PHP?

Easy one, but I can't find an answer.
I wanna to list sub-categories to create tabs in code below. This one works fine in my output. But the first tab needs a <li class= "selected" > to show the content on page load .
How to force the first echo result to include a class on <li> element ?
.
.
.
[ Output ]
Actual: .......... Category 1 | Category 2 | Category 3
Needed: .......... Category 1* | Category 2 | Category 3
*class="selected"
<ul class="cd-tabs-navigation">
<?php
$argumentos = array(
'hide_empty' => 1,
'child_of' => 44,
);
$categories = get_categories($argumentos);
foreach($categories as $category) {
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
?>
</ul>
.
.
[ EDIT ]
The solution by Lal. Thank you everyone!
foreach($categories as $category) {
if(++$i==1)
echo '<li class="selected"><a class="selected" data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
else
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
I would suggest you to use a counter.. Not sure if this is the right way to do it..
Change your foreach as follows
$i=0;
foreach($categories as $category) {
if($i==0)
echo '<li class="selected"><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
else
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
$i++;
}
OR
As suggested in the answer here, you could use array_shift() to process the first item in the array separatley.
That is, do it as below
$first = array_shift($categories);
echo '<li class="selected"><a data-content="' . $first ->slug . '" href="#' . $first ->slug . '">' . $first ->name . '</a></li>';
foreach($categories as $category) {
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
Read more about array_shift() in the docs
Lal's solution will work fine (as long as you increment $i), but here's a simpler way to do it without an extra variable for the counter :
foreach($categories as $index => $category){
if(!$index) echo "This is the first element";
else echo "This is not the first element";
}
(not sure if this should be an edit or an answer since it's basically the same method but with a slightly simpler synthax, I'll post as an answer for now)
You can use this syntax for foreach. Using this syntax you have in $k the index of the iteration, while in $v the value of the array you're iterating. So if $k == 0 you're in the first iteration.
$a = array("first", "second", "third", "fourth");
foreach ($a as $k=>$v) {
echo $k . "--->" . $v ;
if($k == 0){
echo "First iteration!";
//Do your stuff
}
echo "<br>";
}

The_excerpt showing before the div

i've created latest post container, which i'm trying to create in a shortcode, however when i add the excerpt into the div it is not being shown as it would normally. it is being pushed before the div. The test string after the excerpt is being placed the correct place inside in the div. How come the excerpt is being pushed to the top?
if($insta->have_posts()) :
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
$post_excerpt = the_excerpt();
$permalink = get_permalink();
$output = '<div class="betting-item">'
. '<div class="betting-thumb">'
. '<a href=" '. $permalink .' ">'
. '<img class="thumbnail" src="'. $thumb_url[0] .'" alt="0" />" </a>'
. '</div>'
. '<div class="betting-description">'
. '<h6>Instalocket</h6>'
. '<div class="meta">November 18, 2014 • No Comments</div>'
. '<div>' . $post_excerpt[0] . ' test</div>'
. '</div>'
. '</div>';
endwhile;
endif;
That is happening because the_excerpt() will print the except there and then (so before the div), you need to use get_the_excerpt () which will allow you to print it out at a later stage (in the div).
EDIT:
This should work and only print the excerpt on the first loop:
if($insta->have_posts()) :
$count = 0;
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
if($count == 0)
{
$post_excerpt = the_excerpt();
$count = 1;
}
else
{
$post_excerpt = '';
}
$permalink = get_permalink();

Categories